Phase 4: Mutation Testing & Behavioral Verification · 50 min · Python · pytest · mutmut
The Problem
A team has 92% code coverage on their AI-generated deduplication module. They feel confident. But there's a bug: the function removes duplicates AND sorts the output, changing the order. The tests execute 92% of the lines, but they don't verify that the order is preserved. Coverage is green. The bug is in production.
Code coverage measures which lines were executed. It does NOT measure whether the tests actually check the correct behavior. A test that calls deduplicate([3, 1, 2, 1]) and asserts the result is [1, 2] executes 100% of the function's lines. But it doesn't catch the order-changing bug because the expected output [1, 2] happens to be sorted -- the test author didn't realize the function was sorting in addition to deduplicating.
Coverage vs mutation score
| Metric | What it measures | What it misses |
|---|
| Code coverage | Which lines were executed | Whether assertions are correct |
| Branch coverage | Which branches were taken | Whether branch outcomes are verified |
| Mutation score | What % of code changes are caught by tests | Nothing -- it's the gold standard |
Mutation Score Formula
mutation_score = killed_mutants / (total_mutants - equivalent_mutants)
- Killed: the test suite catches the mutation (a test fails)
- Surviving: the mutation passes all tests (the tests don't catch this change)
- Equivalent: the mutation doesn't change behavior (can't be killed)
- Timeout: the mutation causes an infinite loop
The 92% coverage / 40% mutation score gap
A function with 92% line coverage can have a 40% mutation score. This means 60% of behavioral changes to the code go undetected by the test suite. The tests execute the lines but don't verify the behavior. For AI-generated code, this gap is critical: AI code has subtle behavioral bugs (wrong order, missing edge cases, incorrect error handling) that pass coverage checks but fail mutation testing. Mutation testing is the only metric that measures what your tests actually TEST, not just what they EXECUTE.
The deduplication bug case study
# AI-generated function
def deduplicate(items):
return sorted(set(items)) # Bug: also sorts
# Test suite with 100% coverage
def test_dedup_basic():
assert deduplicate([1, 1, 2]) == [1, 2] # Executes all lines
def test_dedup_empty():
assert deduplicate([]) == [] # Executes all lines
def test_dedup_single():
assert deduplicate([1]) == [1] # Executes all lines
# Coverage: 100%. Mutation score: ~40%.
# The mutation "remove sorted()" survives -- no test fails.
# The mutation "remove set()" is killed -- test_dedup_basic fails.
A function has 92% line coverage and 40% mutation score. What does this mean?
92% line coverage means 92% of lines are executed by at least one test. 40% mutation score means only 40% of behavioral changes (mutations) to the code are caught by the test suite. 60% of mutations survive -- the tests don't catch them. This reveals that the tests are calling the code (executing lines) but not properly verifying the results. For example, a test might call deduplicate([1,1,2]) and get [1,2] but not check that the order is preserved. The line is executed (coverage green) but the behavior isn't verified (mutation survives). The gap between coverage and mutation score is the 'false security zone' -- the team thinks the code is well-tested because coverage is 92%, but 60% of behavioral changes would go undetected. For AI-generated code, this gap is especially dangerous because AI code has subtle behavioral bugs that pass coverage but fail mutation testing."
This lesson is free
You've read the first 3 sections. The rest covers The Concept, Build, Check Yourself, Key Terms & Next. A free account opens all of it, with its practice questions and its lab. No card.
Create a free account — the opening phases of 29 of 35 courses, and the first lesson of every phase, are free, no credit card.