Mutation testing
Mutation testing is a way of judging a test suite by deliberately breaking the code it tests. A tool makes many small changes, called mutants, one at a time and runs the tests against each. A mutant that makes a test fail is killed, and one that passes every test has survived and marks a gap in the tests.
The longer version
Take a function that grants free delivery when an order total is 50 or more. A mutation tool rewrites the comparison as greater than 50, runs the tests, restores the code and tries the next change: the constant becomes 51, the condition is replaced by true, the return value is flipped. Each altered copy is a mutant, in effect a small logic error planted on purpose. The PIT documentation puts the rule in one sentence: "If your tests fail then the mutation is killed, if your tests pass then the mutation lived." If no test places an order of exactly 50, the first mutant survives, and the tool has found a boundary the suite never checks.
The result is summarised as a mutation score. Stryker's documentation counts killed mutants and those that caused a timeout as detected, and divides them by the valid mutants, leaving out any that failed to compile or run. A suite that detects 150 of 200 valid mutants scores three quarters. The score answers a question coverage cannot. PIT's documentation says that traditional coverage "measures only which code is executed by your tests" and does not check that the tests can detect faults in that code. A test that calls a function and asserts nothing earns statement coverage for every line it touches and kills no mutants. Mutation testing measures the assertions, the part of test coverage that no coverage tool can see.
The idea is decades old. A survey of the field by Yue Jia and Mark Harman traces it to a student paper by Richard Lipton in 1971 and to papers published in the late 1970s by DeMillo and colleagues and by Hamlet. Two of the problems the survey names still stand. One is the high computational cost of executing an enormous number of mutants against a test set, and PIT's advice for containing it is to run mutation testing frequently against only the code that has changed. The other is the equivalent mutant, which differs in its text and behaves exactly as the original, so no test can kill it and a perfect score may be out of reach.
The changes a tool makes are called mutation operators, and BrowserStack's guide sorts them into three families: value mutations, which alter a constant; decision mutations, which change a condition, for instance by negating an if; and statement mutations, which delete, duplicate or replace a line. Every mutant then ends in a state. Stryker's documentation lists killed, survived, no coverage, meaning no test reached the mutant at all, timeout, runtime error and compile error, and a report sorted by state is the to-do list. Most languages have a tool: PIT for Java, Stryker for JavaScript and TypeScript, C# and Scala, MutPy and Mutatest for Python, Mutant for Ruby and muttest for R. A short trial shows the value. One engineering manager ran Mutant on a small Ruby module that already had complete line coverage, found dozens of surviving mutants, mostly untested default values and unchecked return values, and had all 442 killed after about twenty minutes of new assertions. The muttest documentation adds a caution: the score works best as a direction of travel, and the technique pays least on very simple functions, snapshot tests and slow suites.
Common questions
What is mutation testing?
Mutation testing is a technique for assessing tests, in which a tool introduces small faults into the code and checks whether the existing tests notice. A fault the tests catch is a killed mutant, and one they miss is a surviving mutant that points to a missing or weak test.
What is a mutation score?
A mutation score is the share of mutants a test suite detects. Stryker calculates it as detected mutants divided by valid mutants, where detected means killed or timed out and valid leaves out mutants that could not compile or run. Jia and Harman define it as killed mutants over all non-equivalent mutants.
What is the difference between mutation testing and code coverage?
Code coverage shows which lines or branches the tests executed. Mutation testing shows whether the tests would fail if that code were wrong. Code can be fully covered by tests that assert nothing, and such a suite kills no mutants, so a mutation score exposes weak tests that a coverage figure hides.
What is an equivalent mutant?
An equivalent mutant is a changed version of the code that is written differently and behaves identically to the original for every input. No test can tell the two apart, so the mutant can never be killed. Jia and Harman note that detecting every such mutant automatically is impossible, because program equivalence is undecidable, so sorting them from real gaps takes human effort.
Why is mutation testing expensive to run?
Each mutant is a separate version of the program, and tests have to run against every one. A codebase can yield a very large number of mutants, so the total run time is many times that of the ordinary suite. PIT recommends limiting each run to the code that has changed.
What are mutation operators?
Mutation operators are the rules a tool uses to change the code. Typical ones swap a comparison such as >= for >, replace an arithmetic operator, negate a condition, change a constant, remove a statement or return a fixed value. Each application of an operator to one place in the code produces one mutant.
What does mutating mean in coding?
Mutating has two meanings. In everyday programming it means changing a value in place, as when a function alters an array it was handed instead of returning a new one. In testing it means making a deliberate small change to the source code, a mutant, to see whether the tests notice.
What tools are used for mutation testing?
PIT (also written Pitest) is the usual choice for Java, and Stryker covers JavaScript and TypeScript, C# and Scala. Python has MutPy and Mutatest, Ruby has Mutant and R has muttest. Whichever tool is used, PIT's advice to run it often against only the code that has changed is what keeps the cost manageable in continuous integration.
What is a good mutation score?
No single figure is right for every codebase, because equivalent mutants and code that is not worth testing both cap what is reachable. The muttest documentation recommends reading the score as a guide to direction, and the useful habit is reviewing each surviving mutant in the code that matters most and either adding the missing assertion or recording why it can stay.
Bug reports that arrive ready to triage.
Collect replay, console, network and browser context with the Free plan.