Testing demo: pytest without classes


In the previous example we used a test-class to write our tests, but in reality in many cases we don't need the classes. We could just as well write plain test-functions as in this example.

Test-functions without a class around them are easier to write and understand and they are a lot simplert to graps. So unless you really need the features a class can provide I'd recommend you use functions only. After all our test code should be a lot more simple than our application code.


pip install pytest


examples/testing-demo/test_with_pytest.py
import mymath

def test_math():
    assert mymath.add(2, 2) == 4

=========================== test session starts ============================
platform linux -- Python 3.11.2, pytest-7.3.1, pluggy-1.0.0
rootdir: /home/gabor/work/slides/python/examples/testing-demo
plugins: anyio-3.6.2
collected 1 item

test_with_pytest.py .                                                [100%]

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



$ pytest test_with_pytest.py
$ echo $?
0


> pytest test_with_pytest.py
> echo %ERRORLEVEL%
0