Video in progress
What does mutation testing (for example, PIT) measure, and how does it complement line coverage?
PIT makes small changes to the code ("mutants"): it swaps > for >=, + for -, changes a return value. Then it checks whether the tests fail on that broken code. A surviving mutant means the tests didn't notice the change.
📖 Answer
Coverage asks "was the code executed?", and mutation testing asks "do the tests actually check it?".
An example:
boolean isEligibleForFreeShipping(BigDecimal total) {
return total.compareTo(new BigDecimal("300000")) >= 0;
}
The test only checks the values 500,000 and 100,000. Line coverage is 100%. PIT replaces >= with >, and both tests still pass. The mutant survived: the boundary value (exactly 300,000) isn't tested.
Running it:
mvn org.pitest:pitest-maven:mutationCoverage
The report shows "killed / survived / no coverage" for each line. The mutation score is the percentage of mutants killed.
Practical limits:
- it's slow: for every mutant the relevant tests are rerun. On a big project it runs as a nightly job over all the code, or only over changed code (
scmMutationCoverage); - there are "equivalent mutants": changes that don't alter behavior, and they can't be killed;
- it's very slow with integration tests, so it's usually applied only to unit tests.
I use it on critical modules that calculate money, discounts and limits, where a boundary bug is expensive.
Sign up to mark studied questions and save your progress.