Pytest - Starting With Basic Test



Now, we will start with our first pytest program. We will first create a directory and thereby, create our test files in the directory.

Let us follow the steps shown below −

  • Create a new directory named automation and navigate into the directory in your command line.

  • Create a file named test_square.py and add the below code to that file.

D:\Projects\python\myenv>scripts\activate

(myenv) D:\Projects\python\myenv>md automation

(myenv) D:\Projects\python\myenv>cd automation

(myenv) D:\Projects\python\myenv\automation>

Creating Test cases

Create a file with three test cases −

test_square.py

import math

def test_sqrt():
   num = 25
   assert math.sqrt(num) == 5

def testsquare():
   num = 7
   assert 7*7 == 40

def tesequality():
   assert 10 == 11

Output

Run the test using the following command −

(myenv) D:\Projects\python\myenv\automation>pytest

The above command will generate the following output −

============================ test session starts ================================
platform win32 -- Python 3.14.2, pytest-9.0.2, pluggy-1.6.0
rootdir: D:\Projects\python\myenv\automation
collected 2 items

test_square.py .F                                                          [100%]

============================ FAILURES ===========================================
____________________________ testsquare _________________________________________

    def testsquare():
       num = 7
>   assert 7*7 == 40
E      assert (7 * 7) == 40

test_square.py:9: AssertionError
============================ short test summary info ============================
FAILED test_square.py::testsquare - assert (7 * 7) == 40
============================ 1 failed, 1 passed in 0.28s ========================

See the first line of the result. It displays the file name and the results. F represents a test failure and dot(.) represents a test success.

Below that, we can see the details of the failed tests. It will show at which statement the test has failed. In our example, 7*7 is compared for equality against 40, which is wrong. In the end, we can see test execution summary, 1 failed and 1 passed.

The function tesequality is not executed because pytest will not consider it as a test since its name is not of the format test*.

Running Test cases with more details

Now, execute the below command and see the result again −

pytest -v

-v increases the verbosity.

============================= test session starts ================================
platform win32 -- Python 3.14.2, pytest-9.0.2, pluggy-1.6.0 
-- C:\Users\mahes\AppData\Local\Programs\Python\Python314\python.exe
cachedir: .pytest_cache
rootdir: D:\Projects\python\myenv\automation
collected 2 items

test_square.py::test_sqrt PASSED                                            [ 50%]
test_square.py::testsquare FAILED                                           [100%]

============================== FAILURES ==========================================
______________________________ testsquare ________________________________________

    def testsquare():
       num = 7
>   assert 7*7 == 40
E      assert (7 * 7) == 40

test_square.py:9: AssertionError
============================== short test summary info ===========================
FAILED test_square.py::testsquare - assert (7 * 7) == 40
============================== 1 failed, 1 passed in 0.06s =======================

Now the result is more explanatory about the test that failed and the test that passed.

Note − pytest command will execute all the files of format test_* or *_test in the current directory and subdirectories.

Advertisements