BEGINNERCHAPTER 01

Why we test: what a test can and cannot prove

A green suite says these inputs gave these outputs on this code. It does not say the code is right, and the gap is where the incidents live

40 MINSOFTWARE TESTING AND QUALITYUPDATED 2026-11-23
PREDICT

Dalila is on Ojaline's checkout team. She runs the unit suite for the pricing code. It prints 10 passed, 0 failed. What can she conclude?

You test software for a plain reason: you will change it, and you need to find out quickly when you have broken something. That is all a test does. It does not certify that code is correct, and teams that treat it as certification stop looking.

This chapter is about what a passing test earns you, what it does not, and why you write tests anyway. In the lab you run the starter suite for Ojaline’s checkout service, make a bug that every test misses, and write down exactly what the suite proves.

What a test is

A test is a program that runs another program with chosen inputs and compares the result with an answer you wrote in advance. In pytest that is a function whose name starts with test_ and a line that says assert. Here is one from the checkout lab, tests/unit/test_pricing.py:

def test_big_order_gets_the_top_discount():
    assert discount_bp(8_000_000) == 1000

discount_bp takes a subtotal in kobo and returns a discount in basis points (hundredths of a percent). The test says: for a subtotal of ₦80,000 the discount is 1000 basis points, which is 10%. If the function returns anything else, assert raises, pytest reports a failure and the run exits non-zero. If it returns 1000, the test passes and prints nothing.

Three things are baked into that one test, and each is a limit.

  • One input. The test tried 8,000,000 kobo. It did not try 7,999,999, 5,000,000 or 50,000,000.
  • One answer you chose. The 1000 comes from your reading of the pricing rule. If you misread the rule, the test passes against the wrong behaviour and fails against the right one.
  • One environment. The test ran on one machine, with one Python (3.9.6 in this lab), one set of package versions and a clean in-memory state. Production has none of those guarantees.

What a passing suite proves

A suite is many such examples. When all of them pass you know a specific, narrow thing, and it helps to say it in full:

A green run proves A green run does not prove
For each test’s inputs, the code gave that test’s expected output The code works for any input no test used
The code still behaves as it did when the tests were written, for those inputs The expected outputs match what the business wants
No test raised an unexpected exception Every line was checked: only that it ran, if even that
It all happened on this machine, today It will happen on a customer’s phone, a slower disk or at 23:59 Lagos time
The tests that exist still pass The tests that are missing would have passed

Read the right-hand column slowly. Every item in it is a way a team has shipped a bug through a green build.

The idea is old. In 1970 Edsger Dijkstra wrote, in his Notes on Structured Programming, that program testing can be used to show the presence of bugs, but never to show their absence. The reason is arithmetic. A function that takes two 32-bit integers has about 1.8 × 1019 input pairs. A suite that runs a million of them per second would need hundreds of thousands of years to try them all. You never test the input space. You sample it, and the whole craft of the course is choosing the samples so that bugs are likely to land on them.

A failing test is different. One failure proves a bug exists, in the code or in the test, and that asymmetry is why teams run tests as a bug detector and not as a proof.

What a suite is for, then

If a green run proves so little, why write tests? Because the narrow thing it proves is worth a great deal when you change code. Four jobs, in the order most teams feel them.

A bug detector you run all day. Martin Fowler calls code with a thorough automated suite self-testing code: you run one command and it tells you whether you broke something you already knew about. It does not find every bug. It finds the regressions, the bugs that come back because someone touched code that used to work, and those are most of the bugs a mature team ships. A regression found by a test costs minutes. The same one found by a customer costs a support ticket, a hotfix and a post-incident review.

Confidence to change code. The Google testing chapter makes the point with a story about its web server: when the team made tests required and automated, emergency pushes dropped sharply. People stopped being afraid to refactor. Without tests every change is a bet. With them, a refactor that keeps the suite green is a bet with known odds.

Early design feedback. Code that is painful to test is usually painful for a reason: it reads the clock directly, talks to the network from inside a calculation, or does five things in one function. Writing the test first, or even thinking about it, exposes that coupling while it is cheap to fix. Ojaline’s checkout/pricing.py is pure: no I/O and no clock reads, so its tests are three lines each. checkout/orders.py calls a database and a payment provider, and its tests need more set-up. The difference is design, and the tests showed it.

