CoursePython from Scratch to Intermediate · Module 10: Intermediate Python · part 70 of 75
Part 70 · Module 10: Intermediate Python

Lesson 4: Testing the shelf's rules with pytest

16 min read·14 Oct 2026
Module 10 · Lesson 4 · About 50 minutes · Needs: Module 6 (functions and assert), Module 7 Lessons 1, 2 and 4 (the booknook package, dates.py, fees.py, installing pytest), Module 8 Lesson 1 (reading an error message)

The situation

Amira's request for this module is short: "Make sure it never breaks." The shelf's rules now live in booknook/fees.py and booknook/dates.py, and more people will touch them. One careless edit, a 25 where a 20 should be, and Ben is charged the wrong amount without anyone noticing.

This lesson writes tests: small programs that check the package against answers you already know. You will follow Ben's loan L13, The War of the Worlds, 9 days late on Wednesday 14 October 2026, which must cost 180 pence every time the tests run.

ONE CARELESS EDIT, CAUGHT IN SECONDS FEE_PER_DAY = 25 a careless edit python -m pytest runs every test 1 failed Ben: 225, not 180
A wrong edit (red, failure) meets the tests (blue, your code's normal path), and pytest reports at once that Ben's 9 days now cost 225 pence instead of 180.

The rest of this part opens with an account

Signing up is free and takes one email. Nothing to pay, and nothing renews.