Section 10.45

Mutation testing

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

  1. 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.

  2. Generate mutants: apply each operator at each place where it fits, one change per mutant. Discard the mutants that do not compile.

  3. 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.

  4. 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.

  5. 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:

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 15>1615 > 16 is false and 17>1617 > 16 is true. T2 kills M2, because 17>1717 > 17 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: 15≥1615 \ge 16 and 15>1515 > 15 are false, while 17≥1617 \ge 16 and 17>1517 > 15 are true. M5 is equivalent to the original: for whole-number lengths, 𝚕𝚎𝚗≥17\texttt{len} \ge 17 holds exactly when 𝚕𝚎𝚗>16\texttt{len} > 16 does, so no test can kill it.

Two of the four mutants that are not equivalent are killed, a score of 2/4=0.52 / 4 = 0.5, or 50%. Counting M5 by mistake would give 2/5=0.42 / 5 = 0.4. 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 16>1616 > 16 is false. M1 rejects it, since 16≥1616 \ge 16 is true, and M4 rejects it, since 16>1516 > 15 is true, so T3 kills both. The score becomes 4/4=14 / 4 = 1, 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 MM be the set of mutants that compile, E⊆ME \subseteq M the set of mutants equivalent to the original program, and K⊆MK \subseteq M the set of mutants killed. Write |S||S| for the number of elements of a set SS. The mutation score is score=|K||M|−|E|,\text{score} = \frac{|K|}{|M| - |E|}, defined when |M|>|E||M| > |E|. A test tt kills a mutant mm when tt passes on the original program and fails on mm. In the example, |M|=5|M| = 5 and |E|=1|E| = 1; first |K|=2|K| = 2, giving 2/(5−1)=0.52 / (5 - 1) = 0.5, and after T3, |K|=4|K| = 4, giving 4/4=14 / 4 = 1.

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 2000×30=60,0002000 \times 30 = 60{,}000 seconds, about 60,000/3600≈16.760{,}000 / 3600 \approx 16.7 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].