QA AND TEST AUTOMATION · PART 3 · AUTOMATION FUNDAMENTALS · QUALITY · 37 OF 112

Code Coverage vs Mutation Testing, Explained

What code coverage measures (lines, branches, conditions) and what it cannot tell you, then mutation testing, which plants small bugs and checks your tests catch them. Worked on ShopRung's shipping rule: 100% line coverage with a weak suite, the surviving >= to > mutant, the boundary test that kills it, and tools (JaCoCo, coverage.py, PIT, mutmut, Stryker).

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 67
  63. 68
  64. 69
  65. 70
  66. 71
  67. 72
  68. 73
  69. 74
  70. 75
  71. 76
  72. 77
  73. 78
  74. 79
  75. 80
  76. 81
  77. 82
  78. 83
  79. 84
  80. 85
  81. 86
  82. 87
  83. 88
  84. 89
  85. 90
  86. 91
  87. 92
  88. 93
  89. 94
  90. 95
  91. 96
  92. 97
  93. 98
  94. 99
  95. 100
  96. 101
  97. 102
  98. 103
  99. 104
  100. 105
  101. 106
  102. 107
  103. 108
  104. 109
  105. 110
  106. 111
  107. 112
1 / 13

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Coverage: what ran (lines, branches)
  • not what was checked
  • mutation: plant bugs, see tests fail
  • aim at critical code

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.

Kinds of coverage
  • 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.

shipping() and two tests
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.

What coverage misses
  • 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.

The idea
  • 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.

Mutant: >= becomes >
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.

The killing test
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.

Mutation tools
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.

Practical use
  • 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.

Together
  • 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.

Avoid these
  • 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.

In the interview
  • "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 >."

Interview questions

Q1What is code coverage?

A measure of which parts of the code ran during tests: lines or statements, branches (both outcomes of each decision) and conditions.

Q2What is the difference between line and branch coverage?

Line coverage counts lines executed at least once; branch coverage requires each decision to take both its true and false paths, so it is stricter.

Q3Does 100% code coverage mean the code is well tested?

No. Coverage shows code ran, not that results were asserted; tests without assertions, missed boundaries and missing requirements are all invisible to it.

Q4What is mutation testing?

A technique that creates copies of the code with small changes (mutants) and runs the tests; a good suite fails (kills the mutant). Surviving mutants reveal weak tests.

Q5What is a mutation score?

The percentage of mutants killed by the test suite, excluding equivalent mutants where possible.

Q6What is an equivalent mutant?

A mutant that changes the code but not its observable behaviour, so no test can kill it; it should be ignored rather than chased.

Q7Which mutation testing tools do you know?

PIT (pitest) for Java, mutmut for Python and Stryker for JavaScript/TypeScript (and .NET).

Q8How would you use coverage and mutation testing together?

Coverage on every build as a floor to find untested code; mutation testing on critical logic (nightly or on changed code) to find tests that would not catch real bugs.