Examples that cannot go stale. A comment saying “orders of ₦10,000 or more get 5% off” can become false without anyone noticing. A test that asserts it fails the day it becomes false. A good suite is the most accurate documentation of behaviour a codebase has, provided the tests say what they mean.

None of these jobs needs the suite to be complete. They need it to be fast, trusted and aimed at the code you are about to change.

What tests cost

Tests are code, so they have the costs of code. You write them, review them, run them and maintain them. A slow suite gets run less often. A flaky suite, one that fails without a code change, gets ignored, and then a real failure is ignored with it. A test that checks the wrong thing fails when you improve the code, which taxes every refactor. Later chapters take each of these in turn: the shape of a suite, flaky tests, what not to test.

The cost matters for how you read this chapter’s lesson. You do not need more tests. You need tests that would fail if the code were wrong. The next section shows how to have plenty of one and none of the other.

Coverage tells you what ran, not what was checked

The usual way to ask “how good is our suite” is line coverage: the percentage of lines that ran during the tests. It is a cheap, honest measure of one thing, which lines no test reached. It cannot tell you whether any test checked what those lines computed.

The simulation below runs a pricing function under a suite with no useful assertions. Coverage reaches 100% and a planted bug stays in. Before you start, decide what you think will happen if you flip a comparison in the code.

▶ SIMULATION
SUITE
calls every path, asserts fee >= 0
line coverage: —
CODE UNDER TEST
if (subtotal >= 5000) return 0;
let fee = 499;
if (member) fee -= 250;
return Math.max(fee, 0);
MUTANTS (build result)
—
last—
✓ the build is red whenever the code is wrong
SCHEDULE
01[TEAM]suite: calls every path, asserts fee >= 0
02[CI]run the suite on the real code, with coverage
03[MUTATOR]flip >= to >
04[MUTATOR]off by one: 5000 to 4999
05[MUTATOR]base fee 499 to 500
06[MUTATOR]member discount adds instead of subtracts
07[MUTATOR]drop the member line
08[CI]report
Start of break: 100% coverage, no assertions scenario. 8 steps.
// Every line runs, so coverage says 100%. Nothing checks the answer.
test('shippingFee runs', () => {
  expect(shippingFee(1000, false)).toBeGreaterThanOrEqual(0);
  expect(shippingFee(1000, true)).toBeGreaterThanOrEqual(0);
  expect(shippingFee(9000, false)).toBeGreaterThanOrEqual(0);
});

Watch two numbers together: the coverage figure and the count of changes the suite notices. Coverage climbs as tests are added, and the detection count stays flat until a test asserts a result. That is the whole argument for treating coverage as a floor, not a score. A line nobody ran is certainly untested. A line that ran is only possibly tested. The lesson walks through it with mutants, which are the tool for asking the second question, and a later chapter here, coverage and mutation testing, does the same on Ojaline’s pricing code.

Where the gap is widest: boundaries

Bugs cluster at edges because edges are where a programmer has to decide something and a test writer has to choose something. Ojaline’s discount rule is “orders of ₦10,000 or more get 5% off”. That sentence is two lines of code:

for floor, bp in DISCOUNT_TIERS:
    if subtotal >= floor:
        return bp

The difference between >= and > is exactly one value, a subtotal of 1,000,000 kobo, and the code is wrong or right depending on that single number. Now look at the starter tests: one at 200,000 kobo (no discount) and one at 8,000,000 (top discount). Both are far from the edge. Either comparison passes both.

Tests that sit in the middle of a range are easy to write and feel like coverage. They are the least likely tests to catch a mistake. In the lab you will make this exact change and watch the suite stay green. Choosing cases that do land on edges is a skill with names (boundary values, equivalence classes) and the chapter Choosing test cases teaches it.

Tests are not the only control

Back to Knight Capital. It is tempting to read the story as “they needed more tests”, and the SEC order does note that the old Power Peg code was not retested after it was changed in 2005. But look at what else the order records. A technician deployed the new code to seven of eight servers and missed the eighth. No second person reviewed the deployment. There was no written procedure requiring one. Automated emails reading “Power Peg disabled” arrived before the market opened and nobody treated them as alerts. SMARS, the router, had no control comparing the orders leaving it with the orders that came in, and no way to halt itself on its own odd behaviour.

