Lesson 46 / 47
Testing Basics
Write small automated checks that confirm your code still works.
A first pytest test
pytest finds any function starting with test_ and runs it. Plain assert statements express what should be true.
# calc.py
def add(a, b):
return a + b
# test_calc.py
from calc import add
def test_add():
assert add(2, 3) == 5
assert add(-1, 1) == 0
Output:
$ pytest 1 passed in 0.01s
What makes a good test
Good tests are small, check one behaviour, and use clear names like test_add_negative_numbers so a failure tells you exactly what broke.