Programming foundations

Stage 6 · Lesson 6 of 8

Tests with pytest

Build and ship your first projects

is a Python testing tool. It finds files named test_*.py and runs every whose name starts with test_. Tests use plain assert statements, such as assert monthly_total(entries) == 42.5.

Run pytest from the project root, and it reports each failure with the values involved. Test the core logic: functions that take values and return values. Cover such as an empty list, one entry and invalid input.

Use pytest.raises(ValueError) to check that bad input is rejected. @pytest.mark.parametrize runs one test over many inputs, and the tmp_path gives you a temporary folder for testing file code. Compare floats with pytest.approx, since 0.1 + 0.2 is not exactly 0.3.

A question to check yourself, an AI practice session and 2 cheat sheet entries come when you add the roadmap to your account. Add it

Resources for this stage

Start with· follow one of these

Keep handy

  • Free APIs that give a small project real data to work with

Also worth your time

Mark this stage as not done?