Lesson 8 of 9 · 22m
Mutation Testing with Infection
What MSI really measures, how to interpret killed and escaped mutants, and how to use Infection without turning CI into a vanity metric.
Coverage tells you which lines executed. It does not tell you whether the tests would notice a broken implementation. Mutation testing attacks that illusion directly: it makes small deliberate changes to your code, reruns tests, and asks whether anything noticed.
The mental model
Infection mutates code like this:
if ($attempts >= 3) {
throw new TooManyAttempts();
}into mutations such as > 3, >= 2, true, or removing the exception. Each mutant is a small possible bug. If a test fails, the mutant is killed. If all tests pass, the mutant escaped.
An escaped mutant means one of three things: you are missing a meaningful assertion, the behavior is genuinely untested, or the mutation is equivalent — behaviorally identical in this context.
Equivalent mutants are the tax of mutation testing. They are rare enough to tolerate and common enough that chasing 100 percent is irrational.
Run Infection deliberately
vendor/bin/infection --threads=max --min-msi=72 --min-covered-msi=82 --only-coveredMSI is the Mutation Score Indicator: killed mutants as a proportion of all mutants, adjusted for uncovered and escaped outcomes depending on configuration. covered MSI focuses on code your tests execute. A project can have 90 percent line coverage and 45 percent mutation score if assertions are weak.
Configure the noise out
infection.json5 belongs in the repository:
{
timeout: 10,
source: { directories: ["src"], excludes: ["Kernel.php", "Migration"] },
mutators: { "@default": true, "TrueValue": { ignore: ["src/Security/AlwaysAllow.php"] } },
logs: { text: "build/infection.log", summary: "build/infection-summary.log" }
}Exclude generated code, framework glue, and intentionally defensive branches that cannot be observed through public behavior. Do not disable mutators because they are inconvenient. Disable them because they do not express a real risk in that code.
Use escaped mutants as test design feedback
A typical escaped mutant report means your test checked the happy path only. Example: changing > to >= survives because every test uses values far from the boundary. Add boundary cases: 2, 3, and 4 attempts. The value of Infection is not the score; it is the list of missing behavioral edges.
Run it on focused code first:
vendor/bin/infection --filter=src/Billing --threads=maxA full run may take longer than CI allows. Mutation testing is inherently expensive because it reruns tests for every mutation. Treat it as a targeted quality gate for high-risk logic and a scheduled analysis for the rest.
Field notes
- A mutant killed by an unrelated test often indicates order dependence or overbroad tests. Investigate rather than celebrating.
- Assertions on exact boundaries matter more than assertions on enormous random inputs.
- Global test state makes Infection slow and flaky. Fix isolation first.
- Do not set
min-msito 100; you will spend your career arguing with equivalent mutants.
Was this lesson helpful?