Pytest - simple passing test
- Filename startes with test_
- A function that starts with test_
- Call the test function with some parameters and check if the results are as expected.
If it is False and exception is raised.
import mymath def test_add(): assert mymath.add(2, 2) == 4
pytest test_mymath.py python -m pytest test_mymath.py
============================= 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 test_mymath.py . [100%] ============================== 1 passed in 0.00s ===============================
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 $? 0
> echo %ERRORLEVEL% 0