How to Create the first Pytest Project

Create a project

For the first pytest project, a python project with a virtual environment with the required set of libraries is required. Pycharm interpreter is used to perform the test as it has Python with a built-in virtual environment in it.

Steps to get started to create a pycharm project to perform pytest test-

  • Create a new project in the pycharm interpreter.
  • Enter the file name pytest_project.
  • Make sure it is in Virtualenv mode.
  • The python version must be the latest version Python 3.9
Screenshot 2021 08 31 At 1.33.14 Pm
  • Create the project by clicking “Create” button on the bottom right of the application.

Getting started

  • Virtual environment is created in the Pycharm IDE for the execution.
1 47
  • Create a new python package by selecting on the python_project-> New -> Python package.
2 1
  • Name the python package according to the naming convention , refer here. Let us name our package “pytest_concepts” as we intend to perform the pytest operations in the files of this package.
3 1
  • To execute our first pytest file, create a file in the pytest_concepts package .
  • Just right click pytest package->New->Python File.
5 1
  • Name the python file according to the naming conventions, refer here. Let us name the first pytest file test_program1.
Screenshot 2021 09 03 At 12.32.56 Pm

Writing the first pytest test code

  • In the first pytest program, the code to be tested is defined under a function whose name is named under the naming conventions, refer here. Let our name our function “test_first()”.
  • Inside the function, we use “assert” for the statements to be validated to be true or false in the test.
def test_first():
    assert 3 + 2 == 5
    assert "calm" == "calm"
  • In the terminal of the Pycharm IDE, enter the following command.
pytest package_name
  • The command “pytest pytest_concepts” is entered in the terminal to obtain the output below.
Test1 1
  • Here, another function “test_second()” is defined. In the assert statement, we provide a statement “intentional failure of the test” which defines the statement to be printed with the assertiveness error.
def test_first():
    assert 3 + 2 == 5
    assert "calm" == "calm"

def test_second():
    assert 5 / 5 == 0, "intentional failure of the test"

Naming conventions

In the pytest program, there are certain naming convention rules to be followed.

  • To name a file of the pytest program, the naming should be in the format “test_filename”. The file name should start with “test_” followed by the preferred file name.
  • To name a function of the pytest program, the naming should be in the format “test_functionname”. The function name should start with “test_” followed by preferred function name.
  • To name a package that contains pytest program, the naming can be “pytest_packagename”. Though it is not a mandatory rule, it is highly preferrable as it can be understood easily by the other developers that the particular package contains pytest files.

Running the pytest program

The pytest programs can be run by Run command with the following steps-

  • Go to Settings-> Tools.
  • Change the “Default test runner” from Unittests to Pytest.
  • Click Okay.
Screenshot 2021 09 02 At 4.20.42 Pm
  • Right click on the first line of the file and select Run(file) option to execute all the test methods of the file.
Run All
Result All
  • The test results are obtained in the terminal. The failed test case of the file program is displayed in the small portion in the bottom-left.
Run One 1
  • If we wish to test only a particular function, place the cursor on the function and right click on it. From the drop down box, select the Run option.
  • The test output is obtained in the terminal.
Result One

Analysing the outputs


Let us decode the output obtained in the pytest test.

  • v(verbose option) is used to provide more information about the testcase that is run. However, the testcase runs fine without verbose option.
  • platform darwin indicates the platform in which the application is run. For windows, it is usually platform win32. It is followed by the python packages with its verssions used in this file.
  • The path for the python.exe in the local computer is followed by the pacakges.
  • cachedir indicates .pytest_cache
  • rootdir indicates the root directory of the folder pytest_project.
  • Collected 2 items indicates that two test cases have been validated in the test.
  • The next lines indicates the status of the test performed on each of the functions of the module in sequential order.
  • The percentage to the right side indiactes the accuracy of the passing of the test. The pass is indicated with green while the fail is indicatd with re. The overall accuracy is indicated with red atleast if there is a one testcase that fails.
  • The next section FAILURES show the details of the failed test cases.
 def test_second():
>       assert 5 / 5 == 0, "intentional failure of the test"
E       AssertionError: intentional failure of the test
E       assert 1.0 == 0
E         +1.0
E         -0

pytest_concepts/test_program1.py:8: AssertionError
  • In the above code snippet, E indicates Error, i.e., the staments which caused the code to fail.
  • In the last line of the snippet, it shows the statement line number where the test fails.
  • It is followed by short summary which indicates the overall performance of the module, i.e., the number of passed and failed test cases.

To analyse the failed test cases in the last run of the testcases, the following commands can be used.

--lf, --last-failed - to only re-run the failures.

--ff, --failed-first - to run the failures first and then the rest of the tests.
Screenshot 2021 09 03 At 4.32.51 Pm
  • For the last failed test case, .pytest_cache->v->cache->lastfailed.
  • In the above code snippet, it shows the failed function name as well as the file name in which the function is present.

Inspecting the cache content

The command pytest—cache-show will show all the contents of the cache.

Clearing cache content

The command pytest—cache-clear will help you clear the cache folder.

You can refer official document for more information.