Pytest - simple passing test

We don't need much to test such code. Just the following things:
Specifically the assert function of Python expects to recived a True (or False) value. If it is True the code keeps running as if nothing has happened.

If it is False and exception is raised.

import mymath

def test_add():
    assert mymath.add(2, 2) == 4
We can run the tests in two different ways. The regular would be to type in pytest and the name of the test file. In some setups this might not work and then we can also run python -m pytest and the name of the test file.

python -m pytest

============================= test session starts ==============================
platform linux -- Python 3.8.6, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: /home/gabor/work/slides/python/examples/pytest/math
plugins: flake8-1.0.6, dash-1.17.0
collected 1 item .                                                         [100%]

============================== 1 passed in 0.00s ===============================

The top of the output shows some information about the environment, (version numbers, plugins) then "collected" tells us how many test-cases were found by pytest. Each test function is one test case.

Then we see the name of the test file and a single dot indicating that there was one test-case and it was successful.

After the test run we could also see the exit code of the program by typing in echo $? on Linux or Mac or echo %ERRORLEVEL% on Windows.

$ echo $?