A tiny exercise to practice TDD with pytest using three features:
@pytest.mark.parametrize
pytest.raises
pytest.approx
You'll implement a few simple math functions working purely with basic float
types, then run the tests and iterate until they pass.
Open src/simple_math/core.py
and complete these functions (they currently raise NotImplementedError
):
add(a: float, b: float) -> float
: Returna + b
.safe_divide(a: float, b: float) -> float
: Returna / b
; raiseValueError
ifb == 0.0
.average(xs: list[float]) -> float
: Return the arithmetic mean; raiseValueError
for an empty list.
Each function includes a docstring with examples to guide you.
-
From the project root, install dependencies:
poetry install
-
Run tests:
poetry run pytest -q
- Run tests first to see failures.
- Implement the smallest change to make one test pass.
- Re-run tests, refactor only after green.
- Repeat.
Happy testing! 🎯