Definition
Mutation testing is a method for measuring how well a test suite detects faults. It makes many copies of the program, called mutants, each with one small deliberate change, such as > replaced by >=, and runs the test suite on every mutant. A mutant is killed when at least one test that passes on the original program fails on it; otherwise it survives. The mutation score is the fraction of mutants killed, leaving out mutants that behave exactly like the original. Mutation testing evaluates the tests, not the program.
The idea
A test suite passes and executes every branch of a length check. That shows that the code ran, not that the tests would notice a wrong result: a test can execute a comparison without ever using an input on which the comparison matters. Mutation testing asks directly, one small fault at a time: change > to >= and rerun the tests. If every test still passes, no test tells the two programs apart, and the suite would have missed that fault had it been real.
The method plants faults the way a fire drill stages a fire: to test the response, not to cause harm. The comparison stops at realism: a drill can resemble a real fire closely, while a mutant is only a small change to the program text. The method therefore rests on two hypotheses stated by DeMillo, Lipton and Sayward in 1978 [502]. The competent programmer hypothesis says that real programs are usually close to correct, so many real faults are small changes. The coupling effect says that tests which detect simple faults also detect most complex faults built from them.
How it works
Choose mutation operators. A mutation operator is a rule for one kind of small change: replace a comparison operator (
<by<=) or an arithmetic operator (+by-), change a constant by one, replace&&(and) by||(or), negate a condition, delete a statement, or replace a returned value with a constant.Generate mutants: apply each operator at each place where it fits, one change per mutant. Discard the mutants that do not compile.
Run the suite on each mutant. The suite must first pass on the original program. A mutant is killed when some test fails on it. Tools usually also count as killed a mutant that makes a test run past a time limit, for example by turning a loop into an endless one, because the tests noticed a difference.
Classify the survivors into three groups.
Not executed: no test runs the mutated code, so the suite lacks a test for that path.
Executed but not detected: a test runs the mutated code, but its inputs never make the change matter, or the change alters a result that the test oracle §10.5, the check that decides pass or fail, does not examine. The suite lacks an input or a check.
Equivalent: the mutant computes the same result as the original for every possible input, so no test can kill it.
Act on each survivor that is not equivalent: add a test, derived from the requirement, that fails on the mutant and passes on the original.
Deciding whether a mutant is equivalent is undecidable §19.2.1 in general: no algorithm §14.1 decides it correctly for every program, because the question includes deciding whether two programs compute the same function. In practice equivalence is judged by hand, helped by tools that settle some cases. The usual form of the method, strong mutation, requires the difference to show in a test’s verdict. Weak mutation counts a mutant as killed as soon as the program’s state differs right after the mutated code runs; it is cheaper, but does not show that any test would report the difference.
The same check applies to a defect fix, because the faulty and the fixed version differ by a small change, just as a mutant and the original do. A test written for the fix must fail on the old code and pass on the new one; only then is it shown to detect the defect. If an existing suite passes unchanged on both versions, it cannot tell them apart, however many cases it has: none of its cases both reaches the defect and checks the result that the defect changes.
Worked example
A function accepts a message when its length len is at most 16 bytes and rejects it otherwise. The original code is if (len > 16) reject;. Five mutants change the condition or the statement:
M1:
len >= 16, which wrongly rejects length 16;M2:
len > 17, which wrongly accepts length 17;M3: the
rejectstatement deleted, which accepts every length;M4:
len > 15, which wrongly rejects length 16;M5:
len >= 17, a change that turns out to make no difference.
The suite has two tests: T1 sends length 15 and expects acceptance, and T2 sends length 17 and expects rejection. Both pass on the original, since is false and is true. T2 kills M2, because is false, so M2 accepts length 17; T2 also kills M3, which accepts every length. M1 and M4 survive, because on lengths 15 and 17 they decide exactly as the original does: and are false, while and are true. M5 is equivalent to the original: for whole-number lengths, holds exactly when does, so no test can kill it.
Two of the four mutants that are not equivalent are killed, a score of , or 50%. Counting M5 by mistake would give . The survivors M1 and M4 are executed by both tests, and they differ from the original only at length 16, which no test uses: the suite lacks the boundary input that boundary-value testing §10.31 would add. A third test T3 sends length 16 and expects acceptance. The original accepts it, since is false. M1 rejects it, since is true, and M4 rejects it, since is true, so T3 kills both. The score becomes , or 100%, and only the equivalent M5 survives.
A weak oracle shows up differently. If T2 checked only that the function returned without crashing, instead of checking that it rejected the message, M2 and M3 would survive as well, although T2 executes the changed code.
Stated precisely
Let be the set of mutants that compile, the set of mutants equivalent to the original program, and the set of mutants killed. Write for the number of elements of a set . The mutation score is defined when . A test kills a mutant when passes on the original program and fails on . In the example, and ; first , giving , and after T3, , giving .
The score depends on the mutation operators, the places they were applied, the equivalence decisions and the test suite, and a report must state all four. Mutants whose equivalence is unresolved belong in the denominator or in a separate count; dropping them raises the score without evidence. A score of 1 states only that for every chosen change, some test tells the mutant apart from the original.
Limits and common mistakes
A mutation score measures the tests against the chosen kinds of change only. Faults of other kinds, such as a missing feature, a misunderstood requirement or a timing problem between threads, have no mutant, so a score of 100% says nothing about them. Killing every mutant does not show that the program is correct.
Every mutant needs a run of the suite. With 2000 mutants and a suite that takes 30 seconds, a full run takes seconds, about hours. Tools cut this cost by running only the tests that execute the mutated code, by stopping at the first failing test, and by sampling mutants.
A nondeterministic test §10.38, one that sometimes fails without any change to the program, can kill a mutant for the wrong reason. A kill counts only if the test passes reliably on the original program.
A test written only to kill a mutant, by asserting an internal detail of the code, raises the score without checking any requirement. Many mutants are also killed by any test that runs them at all, so a high score can come from easy mutants, and scores computed with different operators or tools cannot be compared.
Mutation testing changes the program to evaluate the tests. Mutation-based fuzzing §10.10 changes the inputs to search for failures in an unchanged program. Fault injection §10.28 changes the running conditions, for example by making a memory allocation fail, to check how the program handles errors.
Questions answered
Which surviving mutations reveal a weak oracle or untested path?
A surviving mutant that is not equivalent to the original program reveals a gap in the tests, and whether any test executes the mutated code decides which gap. A mutant is a copy of the program with one small deliberate change; it survives when every test still passes on it. If no test executes the changed code, the survivor reveals an untested path. If a test executes the changed code and still passes, the survivor reveals either a weak oracle, a check that ignores the result the change alters, or a missing input on which the change matters, such as length 16 when len > 16 becomes len >= 16.
To tell the two apart, compare the original’s and the mutant’s results on the tests that execute the mutated code. If the results differ but the test still passes, the oracle is weak; if the results are equal on every test input, an input is missing. A survivor that computes the same result as the original for every possible input is an equivalent mutant and reveals nothing. Close each real gap with a test, derived from the requirement, that fails on the mutant and passes on the original.
Related entries: Test oracle (§10.5), Mutation-based fuzzing (§10.10), Negative control (§10.29), Boundary-value testing (§10.31), Coverage metric (§10.18), Coverage versus correctness (§10.40), Fault injection (§10.28), Regression test (§10.4). Further reading: [502].