# Mutation testing

## What it is

Mutation testing makes small automated changes to an implementation and runs its
tests against each change. The altered program is called a mutant. A rule might
replace an inclusive comparison with an exclusive one, invert a condition or
remove a return value. The tool checks whether the suite reports a failure when
the change affects the behaviour the tests ought to protect.

When a test fails for a mutant, the tool can say that mutant was killed. A
surviving mutant ran through the selected tests without producing a detected
failure. This gives a team a useful question about one part of its suite. Which
required behaviour would distinguish the original program from the changed one?
A surviving change can reveal a missing boundary assertion, a fixture that
reaches only one path, or a requirement that deserves clarification.

Inspect what the tool actually changed. Some mutations do not alter observable
behaviour for any supported input. Such mutants can arise in expressions that
are already equivalent under the application's constraints. Tools may also
report timeouts, build errors and code without coverage. Record each outcome
separately so a reader can distinguish a useful test result from an
infrastructure or build problem.

## Why it matters

A test can execute a line and still leave its crucial decision unchecked.
Imagine a reservation limit that permits a quantity equal to the maximum. A test
might exercise the route with a quantity well below the threshold and another
test well above it. Both tests can pass if someone changes the comparison to
reject the exact boundary. A mutation that changes the comparison can expose the
missing case.

The same applies when the suite checks the wrong effect. A response test can
confirm that a notification request returns success while leaving the chosen
destination unverified. A mutant removes the destination selection or changes a
fallback. If the test suite still passes, read the application contract and
decide whether it was supposed to prove delivery to a particular destination.
Improve the assertion so the test proves the delivery contract.

Mutations also give feedback about test scope and cost. A large suite that runs
slowly for every small change may be difficult to use routinely. Apply mutation
testing first where failures would affect important choices. Check the rules and
selected tests around a pricing boundary, ordering rule or error contract. Keep
the scope clear so a person investigating a result knows which behaviour was
exercised and which code did not enter that run.

## What it looks like in practice

Take a function that accepts a count within an agreed range. Give it ordinary
values, then add tests for the lower and upper boundaries and just outside each
one. Run the mutation tool and inspect a surviving change. If it removed one
boundary check, write the test in terms of the expected user result. Include the
correct classification or error details at the boundary. Re-run the original
suite and the selected mutation to see whether the new evidence distinguishes
the two versions.

Also review mutants that change no observable behaviour. Suppose two internal
branches produce the same result for every supported input because of a
validated earlier condition. The surviving change may identify redundant code or
a test with unnecessarily wide input assumptions. Discuss the intended contract,
and record a decision grounded in that behaviour. Avoid adding assertions about
irrelevant internal operations just to change the reported score.

Engineer's
[Go, SQL and browser test-suite case study](/portfolio/built-a-test-suite-across-go-sql-and-browser/)
records use of Stryker mutation testing in the suite. It also describes separate
coverage tools, property-based inputs and browser tests. The illustrative
boundary case gives a way to use that feedback in a review. Start with a
behaviour people rely on, inspect a small change to it and add an assertion
whose reason another maintainer can read.

## Where it goes wrong

A mutation score can be treated as a target without reading the cases behind it.
A tool may count some timeouts as detection, or report no coverage separately. A
new rule set can change the denominator between runs. Keep the scope and tool
version in the record, and check the status of specific changes that matter.
Compare results over the same conditions before attributing a different number
to better evidence.

Another common problem is fragile tests. A mutant may be caught because a test
depends on incidental logging, call order or an implementation type a caller
never sees. Verify that the assertion describes a real obligation. When it
checks an internal decision, make the reason clear, such as protecting a
transaction boundary or preventing a second durable effect.

Confirm that the tool runs the tests matched to the mutated code. A selection
optimisation can shorten execution by choosing tests that touched a particular
line during coverage collection. If important behaviour goes through generated
code, an integration process may see a different source file from the one the
mutator selected. Start with a small change whose consequence is known and prove
that the fixture runs against the altered artefact.

Re-run a surprising result in a clean environment. A test that depends on state
carried across runs may appear to detect a change when it actually fails on a
leftover file or database row. Isolating mutations and testing the original
baseline give the team a clearer signal about why a particular case failed.

Finally, plan the execution cost. Run a small set routinely when it fits the
team's change cycle, and use broader runs where they can finish with useful
feedback. Give failures a readable location and a relevant command. A mutation
exercise helps when it ends with an explanation of the behaviour that was
missing from the suite and a focused test that now protects it.

## Further reading

- [PIT mutation testing](https://pitest.org/). Introduces JVM mutation testing
  and its feedback.
- [Stryker mutant states and metrics](https://stryker-mutator.io/docs/mutation-testing-elements/mutant-states-and-metrics/).
  Explains how runs classify changes and calculate results.
- [PIT mutation testing concepts](https://pitest.org/quickstart/basic_concepts/).
  Discusses coverage, equivalent mutations and interpretation.

<https://engineer.company/glossary/mutation-testing/>
