100% coverage, bug intact

Every line ran. Nothing checked the answer.

L217 MINTESTING
PREDICT

A suite calls shippingFee on every branch and asserts only that the result is not negative. Line coverage is 100%. You flip a comparison in the code from >= to >. What does the build do?

Line coverage records which lines executed. A test that calls the function and asserts nothing useful executes them all. Coverage rises, the dashboard is happy, and the suite cannot fail.

Mutation testing asks the other question: would the suite notice if the code were wrong? A tool makes a mutant, a copy with one small change (flip a comparison, shift a constant by one, delete a line), and runs your tests. A failing test kills the mutant. A passing suite lets it survive, and the survivor points at an assertion nobody wrote.

▶ 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 the invariant strip: the build should be red whenever the code is wrong. In the first scenario the line coverage reads 100% and every mutant survives. In the strong scenario they die, and the last mutant is the odd one out.

BREAK IT NOT YET

A suite with exact assertions on three ordinary cases is green on the real code. Insert one mutant so the build stays green on wrong code.

GOAL: a wrong version of the code that passes the suite

FIX IT NOT YET

Write testShippingFee(shippingFee, assertEqual). Rules: orders of 5000 or more ship free; otherwise the fee is 499, or 249 for members. assertEqual(actual, expected) throws when they differ. Your test must pass on the real code and fail on every wrong version: flipped comparison, off-by-one threshold, wrong free value, wrong base fee, member discount in the wrong direction, member line dropped. One further version looks different but behaves the same; you cannot and need not catch it.

The mutation score is mutants killed divided by mutants tried. Read it with three cautions. First, equivalent mutants: changing Math.max(fee, 0) to Math.max(fee, -1) changes the text but not the result, because the fee is never negative. No test can kill it, so a good score stops short of 100% and someone has to judge the survivors. Second, cost: every mutant needs a test run, which is why teams mutate only changed code, as the Google system does. Third, the score covers only the mutants the tool tries. It is a way to find missing assertions, not a number to chase.

Mental model:

Coverage says the code ran; it never says anyone looked at the result. Ask the harder question: if the code were wrong in one small way, would a test fail? A surviving mutant is a missing assertion, and an equivalent mutant is the survivor you may leave alone.

IN PRODUCTION

Coverage gates in CI that reward calling code over checking it; code reviews that ask for tests and accept any test. Tools such as Stryker (JavaScript), PIT (Java) and mutmut (Python) generate mutants; run them on changed code, not the whole repository, because of the cost.

IN INTERVIEWS

“Your suite has 95% coverage and a bug still shipped. How do you tell whether the suite is any good?” Explain that coverage measures execution, describe mutation testing and the surviving mutant, mention equivalent mutants and the run-time cost, and say you would use it to find weak assertions on risky code, not as a target.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: BOUNDARY VALUES →