How to Run PyTest in Python?

Here is a step-by-step guide to running PyTest in Python.

Step 1: The first step to use PyTest in Python is to install it on the system. We can do so by executing the following command in our terminal:

pip install pytest

Step 2: For this article, we will take a simple Python Program as an example. We will write 6 different basic maths functions for addition, multiplication, subtraction, and division. It also contains two geometry functions to calculate the area and perimeter of the rectangle. All the functions are very much self-explanatory. We will name this file “maths.py“.

Python3




def calc_addition(a, b):
    return a + b
 
def calc_multiply(a, b):
    return a * b
 
def calc_substraction(a, b):
    return a - b
 
def calc_division(a, b):
    return a / b
 
def area_of_rectangle(width, height):
    area = width*height
    return area
 
def perimeter_of_rectangle(width, height):
    perimeter = 2 * (width + height)
    return perimeter


Step 3: In order to test these functions, we will create a new Python file “test.py“, import our above maths functions file and write test cases for those functions. The pytest of division function is not added intentionally so that we can capture that part of code in the test coverage and then analyze the generated coverage report.

Python3




import maths
 
def test_calc_addition():
    output = maths.calc_addition(2, 4)
    assert output == 6
 
def test_calc_substraction():
    output = maths.calc_substraction(2, 4)
    assert output == -2
 
def test_calc_multiply():
    output = maths.calc_multiply(2, 4)
    assert output == 8
 
def test_area():
    output = maths.area_of_rectangle(2, 5)
    assert output == 11
 
def test_perimeter():
    output = maths.perimeter_of_rectangle(2, 5)
    assert output == 14


Step 4: Now, we run Pytest on our Python program. To run Pytest, we need to run the following command.

pytest test.py

Output:

As we can see, the output that we have got for the given test file is very plain (pure ASCII) and not very easy to understand and debug-friendly. Hence, we use Pytest to generate interactive output.

Command to run pytest

PyTest: Interactive Output instead of pure ASCII

There are numerous benefits of testing the code. It assures that modifications to the code won’t result in regressions and boosts the confidence that the code acts as we anticipate. We should make the most of all the tools to make writing and maintaining tests as comfortable as we can because it is an exacting job. There are a number of different advantages of testing such as bug detection, functional verification, refactoring, maintenance, collaboration, documentation, and supporting CI/CD practices.

Similar Reads

PyTest in Python

The PyTest is a third-party managed unit testing library developed for Python developers. It is an open-source tool with a good user base. All different types of testing are supported in the Pytest framework such as integration testing, end-to-end run testing, functional testing, etc....

How to Run PyTest in Python?

Here is a step-by-step guide to running PyTest in Python....

How to Generate Interactive Output Instead of Pure ASCII?

...

Contact Us