Case File: The Discount Tests
A discount rule decides what customers pay, and nobody dares to touch it. Instead of changing the code, you write the unit tests: functions that use assert. The checks run your tests on the real function and then on copies that each carry one deliberate bug, called mutants. A good suite fails on every mutant. The rule is generated for you alone.
Sabi Logistics · Engineering 1
The Discount Tests
The pricing code has no tests, and the last change cost us money without anyone noticing
Ifeoma Lead developer · Phone call
Ifeoma. Last month somebody changed “ten or more” to “more than ten” in the discount rule, and for three weeks every customer who bought exactly ten items paid too much. No test noticed, because there are no tests. The function is called final_price(unit_price, qty, member), and the rule is written in the code.
I do not want you to change it. I want you to protect it. Write unit tests, and I will check them the way a careful engineer would: I will break the function in small ways, one at a time, and see whether your tests notice. A test suite that passes on correct code proves little. A test suite that fails on every broken copy proves a lot.
The brief
What you need to know first.
- Unit test. A function whose name starts with
test_and that calls the code and usesassert actual == expected. If the assertion is false, the test fails. Each test should check one thing and have a name that says what. - Work the expected value out by hand. Never copy the number that the function gives you: that only proves the function agrees with itself. Read the rule, work the answer out on paper, and write that number.
- Boundaries are where bugs live. If a rule changes at 10 items, test 9, 10 and 11. If a limit is a cap, test an order that is exactly at the cap and one that goes over it. Test each rule alone and then together, and test a price where the division leaves a remainder (the rule rounds down).
- Mutants. The checker runs your tests against the real function (all must pass) and then against copies with one deliberate bug each. A mutant that survives shows a gap in your tests. There are three groups: boundaries, arithmetic and logic. You are not told which mutant is which, so think about what could go wrong in every line.
- The lab gives you the function in the starter, with one test as an example. Add your own tests below it.
The lab gives you the data for this case, invented and generated for you alone. Today is 2026-11-09.
Preparing your data…
On a real system
The lab modelled the rules with a script. These are the commands that you would type on the real thing.
| You want to | Type | What you get |
|---|---|---|
| run the tests of a Python project | pytest -q | the number of tests that passed and failed |
| measure which lines the tests run | pytest --cov=app --cov-report=term-missing | coverage per file and the lines that no test reaches |
| check whether the tests would notice a bug (mutation testing) | mutmut run | changes the code in small ways and reports the mutants that survive |
Only run commands on systems that you own or have written permission to test.
Ifeoma: “Your tests caught every mutant. Now a developer says that tests are a waste of time because the code is simple. And another says that we need a hundred percent coverage. Who is right?”
🔒 The debrief unlocks when you close the case. Finish the lab above.
The debrief
Ifeoma: “So neither: tests are worth what they catch.”
What a senior engineer would do next. Run mutation testing with a tool (mutmut for Python, Stryker for JavaScript) so that the question “would my tests notice a bug?” has a number. Treat coverage as a way to find code that no test runs, never as proof that the tests are good: a test with no assertion gives full coverage and no protection. Put the tests in the build, so that a change that breaks a rule cannot be merged. When a bug is found in production, write the failing test first, then fix it.
The trap. Expected values copied from the output of the code. The tests pass, the bug stays, and everyone feels safe.
Write the test report
Write the note for the lead developer: the kinds of mistake that your tests can catch (boundaries, arithmetic, logic), which cases you chose and why, how you worked the expected values out (by hand from the rule, not from the output of the function), what a mutant that survives tells you, and why coverage alone does not show that tests are good.
🧪 Want to practise this on your own computer? See how to set up your lab →