Topic 2 of 5 · 4 min read
Mutation thinking: would this test catch a bug
Goal: Name a small code change an AI-written test suite would miss, and the assertion that would catch it.
Mutation thinking means making a small deliberate bug in the code and checking that at least one test goes red; if none does, the tests have a hole. It is the fastest way to judge a test an AI wrote, because it replaces "does this look right" with a result you can observe.
Check yourself · 1 of 3
In mutation testing, what does a surviving mutant tell you?
Why it matters
Reading a generated test tells you what it says. Mutating the code tells you what it protects. A suite can reach 95 percent line coverage and still miss that > became >=. Mutation testing tools call each change a mutant. A mutant that makes a test fail is killed. One that no test notices survives, and every survivor is a place a real bug could hide.
The by-hand method
Pick a function and try the common small changes:
- Flip a comparison:
>to>=,<to<=. - Change a constant:
10to11,0to1. - Swap an operator:
+to-,&&to||. - Delete a line, or a whole branch.
- Return an empty value or a fixed one.
After each change, run the tests. If they stay green, write down the missing case.
The tool method
Stryker (JavaScript, TypeScript), PIT (Java) and mutmut (Python) generate the mutants and run your tests against each. They report a mutation score and list survivors. Run them on one module at a time: they rerun the suite many times, so they are slow on big projects.
Before and after
export const canVote = (age: number) => age >= 18;
test("adult can vote", () => {
expect(canVote(30)).toBe(true);
});
Change >= to >: age 30 still passes. The mutant survives. Add the boundary and the other side:
test.each([[17, false], [18, true], [19, true]])("age %i", (age, ok) => {
expect(canVote(age)).toBe(ok);
});
Now > fails on 18, and a mutant that makes everything true fails on 17.
What goes wrong
- Chasing 100 percent. Some survivors are equivalent mutants: the change does not alter behaviour. Judge them, do not blindly kill them.
- Killing mutants with tests that copy the code. Writing an assertion per mutant can recreate the tautology problem.
- Mutating once and stopping. Run a few different kinds of change, especially boundaries and deleted lines.
- Running the tool on the whole repository. Scope it to the changed files.
Examples
The deleted line
def checkout(cart):
validate_stock(cart)
return charge(cart.total)
Delete validate_stock(cart) and run the tests. All green? Then no test covers out-of-stock behaviour. Add a case with an empty shelf and assert that checkout raises OutOfStock and charge is not called.
A report read in plain terms
A Stryker report says discount.ts: 14 mutants, 9 killed, 5 survived. Three survivors are > versus >= at the 100-unit threshold. That tells you exactly which boundary case to add.
Key points
- A surviving mutant is a bug the suite would let through.
- Boundary flips such as > to >= are the cheapest mutations and catch many AI-written tests.
- Stryker, PIT and mutmut automate mutation testing for JavaScript or TypeScript, Java and Python.
- Scope mutation runs to the module you are reviewing, because they rerun the suite many times.
- Some survivors are equivalent mutants that change nothing observable, so judge each one.
Common mistakes
- Judging a generated test by reading it only, without changing the code and running it.
- Treating line coverage as proof that boundaries are tested.
- Running a mutation tool on the full repository and giving up when it is slow.
- Writing a test per surviving mutant that restates the implementation.
Check yourself · 2 of 3
A function returns true when age >= 18. A generated test checks only age 30. Which change would the test miss?
Check yourself · 3 of 3
Which tool runs mutation testing on a Java project?
Practice items, not counted towards your mastery.
Start here
- The Practical Test Pyramid (martinfowler.com) (opens in a new tab)ArticleFree
Where unit, integration and end-to-end tests belong, and why test behaviour rather than implementation.
- Mocks Aren't Stubs (martinfowler.com) (opens in a new tab)ArticleFree
Defines mocks, stubs, fakes and spies, and the difference between state and behaviour verification.
- Stryker mutation testing (opens in a new tab)ToolFree
Mutation testing for JavaScript, TypeScript, C# and Scala: the tool that checks whether tests notice changed code.
More for this topic
- PIT mutation testing (opens in a new tab)ToolFree
Mutation testing for Java, with a clear explanation of mutants, killed and surviving.