Paul's Python CoursePPC

pytest

Not met yet

pytest finds files named test_*.py, runs every function in them whose name starts with test_, and reports each one as passed or failed, showing the values on both sides of a failed assert. Test functions take no arguments and need no code to call them. A test with no assert checks nothing, so it always passes.

Try it

main.py
# test_till.py
from till import price

def test_three_loaves():
    assert price("bread", 3) == 720

# then, in a terminal at home:  pytest

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Thinks pytest runs every function in a test file, not only the ones named test_.
  • Thinks a test function with no assert still checks something.

Where it comes up