100% coverage, bug intact
Every line ran. Nothing checked the answer.
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.
// 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);
});// Assert the value, and the edge either side of the threshold.
test('shippingFee', () => {
expect(shippingFee(1000, false)).toBe(499);
expect(shippingFee(1000, true)).toBe(249);
expect(shippingFee(4999, false)).toBe(499);
expect(shippingFee(5000, false)).toBe(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.
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
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.
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.
“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.
USED IN
Chapters that build on this lesson.
- Choosing cases: boundaries, equivalence classes and the unhappy pathSoftware Testing and Quality
- Coverage and mutation testing: measuring the testsSoftware Testing and Quality
- Project: put a legacy function under test and find its bugSoftware Testing and Quality
- Quality metrics that mean somethingSoftware Testing and Quality
- Why we test: what a test can and cannot proveSoftware Testing and Quality