A unit test on the Power Peg function would have helped. It would not have caught the missed server, and a test suite cannot. Different failures need different controls: tests for logic, review and automation for deployment, monitoring for the behaviour you did not predict. This course covers testing in production, contracts and load alongside unit tests for that reason. The craft is knowing which control answers which question, and saying plainly which questions are still open.

Reading a test run

Your first job in the lab is to read a pytest run as evidence. A run like this one:

..........                                                               [100%]
10 passed in 0.12s

says: ten test functions were collected, each ran to the end without raising, and it took 0.12 seconds. Each dot is one test. Run with -v and every test gets a line with its name, which is the list of the ten facts you now know. That is the whole of what the suite claims, and it is short enough to read in one sitting. Reading it, and noticing what is not on the list, is a habit worth building on day one.

LAB Read the suite, then break a rule it does not guard ~25 MIN
YOU NEEDPython 3.9 or laterlabs/testing from the atlas-labs repository, with its .venv set up as in the READMEno Docker, no network after install

You work in Ojaline’s checkout service. Everything here runs from the labs/testing folder. The lab prices carts in integer kobo: ₦10,000 is 1_000_000.

1. Set up the lab and run the unit tests.

cd labs/testing
python3 -m venv .venv
.venv/bin/pip install -r requirements.txt
.venv/bin/pytest tests/unit -q

(If .venv already exists, skip the first two commands after cd.)

.......... and then 10 passed in 0.1s or close to it. Ten dots, ten tests: two in test_money.py and eight in test_pricing.py. The suite uses pytest-randomly, so the order changes between runs, and these tests do not care.

2. Read the output line by line. Run it again with test names and a fixed order.

.venv/bin/pytest tests/unit -v -p no:randomly
Ten lines, each ending in PASSED, for example tests/unit/test_pricing.py::test_big_order_gets_the_top_discount PASSED. This list is everything the suite claims. Read all ten names and ask of each one: which input does it use?

3. Run the whole check. This runs the full suite and then a smoke test that starts the checkout API and the fake payment provider, places an order and checks the response against the provider’s contract.

./scripts/check.sh

The smoke test starts servers on ports 8094 (checkout) and 8095 (provider). If another process holds them, free them first, or run SMOKE_API_PORT=8194 SMOKE_PROVIDER_PORT=8195 ./scripts/check.sh.

23 passed, 1 skipped, 1 deselected from pytest (the skipped test is the browser test, which needs Chromium; the deselected one is a deliberately flaky test kept out of the default run), then nine ok lines from the smoke test and ALL CHECKS PASSED. Nothing here is red. Keep that in mind for the next two steps.

4. Find the rule and the tests that guard it. Open checkout/pricing.py and find discount_bp. Then open tests/unit/test_pricing.py and find the two discount tests. Probe the boundary yourself. Create probe.py in labs/testing:

from checkout.pricing import discount_bp

for kobo in (999_999, 1_000_000, 1_000_001):
    print(kobo, discount_bp(kobo))
.venv/bin/python probe.py
999999 0, 1000000 500, 1000001 500. An order of exactly ₦10,000 gets 5% (500 basis points), one kobo less gets nothing. The starter tests use 200,000 and 8,000,000 kobo and never ask about 1,000,000.

5. Introduce the bug. In checkout/pricing.py, change the comparison in discount_bp from subtotal >= floor to subtotal > floor. Do not touch the tests. Rerun the unit tests, the probe and the whole suite.

.venv/bin/pytest tests/unit -q
.venv/bin/python probe.py
.venv/bin/pytest -q
Still 10 passed. The probe now prints 999999 0, 1000000 0, 1000001 500: a basket of exactly ₦10,000 loses its discount and is priced like ₦9,999.99. The whole suite still prints 23 passed, 1 skipped, 1 deselected. You have a pricing bug live in the checkout service, with every test green. This is Ojaline’s Thursday. The change also moved the ₦50,000 tier, since both tiers use the same comparison, and no test noticed that either.

6. Write the test that was missing. Create tests/unit/test_boundary_probe.py:

from checkout.pricing import discount_bp


