core Estimated learning time: 5 h

2.15 Testing with pytest

You can change code confidently because tests catch the break.

Before:00. Orientation & SetupUnlocks:03. Data Handling & Analysis04. Classical AI — Agents, Search & Knowledge Representation

Tests are what make change safe: pytest keeps them cheap — plain functions, plain asserts, fixtures for setup — and parametrisation covers edge cases in bulk. It sits near the end of the Python module so everything built can now be guarded. The metric scepticism is the real lesson: coverage percentages prove lines ran, not that behaviour is right, and one thoughtful test of a boundary beats ten that restate the happy path.

Work through these

  • Test discovery, assertions, fixtures

    How the test runner finds tests, how assertions report failures, and the mechanism for shared setup. These three are enough to write a useful test suite.

  • Parametrize and test data

    Running the same test over many inputs without repeating the test. It is the feature that makes thorough testing affordable.

  • Mocking and monkeypatching

    Replacing a dependency with a controllable stand-in so a test does not need a network or a database. Overusing it produces tests that pass while the program is broken.

  • Coverage and what it does not prove

    Coverage tells you which lines ran, which is not the same as which behaviours were checked. Knowing what the number does not prove keeps it honest.

Sign in to keep your progress.

Free resources

Links last checked 29 Aug 2026.

Stuck here?

Ask a mentor. A real person answers, and they can see exactly which topic you're on. Usually within a couple of working days.

Checking your session…

Topics shown in module order.