In brief
Mutation testing tests your tests: a tool plants small, deliberate bugs (mutants) in the code, for example changing `>=` to `>`, and runs the tests again. If no test fails, the mutant "survived", which means your tests would not catch that bug. The share of killed mutants says more about test strength than code coverage does. Start with modules that hold business rules, run the analysis only on changed code and read every surviving mutant.
How a mutation test works
The tool takes working code and creates many slightly altered copies of it. Each copy contains one change. For every copy, it runs the unit tests that touch the changed line.
An example from a sales system. The rule says: "a 5% discount for orders of 1,000 PLN or more".
if (wartoscZamowienia >= 1000) {
return wartoscZamowienia * 0.95;
}The team has two tests: an order worth 1,500 PLN gets the discount, an order worth 500 PLN does not. Line coverage is 100%. The tool changes `>=` to `>` and runs both tests. Both pass, because neither checks an order worth exactly 1,000 PLN. The mutant survived, and the report points to the missing case: the boundary value. Once a test for 1,000 PLN is added, the same mutant is killed.
Three terms are enough to read the report:
- killed mutant: at least one test failed, so the tests would catch such a bug,
- surviving mutant: all tests passed, so there is a gap in the tests or the change does not matter,
- mutation score: the share of killed mutants among all mutants that could be evaluated.
Why code coverage is not enough
Coverage tells you which lines ran during the tests. It does not tell you whether a test checked anything. A test without assertions that just calls a method gives full coverage and zero protection. Such tests appear more often than you would expect: under pressure from a coverage gate in CI, in automatically generated tests and with assertions like "the result is not empty".
| Situation | Line coverage | Mutation score |
|---|---|---|
| the test calls the method and has no assertion | high | close to zero |
| the assertion only checks that a result exists | high | low |
| no test for the boundary value | high | surviving condition mutants |
| tests check the result and the boundary values | high | high |
That is why the mutation score complements the software quality metrics a team already collects. Coverage shows what nobody tests. The mutation score shows what is tested only on the surface.
What kinds of bugs the tool plants
The changes are called mutation operators. Tools differ in their sets, but the basic groups are the same everywhere:
| Operator | Example change | The gap it reveals |
|---|---|---|
| conditional boundary | `>=` to `>` | no test at the boundary |
| negated condition | `==` to `!=` | the test does not cover both branches |
| math operator | `*` to `/`, `+` to `-` | the assertion does not check the numeric result |
| removed call | a skipped audit log write or event publish | nobody checks the side effect |
| return value | returning an empty list, `null`, `false` | the test accepts any result |
It pays off most in code that calculates money, deadlines and permissions. There, a surviving conditional mutant is often a real bug that will one day reach production: interest accrued one day too long, a discount granted at the wrong amount, access given to a role that should not have it.
Tools and the cost of running them
For Java and other JVM languages, the standard tool is PIT. For JavaScript, TypeScript, C# and Scala there is Stryker. Both run as plugins to common build tools and produce an HTML report that lists surviving mutants next to specific lines.
The cost is easy to estimate: the number of mutants times the run time of the tests that touch them. In a large module that means hundreds or thousands of runs, so a full analysis of the whole repository on every commit rarely makes sense. Practical settings:
- Analyze only changed code on a pull request. PIT and Stryker can remember the results of the previous run and recompute only what changed.
- Run the full analysis nightly or once a week, with a score for each module.
- Use a threshold that never drops. Stryker lets you set a threshold below which the run fails. Start from the current score and raise it step by step, rather than writing an ambitious number the team will switch off on day one.
- Exclude code with a stated reason: generated code, DTO mappings, logging. Mutants there cost time and tell you nothing.
Mutation testing applies to unit and component tests, the lower layers of the test pyramid. End-to-end tests are too slow and too coarse for it.
How to read surviving mutants
Every surviving mutant has one of four causes:
- a missing test, most often a boundary value or the other branch of a condition: add the test,
- a weak assertion: the test runs the code but checks too little, for example only the number of line items instead of the amounts,
- an equivalent mutant: the change does not alter program behavior (for example a condition whose two variants give the same result for all possible inputs); mark it and skip it,
- dead or redundant code: the line does nothing, so changing it breaks nothing; a signal to simplify the code.
Review surviving mutants together with the author of the code, for fifteen minutes on the pull request. It is a quick lesson in test design and works better than a general comment like "add more tests". When a team starts generating tests with a language model, the mutation score is one of the few measures that show whether those tests check anything at all. More on that in AI in test automation.
Mutation testing complements test automation well: it shows which tests in a regression suite really guard the business rules and which ones only execute the code.
Questions and answers
What is a good mutation score?
There is no single number for a whole system. For a module with financial rules, aim for a high score and review every surviving mutant. For configuration code, a low score is not a problem. The trend matters more than the number: the score should not drop with further changes, and new code should score at least as well as the rest of the module.
Will mutation testing slow down CI a lot?
A full analysis of a large module takes a long time, so on a pull request you run it only for the changed files and do the full run overnight. With that split, the extra time on a pull request is usually small. A fast unit test suite also helps: the shorter a single test run, the cheaper the analysis.
Where should you start in an older system with many tests?
With the one module that carries the highest business risk, for example fee calculation. The first report usually shows a lot of surviving mutants. You do not need to close them all at once: record the current score as the threshold, and with every change to the module, close the surviving mutants in the lines you touch.
Does mutation testing replace reviewing the tests?
No. The mutation score shows whether the tests detect changes in the code, but it cannot tell you whether the code does what the business wants. A test can kill a mutant and still check the wrong rule. Reviewing test cases with an analyst or the process owner is still needed.