def test_ten_thousand_naira_gets_five_percent():
    assert discount_bp(1_000_000) == 500
.venv/bin/pytest tests/unit -q
One failure: assert 0 == 500, with where 0 = discount_bp(1000000), and 1 failed, 10 passed. One example on the edge was enough. Its failure message names the input and both values, which is what a good failing test gives you.

7. Revert everything. Change > back to >=, delete tests/unit/test_boundary_probe.py and probe.py, and run .venv/bin/pytest tests/unit -q once more. It prints 10 passed.

8. Write down the claim. On paper or in a file outside the repo, finish these two sentences for the starter suite: “This suite proves that…” and “This suite does not prove that…”. Give at least three items in the second list, one of them about a value, one about the clock and one about another service.

A good answer to the first sentence is narrow: for these ten inputs, the money, pricing and discount functions return these ten outputs. The second list should include the tier boundaries at ₦10,000 and ₦50,000, the delivery band edges at 5, 15 and 40 km, a promo on its first or last day, and anything the browser, the database or the payment provider does. The README’s notes for chapter authors list the starter suite’s weak spots on purpose; compare your list with the kinds of gaps you now know to look for.
Break it differently: in DELIVERY_BANDS, change (15, 300_000) to (15, 400_000) so the 6 to 15 km delivery fee rises by ₦1,000. Run .venv/bin/pytest -q. It stays green, because the only delivery tests use 3 km and 200 km, and the test named test_delivery_fee_mid_band_runs calls the function with 10 km and asserts nothing. A passing test with no assertion is worse than no test, since it shows up in the count. Revert the change, then fix that one test by giving it the answer.
DECIDE

Gebre leads Ojaline's checkout team. A change to the delivery fee bands passes CI, 23 tests green, and the author, Dalila, has to leave for a flight. The change affects every order in the 6 to 15 km band. Marketing wants it live before a weekend sale starting at midnight. Nobody has run an order through the new bands by hand. What should Gebre do?

READING
  1. PAPER
    Dijkstra, Notes on Structured Programming (EWD249, 1970) Edsger W. Dijkstra, 1970

    The source of the line that testing shows the presence of bugs, not their absence. It is a scanned typescript and a long read; take the statement on testing and its reasoning, and skip the programming-methodology sections until a later course. Read: Section 3, 'On the reliability of mechanisms'. The scan has no text layer, so you cannot search it; read that section by eye. The date is given here as 1970; sources vary between 1969 and 1972..

  2. DOC
    SEC, In the Matter of Knight Capital Americas LLC (Release No. 34-70694) U.S. Securities and Exchange Commission, 2013

    The regulator's own account of 1 August 2012: the deployment to seven of eight servers, the old code, the 45 minutes and the $460 million. Read paragraphs 1 to 21 for the facts; the legal sections after that are about market-access rules. Read: Introduction and Facts, paragraphs 1 to 21.

  3. BOOK
    Software Engineering at Google, chapter 11: Testing Overview Adam Bender, 2020

    The clearest short argument for why a team with a large codebase must test, including the Google Web Server story. Read 'Why Do We Write Tests?' and 'The Story of Google Web Server'; skim the later sections on test size, which a later chapter covers. Read: Why Do We Write Tests?; The Story of Google Web Server.

  4. POST
    Fowler, SelfTestingCode Martin Fowler, 2014

    Two minutes on what a test suite is for: a bug detector you can run in one command, and the confidence to change code that comes with it. It is the shortest statement of the case for tests.

  5. BOOK
    Khorikov, Unit Testing: Principles, Practices, and Patterns Vladimir Khorikov, 2020

    The modern book on what makes a test worth its cost, with a chapter on the goal of unit testing. Read the opening chapters on the goal and the cost of tests; later chapters on mocks and database tests belong to modules two and three of this course. Read: Part 1, chapter 1: The goal of unit testing.

  6. BOOK
    Beck, Test-Driven Development: By Example (publisher page) Kent Beck, 2002

    The book that made writing the test first a mainstream practice. You do not need it for this chapter; it is here so you know where the idea in the test-driven development chapter comes from. Read Part I, the money example, when you reach that chapter. Read: Part I: The Money Example.

All companies and people in these examples are fictional. Any resemblance to a real business or person is unintended.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: A first unit test →