Skip to content
mentoor

Topic 4 of 5 · 4 min read

Coverage numbers versus real confidence

Goal: Judge whether a suite with high coverage protects a behaviour by asking which code changes it would still let pass.

Coverage tells you which lines ran during the tests. It does not tell you whether the tests would notice if those lines were wrong. To judge a suite, ask: which changes to the code would still pass?

Check yourself · 1 of 3

A function has 100% line coverage. What does that guarantee?

Choose one answer

Why it matters

Agents are very good at raising coverage. Ask for "100% coverage" and you will get it, often with tests that call a function and assert nothing useful. Martin Fowler notes that coverage is useful for finding code that is not tested, but a high number says little about how good the tests are. Google's testing team makes a similar point: use coverage as a signal, not a goal.

How to do it

  1. Read the assertions, not the percentage. Open a test the agent wrote. What does it check? expect(result).toBeDefined() passes for almost any result.
  2. Think in mutations. Take one line of the code and change it in your head: flip > to >=, remove a condition, return a constant, delete a call. Would any test fail? If not, that line is unprotected, even if it is "covered".
  3. Try it by hand. Make the change in a scratch copy and run the tests. A test that stays green is a surviving mutant, and a gap.
  4. Use a tool for the whole suite. Mutation testing tools such as Stryker (JavaScript, TypeScript) and PIT (Java) do this automatically. They change the code in small ways and report which changes no test noticed. Use them on important modules, since they are slow.
  5. Fix gaps with one targeted test each, reviewed like any other test.

Before and after

export function canRefund(daysSincePurchase: number): boolean {
  return daysSincePurchase <= 30;
}

test("refund works", () => {
  expect(canRefund(5)).toBe(true);
});

Coverage is 100%. Change <= to <, or to <= 300, and the test still passes. The fixed suite adds the cases that pin the rule:

test("refund allowed on day 30", () => expect(canRefund(30)).toBe(true));
test("refund denied on day 31", () => expect(canRefund(31)).toBe(false));

What goes wrong

  • Coverage as a target. The agent writes tests that run lines and check nothing.
  • Trusting a mutation score blindly. Some surviving mutants are harmless, such as a changed log message. Read them.
  • Testing only the agent's own code path. The mutation idea still needs your judgement about which behaviours matter.

Examples

A covered line nobody protects

A tax function has 100% coverage. You change the rate from 0.2 to 0.02 and every test still passes, because the tests only check that the result is a number. The line was covered and unprotected.

A harmless survivor

A mutation report says changing the text of a log message survives. No test needs it to change, and the message is not a requirement. You mark it as acceptable and move to the surviving mutant in the discount rule.

Key points

  • Coverage shows which lines ran, not whether any test would notice a wrong result.
  • Weak assertions such as 'is defined' pass for almost any output.
  • Ask which small code changes would still pass: that is the mutation way of thinking.
  • Mutation tools such as Stryker and PIT automate the check but are slow, so use them on important code.
  • Close each real gap with one targeted test that you review.

Common mistakes

  • Reporting a coverage percentage as proof that the feature is safe.
  • Asking an agent for "100% coverage" and accepting tests that assert nothing.
  • Using mutation tools on a whole large project, then ignoring the slow, noisy output.
  • Chasing every surviving mutant, including harmless ones.

Check yourself · 2 of 3

Which test is weakest at protecting a price calculation?

Choose one answer

Check yourself · 3 of 3

You change '>=' to '>' in the code and no test fails. What does that show?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic