A test runner registers checks, executes them and classifies their observed outcomes.
Build a small runner that keeps passes, assertion failures and unexpected errors distinct, then test the runner itself.
The tests themselves need a reliable runner: every selected case must receive its inputs, run once and appear in an honest outcome bucket. Assemble the small mechanisms from this section, then challenge the runner with known failures, missing fixtures and shared-state leaks.

Five lessons, five pieces: a table of parser cases, a fixture factory, a fake source, a round-trip property, a failure narrower. Each works on its own. What Quarry doesn't have yet is the thing that holds them together — one command that finds every test, gives each what it needs, runs it in isolation, and tells you in a few lines whether Quarry still works.
Register tests explicitly instead of scanning and guessing
Lesson 1 collected by scanning globals() for names starting test — that
works, but it guesses. A registry is explicit: a decorator adds each
test to a list the moment it's defined, and the rig runs exactly that list,
in definition order.
Both decorators hand back the same function they were given — they only record it. That matters when decorators stack:
REGISTRY = []
def test(fn):
REGISTRY.append(fn)
return fn
def parametrize(*cases):
def attach(fn):
fn.cases = cases
return fn
return attach
@parametrize(("WARN",), ("INFO",))
@test
def test_level(level):
pass
print(len(getattr(REGISTRY[0], "cases", ())))Preparing this topic’s interactive investigation…
Classify an outcome — and get the except order right
pytest's pytest.skip("reason") and unittest's SkipTest are both an
exception the runner recognises, which puts a trap in the classifier:
import unittest
def outcome(fn):
try:
fn()
except AssertionError:
return "failed"
except Exception:
return "error"
except unittest.SkipTest:
return "skipped"
return "passed"
def test_later():
raise unittest.SkipTest("the vault encoder isn't merged yet")
print(outcome(test_later))Put the specific classes first — SkipTest, then AssertionError, then
Exception — and each lands in its own bucket.
Expand a parametrised test without the late-binding trap
A parametrised test becomes several runnable jobs, one per row. The obvious way to build them has a famous bug:
Each job should check its own row, giving [True, True, False]. Instead every job checks the last row. Fix how the jobs are built.
A function body looks up outside names when it runs. All three lambdas
share one case variable, and by the time they run the loop is over. A
default argument is evaluated when the function is defined, so
case=case freezes each row into its own job. The rig avoids the problem
entirely by running each row immediately as it loops, rather than
collecting jobs for later.
Here's the heart of the rig for one test ID: split the parameters between row and fixtures, build fresh fixture values, run, classify.
zip(names, row) fills the first parameters from the row; every name left
over must be a fixture. If one isn't registered, looking it up raises
KeyError — before the test runs — and that is an error, not a
failure: the test never got to check anything.
Prove the runner before trusting the suite
The test rig is itself a program with a contract. Test it with deliberately tiny functions whose outcomes you already know: one passes, one raises AssertionError, one raises an unexpected exception, one skips, and one asks for a missing fixture. Check both bucket membership and the total number of expanded jobs.
A useful accounting invariant is:
collected jobs = passed + failed + errors + skipped
Count expanded parametrized cases, not just decorated functions. Then filter by a keyword that matches nothing. An empty report must be visibly different from a report in which all intended tests passed. This is the same “attempted versus completed work” distinction the vault used for committed rows.
The rig's isolation is control-flow isolation: one exception does not prevent later tests from being called. It is not process isolation. Tests can still mutate globals, logger handlers, sys.path, module caches or shared fixture internals. A fresh namespace for exec does not isolate those process-wide objects. Restore temporary changes and run order-sensitive probes deliberately.
Transfer with an adversarial test: make the first test append to a shared list, and the second expect the original list. Run each alone, then together, then in reverse order. Repair ownership of that state rather than changing the order until the suite turns green. Finally, mutate one real parser rule and confirm that a correctly collected test reports the broken behavior.
The rig intentionally uses simpler collection, parameter IDs and outcome categories than pytest. It does not provide pytest's plugin model, fixture dependency graph, process isolation or full command-line expression syntax. Building the mechanism helps you read a real runner's report; it does not mean you should replace a maintained runner in a production project.
References: pytest collection practices and exit codes.
Report flakiness honestly, and let the exit code be the verdict
A flaky test passes and fails on the same code — usually timing,
ordering or the network. The tempting fix is to rerun failures until they
pass — look what that does: with r tries, under an illustrative independent 50%-failure model, a test
shows green with probability 1 − 0.5ʳ, about 94% at four tries. The suite looks
healthy while the bug behind the flakiness ships. Rerun to detect
flakiness, then report it; never rerun to hide it — a test that both
passed and failed across runs of the same code is flagged as flaky, and
that list is worth more than the green tick.
Your numbers will differ run to run — timing is the one measurement in a test report that's never exactly repeatable, which is why the rig reports it but never asserts on it. When pytest finishes it exits with a number CI reads: 0 if everything collected passed (skips are fine), 1 if anything failed or errored, 5 if nothing was collected at all — a suite that runs nothing proves nothing.
Quick practice
def exit_code(outcomes):
if not outcomes:
return 5
if any(o in ("failed", "error") for o in outcomes):
return 1
return 0
runs = [["passed"], ["passed", "error"], []]
print([exit_code(r) for r in runs])seen = {}
for run in [{"a": "passed", "b": "failed"}, {"a": "failed", "b": "failed"}]:
for test_id, outcome in run.items():
seen.setdefault(test_id, set()).add(outcome)
flaky = sorted(t for t, outs in seen.items() if "passed" in outs and outs & {"failed", "error"})
print(flaky)🎯 Your turn
The starter holds three parts. Quarry's code under test: parse_line
and error_rate. Quarry's suite: ten tests using @test, @fixture
and @parametrize, plus a skip(reason) helper. And the rig — which is
yours.
Make the decorators real (test registers, fixture registers by name,
parametrize attaches its rows), then write suite_report(keyword):
- Expand every registered test into IDs: a plain test's ID is its name; a
parametrised one gives
name[v1-v2]per row. - Keep only IDs containing
keyword(an empty keyword keeps everything). - Run each: fill parameters from the row, the rest from fresh fixtures. A
missing or crashing fixture is an error;
SkipTestis skipped;AssertionErroris failed; any other exception is an error. - Return
{"passed": [...], "failed": [...], "errors": [...], "skipped": [...]}, IDs in registration order.
suite_report("rate")
# → {"passed": ["test_rate_of_batch", "test_rate_counts_only_errors"],
# "failed": [], "errors": ["test_rate_of_empty_batch"], "skipped": []}Then read the full report with suite_report(""). Two of Quarry's own bugs
are in it — an empty batch that crashes and a newline the parser keeps —
and a test that asks for a fixture nobody wrote. Those are real findings.
That is what the rig is for.
Practice — level up
0/6 solvedSolve each in its own editor — Run to try, Check to grade. Stuck? Reveal a Hint, or the full Solution + walkthrough.
Write outcome_for(exc_name) mapping what a test raised to its bucket: None (nothing raised) → "passed", "AssertionError" → "failed", "SkipTest" → "skipped", anything else → "error".
pytest's -k takes an expression. Support a simple form: terms joined by and, each optionally prefixed not . An ID is selected when every term holds — a plain term must appear in the ID, a not term must not. Matching ignores case. An empty keyword selects everything. Write select(ids, keyword) returning the selected IDs in order.
timings is a list of [test_id, seconds]. Write slowest(timings, n) returning the IDs of the n slowest tests, slowest first; tests with equal times are ordered by ID.
runs is a list of runs of the same code, each a dict from test ID to outcome ("passed", "failed", "error", "skipped"). Write classify_runs(runs) returning {"flaky": [...], "broken": [...]}: flaky tests passed in at least one run and failed or errored in another; broken tests failed or errored in every run they appear in. Both lists sorted.
Write ci_verdict(runs) for a list of runs (each a dict ID → outcome; the last run is the one CI reports). Return {"exit_code": ..., "flaky": [...], "verdict": ...}. exit_code follows pytest for the last run: 5 if it has no tests, 1 if any test failed or errored, else 0. flaky is the sorted list of IDs that passed in some run and failed or errored in another. verdict is "red" if the exit code is 5, or if the last run has a failure or error on a test that isn't flaky; otherwise "flaky" if the flaky list is non-empty; otherwise "green".
A teammate proposes making Quarry's CI rerun every failing test up to five times and pass if any attempt passes. Argue for or against, and say what you would do instead.