Mutation Testing

Statistics
Introduced by
Richard DeMillo, Richard Lipton, and Frederick Sayward
Year
1978
Type
Test effectiveness evaluation
Family
Software Testing

What does it do?

Mutation testing is a fault-based technique, introduced by DeMillo, Lipton, and Sayward in 1978, for evaluating the effectiveness of a test suite.

It introduces small, deliberate changes (mutants) into the source code and checks whether the tests detect them. The resulting mutation score measures how many of these seeded faults the tests can distinguish from correct behaviour, which is a different question from how much of the code the tests execute.

The intuition behind the method

Code coverage (the share of lines executed) is an incomplete measure of test quality, because a test can execute a line without checking its result. Mutation testing asks a stricter question: if a comparison such as < is changed to ≤, or a + to a -, does any test fail? If all tests still pass, the mutant has survived, which means the suite does not check that behaviour. A high kill rate suggests the tests assert the behaviour of the code rather than merely running it. The method rests on two assumptions. The first is the competent-programmer hypothesis: the program under test is assumed to be close to correct, so its faults are small deviations, such as a wrong operator or constant, rather than arbitrary errors. The second is the coupling effect: tests that detect simple faults tend also to detect more complex ones, which DeMillo, Lipton, and Sayward (1978) note can be used to save testing work. Both are working assumptions rather than guarantees, so a mutation score is informative only to the extent that they hold for the code being tested.

History

DeMillo, Lipton, and Sayward (1978) proposed mutation testing as a test-adequacy approach. Their abstract describes the coupling effect: tests that uncover simple errors are often also effective at uncovering more complex ones, which the authors suggest can reduce testing work. Jia and Harman (2010) surveyed the development of the field, which has been studied for over three decades. Their development-trend analyses indicate that mutation testing techniques and tools are reaching a state of maturity and applicability. The survey describes the development of the field; it is not a measurement of how effectively the method detects faults. Automated tools now carry out the generation and execution of mutants for many programming languages.

When to use it

Choose mutation testing when the question is whether a test suite actually detects faults, not merely whether it executes the code. It is most useful for key modules where test assertions matter and where a low score would be informative, such as code whose coverage is already high. It is a poor fit for very large codebases, where the number of mutants makes full runs costly, unless the analysis is limited to changed or covered code. It should not replace coverage measurement: a suite with low coverage will usually have a low mutation score, so the two measures are complementary.

Limitations and considerations

  • Computational cost grows with the number of mutants, because each mutant requires a run of the test suite; large projects can need long runs.
  • Equivalent mutants cannot be reliably identified automatically in general, so they usually require manual review, and their presence lowers the usefulness of the raw score.
  • The result depends on the mutation operators the tool implements; faults that no operator simulates are never seeded, so a high score says nothing about them.
  • On small or simple functions with few mutation points, the effort may add little beyond ordinary tests.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Exposes weak assertions: a suite can have high line coverage and still leave many mutants alive, which a coverage report alone would not show.
  • Produces a concrete list of survived mutants, each of which names a code location and a change the tests failed to detect, giving a direct guide for writing new tests.
  • Separates equivalent mutants from genuine test gaps during review, so that unreachable or redundant logic can be identified as a distinct finding.

Applications

  • Assessing the adequacy of test suites for critical modules, where the question is whether the tests detect deliberately seeded changes.
  • Checking that tests added after a bug fix would detect the original fault, by confirming that the corrected behaviour is asserted.
  • Using the list of survived mutants to prioritise new test cases in areas where the suite is weakest.

How it works

How it works

1

Parse the source code and identify mutation points, such as operators, literals, and method calls, where a change can be applied.

2

For each mutation point, generate a modified program (a mutant), run the test suite against it, and record whether the mutant was killed, survived, or timed out.

3

Compute the mutation score as the number of killed mutants divided by the number of mutants considered. A mutant is killed when at least one test fails, survives when all tests pass, and is equivalent when its behaviour is identical to the original. Equivalent mutants can never be killed, so two conventions are in use: the score is either killed / total, with equivalent mutants counted as missed, or killed / (total − equivalent), with them excluded. The two give different values for the same suite, so a reported score should state which convention was used and how many equivalent mutants were set aside.

4

Inspect each survived mutant. It may point to a weak test that needs a new assertion, to an equivalent mutant that can be set aside once confirmed, or to unreachable code.

Common pitfalls

  • Ignoring equivalent mutants: if many survivors are equivalent and are counted as missed, the score understates test quality; if they are silently removed, the score may be inflated.
  • Limiting the operator set to, for example, arithmetic and relational operators while omitting constant and return-value changes, which can leave a class of faults untested.
  • Counting timeouts as kills: a mutant that causes an infinite loop is detected by the timeout, not by an assertion, so the kill does not show that the tests check the behaviour.
  • Reading a high mutation score as evidence of code correctness; the score measures how well the tests detect the seeded changes, not whether the code is free of faults.

Frequently asked questions

What is the difference between mutation score and code coverage?

Coverage measures the share of code that is executed; the mutation score measures the share of seeded changes that the tests detect. A test can execute a branch without asserting its result: the line is covered, but a mutant that changes the branch's outcome survives. Mutation testing is therefore the stricter measure.

What is an equivalent mutant and why is it a problem?

An equivalent mutant behaves identically to the original program for every input, so no test can kill it. For example, replacing y = x with y = x + 0 produces an equivalent mutant. Because such mutants cannot be killed, a low score may partly reflect them, and they must be identified, usually by manual review, before the score is interpreted.

How can I reduce the cost of mutation testing?

Common approaches are to mutate only code that the tests execute, to mutate only code changed since the last run, and to restrict analysis to key modules rather than the whole codebase. The cost can also be reduced by running mutants in parallel. Each approach narrows which faults are seeded, so the resulting score should be interpreted with that scope in mind.

Which mutation operators should I use?

Start with the standard operators: replacing arithmetic and relational operators (for example + with − or < with ≤), deleting statements, and negating conditions. Mutation tools for most languages offer larger operator sets, and each tool's documentation lists the operators it applies. Add domain-specific operators when the code relies on them, for example bitwise operators in systems code.

Sources

  1. DeMillo, R. A., Lipton, R. J., & Sayward, F. G. (1978). Hints on test data selection: Help for the practicing programmer. IEEE Computer, 11(4), 34–41.

    Open DOI reference
  2. Just, R., Jalali, D., Inozemtseva, L., Ernst, M. D., & Holmes, R. (2014). Are mutants killed by tests? How test suite composition affects the effectiveness of mutation testing. Proceedings of the 22nd ACM SIGSOFT International Symposium on Foundations of Software Engineering.

    Open publication reference
  3. Jia, Y., & Harman, M. (2010). An analysis and survey of the development of mutation testing. IEEE Transactions on Software Engineering, 37(5), 649–678.

    Open DOI reference