Project: a suite that catches ten seeded bugs, measured by mutation score
Ten bugs are hidden in the checkout service. You are not told what they are. Write tests until every one is caught, then measure what the suite would still miss
You are Fanta. The lab is labs/testing, the checkout service you tested through modules one and two, and the eight chapters before this one in the module gave you the tools: property-based tests, time and concurrency, fault injection, performance and load, security checks, migrations, evals and money invariants. Nothing here is new. The difficulty is that the bugs come without labels.
Ten patches sit in labs/testing/seeded/. Each applies to the clean code under checkout/ and reverts cleanly. The tool is scripts/seed_bugs.py, and it counts a bug as caught when your suite fails with that patch applied. You will write tests until it reports ten of ten, then run the mutation tool on checkout/ and report a score you committed to before you looked.
The rule of the project: do not look
The patch files have names, and the names say what each bug is. seed_bugs.py list, status and try print them. So does seeded/README.md, which also holds the answer for each one. Reading any of them first turns the project into a lookup exercise and teaches you nothing about finding bugs. This chapter gives you a small wrapper that prints a number and a verdict, never a name, and a final section that says when to read the README.
Two more files stay closed until the end: seeded/README.md and the folder seeded/reference_tests/, the author’s answer key. If you open it early, the number you get at the end measures your memory, not your suite.
Where to look
You find a bug by asking what the code promises and checking the promise at its edges. The areas below are where the ten live. Nothing says how many bugs sit in each.
| Area | Files | A question to put to the code |
|---|---|---|
| Arithmetic on money | checkout/pricing.py, checkout/money.py |
At exactly which amounts does a rule change? What does a half kobo do? |
| Days and clocks | checkout/pricing.py, checkout/clock.py |
Whose midnight is it? The lab’s FakeClock lets you choose the instant. |
| Order flow | checkout/orders.py, checkout/inventory.py |
After a failure, is the world exactly as it was before? After a retry, is anything done twice? |
| The provider | checkout/payments_client.py, checkout/api.py |
Of the ways a call can fail, which does the caller see, and is each told correctly? |
| Visibility | checkout/search_index.py |
Who may see what? Try a caller who is not the owner. |
| The old scheduler | checkout/legacy.py |
What must always be true of the output, whatever the input? |
| Two at once | checkout/inventory.py, checkout/db.py |
What happens when two callers want the last unit at the same moment? |
Read the code in each file looking for the places where a rule is written as a comparison, a rounding, a conversion, an exception handler or a gap between a read and a write. A bug that someone seeded lives in one of those places, because that is where real ones live.
Hints: which chapter teaches the tool
A hint names a chapter, never a bug. If an area stays quiet after you have tested it, go back to the chapter and ask what kind of input it taught you to try.
| Area | Chapters to reread |
|---|---|
| Arithmetic on money | Choosing cases, Property-based testing, Testing money |
| Days and clocks | Testing time, randomness and concurrency |
| Order flow | Integration tests with a real database, Testing across services |
| The provider | Fakes, stubs and mocks, API and contract tests, Testing across services |
| Visibility | Security testing |
| The old scheduler | Project: put a legacy function under test, Property-based testing |
| Two at once | Testing time, randomness and concurrency |
For the measuring half, Coverage and mutation testing explains the score, and Running tests on every change explains why the whole suite has to stay fast. Testing data pipelines and migrations, Performance and load tests and Testing AI features matter less here: none of the ten lives in a migration, a load profile or a model.
How you measure progress
Two numbers, and they answer different questions.
Bugs caught. seed_bugs.py try applies each patch alone, runs pytest -q -x, reverts the patch, and prints CAUGHT if the suite failed and MISSED if it passed. It tells you whether the suite reacts to these ten changes. On the starter suite it catches 2 of 10, taking about 14 to 24 seconds. That is where you start.
Mutation score. The mutation tool invents bugs of its own, changing >= to > and + to - throughout the code, and counts how many your tests notice. It tells you whether the suite is thorough in general. The score is only as honest as its scope, and the lab’s mutmut config covers one file by default, checkout/pricing.py, where the starter suite kills 28 of 68 mutants. You widen the scope in the lab. Widening it before you have tests only measures the code your tests cannot reach: over the seven files that hold the bugs the starter suite kills 28 of 390 mutants, and 322 of the 390 report no tests. Treat a score as a statement about a stated scope, never about the product.
The two numbers can disagree, and the disagreement is informative. A suite can catch ten of ten because you wrote ten pointed tests, and still score 40% because everything the bugs did not touch is unguarded. The project asks for both.
Choose your target before you start
Write down a mutation target now. It has to be a number you can defend after reading the survivors: below 100%, because some mutants are equivalent (no input can tell them from the original), and above the level that lets a real survivor hide. Write the reason beside it. The pass condition for the whole project is three things: ten of ten caught, a score above your target, and the whole suite under two minutes.
Where it goes wrong
A test that is red for the wrong reason. A new test that fails on the clean code is a broken test, not a caught bug. Run the suite on clean code first, every time, and commit to the order: green clean, red buggy.
One lucky run. A race test that passes some of the time proves less than it seems. Run the patch for the race several times in a row. A test that forces both callers to start together, for instance with threading.Barrier, fails every time on the buggy code.
A patch that will not apply. The patches apply to the original files. If you edited checkout/ earlier, in the legacy project or elsewhere, apply stops with cannot apply ... is another bug or an edit in the way?. Work in a fresh copy of the lab with your tests added to it.
Property tests under the mutation tool. The lab’s mutmut run executes everything in tests/unit/ first to collect statistics. A Hypothesis test there crashes that step: the run prints failed to collect stats after a traceback from Hypothesis’ internals, and no mutant is tried (the coverage and mutation chapter records the same quirk). Keep property tests in tests/properties/. pytest still runs them in the full suite.
Tests that mock the thing under test. A test that stubs the exact function a bug lives in cannot catch the bug. Stub the provider, never the code you are testing.
Work in a throwaway copy, so a patch left applied by accident does no harm. From the folder that holds labs:
rsync -a --exclude .venv --exclude .playwright-browsers --exclude data --exclude mutants --exclude .hypothesis labs/testing/ scratch-testing/
ln -s "$PWD/labs/testing/.venv" scratch-testing/.venv
cd scratch-testing
export CHECKOUT_DB="$PWD/scratch.sqlite"The copy leaves out the virtual environment (large, and its scripts hold absolute paths), so ln -s points the copy at the one you already have. Everything below runs from scratch-testing.
Start from the starter state. The copy carries whatever sits in labs/testing. The chapters before this one each end with a step that puts the lab back; if you skipped one, test files and edits to checkout/ from that chapter come along, step 2’s counts will be higher than the ones printed, and apply can stop on a file you edited. Remove those files, or copy from a fresh clone of the repository, before you go on.
1. Write the blind wrapper. Create scripts/blind_try.sh. It turns patch numbers into names, runs seed_bugs.py try, throws away every line that holds a name and rewrites the verdicts as CAUGHT bug 03.
#!/bin/sh
# usage: scripts/blind_try.sh [--tests PATH] [NN ...] (no numbers means all ten)
cd "$(dirname "$0")/.." || exit 1
tests=tests/
names=""
while [ $# -gt 0 ]; do
case "$1" in
--tests) tests=$2; shift 2 ;;
*) names="$names $(basename seeded/$1-*.patch .patch)"; shift ;;
esac
done
.venv/bin/python scripts/seed_bugs.py try --tests "$tests" $names \
| grep -E '^(CAUGHT|MISSED)|caught by' \
| sed -E 's/^(CAUGHT|MISSED) +([0-9]+)-[a-z0-9-]+/\1 bug \2/'chmod +x scripts/blind_try.sh
.venv/bin/python scripts/seed_bugs.py status | wc -l
scripts/blind_try.sh10 and nothing else you need: do not run it without the wc, because without it the names print. The wrapper prints CAUGHT bug 04, CAUGHT bug 09, MISSED for the other eight and 2 of 10 caught by tests/. That is the starting point, in about 14 to 24 seconds on the lab’s starter suite.2. Confirm the starting state is clean. Before any new test, make sure no patch is left applied and the suite is green.
.venv/bin/python scripts/seed_bugs.py status | grep -c clean
.venv/bin/pytest -q10 clean patches, and 23 passed, 1 skipped, 1 deselected (the skip is the browser test without Chromium, the deselected one is the quarantined flaky test).3. Write tests, one area at a time. Pick an area from the table. Write the test as if the code were yours and you were asking whether its rule holds at the edge. Run it on the clean code, then ask the wrapper about the whole set:
.venv/bin/pytest -q -p no:randomly
scripts/blind_try.shDo this per area, not per bug. Put pure-rule tests in tests/unit/, tests that need a database file in tests/integration/ (they use the db, clock and payments fixtures from tests/conftest.py, and StubPayments is importable from there) and Hypothesis tests in tests/properties/. For a provider failure that does not need a process, httpx.MockTransport handed to PaymentsClient(transport=...) lets a test raise any error you choose.
N of 10 caught by tests/ only goes up. I reached 10 of 10 with 32 new test cases in four files, and the full suite then ran in about 0.9 seconds: 55 passed, 1 skipped, 1 deselected.4. When you are stuck on one. Ask the wrapper about one number at a time: scripts/blind_try.sh 05 prints one verdict. For a miss you cannot explain, apply the patch, look at what changed in checkout/ in your editor, and never open the patch file:
.venv/bin/python scripts/seed_bugs.py apply "$(basename seeded/05-*.patch .patch)" > /dev/null
# read the changed lines in checkout/, then:
.venv/bin/python scripts/seed_bugs.py revert all > /dev/nullThat is one way to look; the cleaner way is to reason from the table’s questions first, because a bug you find by reasoning is a bug a real suite would have found. Whichever you use, finish with revert all, then run status | grep -c clean and expect 10.
10 after revert all. If a bug is only caught some of the time, run scripts/blind_try.sh NN six times in a row; a race you have forced with a barrier shows CAUGHT six times.5. Widen the mutation scope and say your target. Open pyproject.toml. Under [tool.mutmut] change paths_to_mutate to the files your unit tests exercise, and remove from also_copy any file you now mutate (a file in both lists is copied clean over its mutated version). The numbers below came from this configuration:
[tool.mutmut]
paths_to_mutate = ["checkout/pricing.py", "checkout/money.py", "checkout/legacy.py"]
tests_dir = ["tests/unit/"]
also_copy = ["checkout/__init__.py", "checkout/clock.py", "checkout/db.py", "checkout/search_index.py", "checkout/payments_client.py"]Write your target in a file, MUTATION-TARGET.md, with your reason, before you run it.
rm -rf mutants
.venv/bin/python scripts/mutate.py run
.venv/bin/python scripts/mutate.py results | grep -c survivedorders.py, inventory.py, search_index.py and payments_client.py are out of this scope on purpose. Integration tests cover the first three and mutmut does not run them here, so their mutants would report no tests and drag the score towards 0 without saying anything about your suite. Adding payments_client.py to the list made the statistics run abort with KeyError: -6 in this lab, so it stays a copied file.6. Read every survivor. For each survivor, .venv/bin/python scripts/mutate.py show <name> prints the diff. Classify it: a missing test (write it), or an equivalent mutant (write why no input can tell it apart). Re-run until the survivors that remain are all ones you can justify, then compare the score to your target.
7. Run the final gate. Time the whole suite, ask for all ten, and confirm the tree is clean:
time .venv/bin/pytest -q
scripts/blind_try.sh | tail -1
.venv/bin/python scripts/seed_bugs.py status | grep -c clean10 of 10 caught by tests/, and 10 clean. If all three hold and the mutation score is above your target, you have passed.8. Now read the README. Only now open seeded/README.md. For each bug, compare its description with the test you wrote that caught it. Then run your suite against the author’s key for comparison, which prints the same verdicts:
.venv/bin/python scripts/seed_bugs.py try --tests seeded/reference_tests | tail -110 of 10 caught by seeded/reference_tests. The README also lists, per bug, how a good suite catches it. Where yours differs from the author’s, ask which of the two would still catch a variant of the same bug.Break it. Take a bug you caught with a pointed example test and ask whether a different bug of the same kind would also be caught. Change a constant in the clean code by one step at another boundary, for instance a different delivery band edge or the last day of the promo, and run your suite. If it stays green, the test caught one value, not the rule. Fix that by testing the boundary table, not the single point. Revert your edit afterwards.
What the project proves, and what it does not
Ten of ten says your suite catches ten changes. That is a lower bound on its usefulness and nothing like a guarantee. The mutation score estimates the rest, and it is itself a statement about a stated scope. Between them you know which kinds of mistake the suite reacts to and which it does not, and you can say how you know. That is what Seydou’s question needed, and it is the answer you give the next time a patch arrives with the line “tests pass”.
The module’s other chapters (property-based testing, time and concurrency, tests across services, performance and load, security testing, data pipelines and migrations, AI features and money) each taught a way to find a class of bug that example tests miss. The next module, Quality across an organisation, asks how a team keeps this property as the code changes: a test strategy for a product, CI at scale, testing in production and the metrics that say whether any of it works.
All companies and people in these examples are fictional. Any resemblance to a real business or person is unintended.