Pytest - File Execution



In this chapter, we will learn how to execute single test file and multiple test files. We already have a test file test_square.py created. Create a new test file test_compare.py with the following code −

test_compare.py

def test_greater():
   num = 100
   assert num > 100

def test_greater_equal():
   num = 100
   assert num >= 100

def test_less():
   num = 100
   assert num < 200

Now to run all the tests from all the files (2 files here) we need to run the following command −

pytest -v

The above command will run tests from both test_square.py and test_compare.py. The output will be generated as follows −

============================ 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 5 items

test_compare.py::test_greater FAILED                                       [ 20%]
test_compare.py::test_greater_equal PASSED                                 [ 40%]
test_compare.py::test_less PASSED                                          [ 60%]
test_square.py::test_sqrt PASSED                                           [ 80%]
test_square.py::testsquare FAILED                                          [100%]

============================ FAILURES ===========================================
____________________________ test_greater _______________________________________

    def test_greater():
       num = 100
>   assert num > 100
E      assert 100 > 100

test_compare.py:3: AssertionError
____________________________ 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_compare.py::test_greater - assert 100  100
FAILED test_square.py::testsquare - assert (7 * 7) == 40
============================ 2 failed, 3 passed in 0.07s ========================

To execute the tests from a specific file, use the following syntax −

pytest <filename> -v

Now, run the following command −

pytest test_compare.py -v

The above command will execute the tests only from file test_compare.py. Our result will be −

============================ 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 3 items

test_compare.py::test_greater FAILED                                       [ 33%]
test_compare.py::test_greater_equal PASSED                                 [ 66%]
test_compare.py::test_less PASSED                                          [100%]

============================ FAILURES ===========================================
____________________________ test_greater _______________________________________

    def test_greater():
       num = 100
>      assert num > 100
E      assert 100 > 100

test_compare.py:3: AssertionError
============================ short test summary info ============================
FAILED test_compare.py::test_greater - assert 100 > 100
============================ 1 failed, 2 passed in 0.07s ========================
Advertisements