Notes
Coverage
Code coverage measures which code ran while the tests ran. Line or statement coverage: did each line run at least once? Branch coverage: did each if statement go both ways, true and false? Condition coverage: did each part of an and or an or expression take both values? Tools record this for you: JaCoCo for Java, coverage dot py for Python, and Istanbul or c eight for JavaScript, usually as an HTML report with red and green lines.
- line / statement: each line ran at least once
- branch: each if went both ways (true and false)
- condition: each part of a && or || was true and false
- tools: JaCoCo (Java), coverage.py (Python), Istanbul / c8 (JavaScript)
A weak suite
Here is ShopRung's shipping rule, simplified: free from seventy five hundred cents, otherwise four ninety nine. Two tests: a ten dollar order pays, a ninety dollar order is free. Every line runs and the if statement goes both ways, so line coverage and branch coverage are both one hundred percent. The report is all green. Now, is the boundary at exactly seventy five dollars tested? No. Coverage cannot tell you that.
def shipping(after):
if after >= 7500:
return 0
return 499
def test_cheap():
assert shipping(1000) == 499
def test_expensive():
assert shipping(9000) == 0
# lines 100%, branches 100% The blind spot
That is the blind spot. Coverage says code ran, not that anything checked its result. A test with no assertion at all still counts as covering every line it runs. Tests with wrong expected values still cover. Values far from the edge cover the comparison without testing it. And missing code, a rule someone forgot to write, has no lines to cover, so the report can never show it. Use coverage as a floor that finds untested areas, never as proof of quality.
- a test with NO assert still counts as covering lines
- wrong expected values still cover
- boundaries: 1000 and 9000 cover >= but never test it
- missing code (a forgotten rule) has no lines to cover
- coverage: a floor, not proof
Mutation testing
Mutation testing asks a better question: would your tests catch a bug? A tool makes copies of your code, each with one small change, called a mutant: greater than or equal becomes greater than, a plus becomes a minus, a return value changes, a line disappears. It runs your tests against each mutant. If a test fails, the mutant is killed, which is good. If every test still passes, the mutant survived, and you have found a gap.
- 1 make a copy of the code with ONE small change: a mutant >= → >, + → -, return 0 → 1, delete a line
- 2 run the tests against it
- 3 tests fail → mutant KILLED tests pass → mutant SURVIVED
- survivors = gaps in your tests
A survivor
Change greater than or equal to greater than. Run the two tests. Ten dollars still pays four ninety nine. Ninety dollars is still free. Both pass, so the mutant survives. And that exact mutant is ShopRung's free shipping boundary bug, where seventy five dollars stops being free. One hundred percent coverage, and the suite would have let that bug ship. The surviving mutant points straight at the missing test.
def shipping(after):
if after > 7500: # mutant
return 0
return 499
test_cheap(1000) → 499 pass
test_expensive(9000) → 0 pass
SURVIVED: no test noticed
= ShopRung bug
"free-shipping-boundary" Kill it
Add one test: shipping at exactly seventy five hundred cents is free. Against the mutant, seventy five hundred is not greater than seventy five hundred, so it returns four ninety nine and the new test fails. The mutant is killed. The mutation score is the share of mutants killed. Raising it means adding tests that check results precisely, especially at boundaries, which is exactly what Part one taught you to design.
def test_free_at_exactly_75():
assert shipping(7500) == 0
mutant: 7500 > 7500 is False
→ returns 499 → test FAILS
→ mutant KILLED
mutation score =
killed / total mutants Tools
Every language has a mutation tool. In Java, PIT, run as a Maven plugin, which lab eight of this track uses. In Python, mutmut. In JavaScript and TypeScript, Stryker. Each produces a report listing killed and surviving mutants, mutants in code no test even runs, and mutants that timed out, often because a change created an endless loop, which also counts as caught.
Java PIT (pitest)
mvn org.pitest:pitest-maven:
mutationCoverage
Python mutmut
mutmut run
JS / TS Stryker
npx stryker run
report: killed, survived,
no coverage, timed out Use it wisely
Mutation testing is expensive, because the tests run once for every mutant, which can take hours on a big codebase. So aim it at code where bugs cost the most: money rules, authentication and core business logic. Run it on changed code in pull requests and do full runs nightly. Also know that some mutants are equivalent: they change the code without changing any visible behaviour, so no test can kill them. Do not chase one hundred percent.
- cost: tests run once PER mutant → can take hours
- aim it: money rules, auth, core business logic
- run on changed code in PRs (incremental), full runs nightly
- some mutants are equivalent: they change nothing visible → ignore, do not chase 100%
Use both
Use them together. Coverage is fast and runs on every build: it finds code that no test runs at all, and a sensible floor, like eighty percent branch coverage on new code, keeps gaps from growing. Mutation testing is slower and focused: it finds tests that run code without really checking it. And remember what neither can find: rules nobody wrote. That still needs requirements analysis and exploratory testing.
- coverage fast, every build finds code no test runs good as a floor: e.g. new code >= 80% branches
- mutation slower, focused finds tests that would not catch real bugs
- neither finds missing rules
Common mistakes
How coverage numbers mislead. Making one hundred percent line coverage the goal, which invites tests written only to raise the number, sometimes with no assertions. Measuring lines but not branches. Comparing coverage between different codebases as if it measured quality. Running mutation testing on the whole repository for every pull request. And spending days trying to kill equivalent mutants that no test could ever catch.
- 100% line coverage as goal
- tests written only to raise the number (no asserts)
- line coverage, not branch
- comparing coverage across different codebases
- mutation on the whole repo on every PR
- chasing equivalent mutants
Say it like this
The classic question: is one hundred percent coverage enough? Say no. Coverage shows what ran, not what was checked: a test with no assertion still covers lines, and boundaries can be missed entirely. I track branch coverage as a floor, and use mutation testing, with PIT or Stryker, on critical code to find tests that would not catch a real bug, like greater than or equal changed to greater than. Then mention the ShopRung style example.
- "No. Coverage shows what ran, not what was checked: a test
- with no assert covers lines, and boundaries can be missed.
- I track branch coverage as a floor, and use mutation
- testing, PIT or Stryker, on critical code to find tests
- that would not catch a bug, like >= changed to >."