Unit Testing#
Why test?
Quality: verify correct behavior across inputs and edge cases
Safe refactoring: catch regressions immediately
Documentation: tests show exactly how a function is supposed to behave
Getting started with pytest#
pip install pytest
# src/mypkgs/calculate.py
def calculate_area(length, width):
"""Calculate the area of a rectangle."""
return length * width
# tests/test_calculate.py
from mypkgs.calculate import calculate_area
def test_calculate_area():
assert calculate_area(2, 3) == 6
assert calculate_area(0, 5) == 0
assert calculate_area(-1, 5) == -5
python -m pytest