We should write the test cases to test our software. These tests ensures no regression in existing functionality while adding new changes to the code.

pytest

pytest is a framework that makes building simple and scalable tests easy. Tests are expressive and readable—no boilerplate code required. pytest test cases are a series of functions in a Python file starting with the name test_.

Install

Run the following command in your command line:

pip install -U pytest

Create test

In order to understand the complete picture, before creating a test, let’s create a sample functionality to test.

Here we will implement a functionality which will multiply given two numbers. Create a new project folder and, inside that, create a new folder called my_multi. Inside my_multi, create an empty file called __init__.py. Creating the __init__.py file means that the my_multi folder can be imported as a module from the parent directory.

project/
│
└── my_multi/
    └── __init__.py
        multi.py
# content of multi.py
def multiply(a, b):
    return a * b

You can add your test directly under project folder or under sub-folder. I added it under tests. The test file name should either start or end with test. So that test runner will assume that the python file contains tests to be executed. Similarly the test case should also start with test_

project/
│
├── my_multi/
│   └── __init__.py
|       multi.py
|
└── tests/
    └── __init__.py
        test_multi.py
# content of test_multi.py
import pytest

from my_multi import multiply

def test_multiplication():
    assert (multiply(2, 3) == 6)

Run test

You can invoke testing through the Python interpreter from the command line

python -m pytest tests/

    Or simply

pytest tests/

pytest dumps pass or fail results

============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-3.5.1, py-1.5.3, pluggy-0.6.0
rootdir: project, inifile:
plugins: remotedata-0.2.1, openfiles-0.3.0, doctestplus-0.1.3, arraydiff-0.2
collected 1 items

tests\test_multi.py ...                                           [100%]

========================== 1 passed in 0.02 seconds ===========================

I purposefully changed the multiply function to divide a by b. So now our test failed showing the call stack.

============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-3.5.1, py-1.5.3, pluggy-0.6.0
rootdir: project, inifile:
plugins: remotedata-0.2.1, openfiles-0.3.0, doctestplus-0.1.3, arraydiff-0.2
collected 1 items

tests\test_multi.py F..                                           [100%]
================================== FAILURES ===================================
_____________________________ test_multiplication _____________________________

    def test_multiplication():
>       assert (multiply(2, 3) == 6)
E       assert 0 == 6
E        +  where 0 = multiply(2, 3)

tests\test_multi.py:14: AssertionError
===================== 1 failed in 0.02 seconds ======================

Other options to run tests

You can use -v for verbose result, like status of each test ran.

pytest -v /path/to/test_file.py

If you have mulptiple tests in a file and only wants to run specific test out them, you can do as below.

pytest -v /path/to/test_file.py::test_name

References