Mutation testing
Мутационное тестирование
This technique makes small, deliberate changes to code and reruns automated tests to see whether they catch the changes. It can reveal missing test cases or checks even when code coverage is high.
What it tells you about your tests
A passing test suite tells you that the code satisfies the checks you wrote. Mutation testing asks a different question: would any of those checks fail if the code were slightly wrong? A test can execute a line without checking what it does. Coverage records that execution; mutation testing gives you evidence about whether the test would catch a change in behavior.
How a run works
Start with a passing test suite. A mutation tool then creates altered versions of the code, called mutants, usually with one edit per version. It might change a comparison, replace a constant, or negate a condition. The tool runs tests against each mutant in isolation. If a test fails because of the edit, the mutant is killed. If the tests pass, it survives. The edits are temporary, not fixes to commit.
The mutation score summarizes how many of the counted mutants the suite detected. If 24 out of 30 counted mutants are killed, the score is 80%. Before comparing scores, check how the tool treats outcomes such as timeouts and mutants that fail to compile. The list of surviving mutants is often more useful than the headline percentage because it points to specific changes the tests missed.
Example: an inclusive discount threshold
Suppose a store gives a discount on orders of $50 or more. The code receives the order total in cents:
def qualifies_for_discount(total_cents):
return total_cents >= 5000
assert not qualifies_for_discount(4999)
assert qualifies_for_discount(5001)
Both tests still pass if a tool replaces >= with >. They check a total below the cutoff and one above it, but neither checks the cutoff itself. Adding assert qualifies_for_discount(5000) kills that mutant. The point is not just to raise the score: the new test records the business rule that a $50 order qualifies.
Where it helps and where it doesn't
Start with important logic that has fast, reliable tests, such as pricing rules or eligibility calculations. Running tests for many variants can be expensive, so a team might analyze changed code in pull requests and schedule broader runs separately. PIT is one option for Java projects; StrykerJS supports JavaScript and TypeScript.
A survivor is a clue, not automatically a missing test. Check whether it changes behavior for any valid input. For integer cents, total_cents > 4999 behaves exactly like total_cents >= 5000. This is an equivalent mutant: no valid test can distinguish it from the original. A high score also doesn't prove correctness, because it only reflects the kinds of edits the tool tried.
In an interview
If asked how to improve a suite with 100% line coverage, explain that executing code is not the same as checking its behavior. Use a surviving mutant to show how you'd decide what to do next: identify the affected requirement, find an input that distinguishes the two versions, and assert the expected result.