Pytest File Execution

Let us create a basic pytest example and Execute the file in possible ways.

Below are the steps which needs to follow.

  • Create the python package as “pytestconcept”.
  • Create a file and name it as “test_PytestExample.py”
  • Create functions inside the test class file.
  • Execute the file using pytest command.
pytest concept package and file name

Below are the functions which we create inside the test class file.

test_PytestExample.py

def test_methodA():
    print("This is method A")


def test_methodB():
    print("This is method B")
    assert False


def test_methodC():
    print("This is method C")


def test_methodD():
    print("This is method D")


Below is the command which is used to execute pytest file test_PytestExample.py

Open the terminal and redirect to the package where test_PytestExample.py file exists and type the below command pytest.

pytest


OutPut data which generates after executing the above file.

OutPut data


admin@admin-MacBook pytest % cd pytestconcept
admin@admin-MacBook pytestconcept % pytest
============================================================================ test session starts ============================================================================
platform darwin -- Python 3.8.3, pytest-5.4.3, py-1.8.1, pluggy-0.13.1
rootdir: /Users/admin/Documents/Skill2Lead/Tutorials/pytest/pytestconcept
plugins: rerunfailures-9.0, allure-pytest-2.8.16, ordering-0.6
collected 4 items                                                                                                                                                           

test_PytestExample.py .F..                                                                                                                                            [100%]

================================================================================= FAILURES ==================================================================================
_______________________________________________________________________________ test_methodB ________________________________________________________________________________

    def test_methodB():
        print("This is method B")
>       assert False
E       assert False

test_PytestExample.py:7: AssertionError
--------------------------------------------------------------------------- Captured stdout call ----------------------------------------------------------------------------
This is method B
========================================================================== short test summary info ==========================================================================
FAILED test_PytestExample.py::test_methodB - assert False
======================================================================== 1 failed, 3 passed in 0.08s ========================================================================
admin@admin-MacBook pytestconcept % 



Explanation of output data.

  • Before it starts executing it will show “====test session starts====” in the consol.
  • Then it will shows root directory of the file along with pytest plugins versions data.
  • Then it shows the total number of functions or methods which need to be executed.
  • It shows the file name along with the dot “.” after file name, This dot “.” represents test function status as Pass.
  • If it shows F in red then it indicates test function as failed.
  • Finally, It shows the total number of test functions passed and failed along with the execution duration.