What "survived" tells you and what it does not#
A mutation tool changes one small thing in the code, runs the tests, and reports the change as killed if a test fails and survived if none does. mutmut's documentation lists the kind of change it makes: "Integer literals are changed by adding 1", "< is changed to <=", "break is changed to continue and vice versa", and states the aim: "the mutations should be as subtle as possible." mutmut 3.8.1 README Its operator table maps > to >= the same way. mutmut 3.8.1 mutators.py
A survivor has two possible causes. Either the tests never check the behavior the mutation changed, or the mutation changed no behavior at all. The second kind is called an equivalent mutant, and no test can kill it. The tool reports both the same way, so the report alone cannot tell you which one you have. The input you can or cannot find decides it.
Three survivors in a small module#
The module below is a constructed example: a latency check that finds the peak sample and compares it with a budget. The run used Python 3.11.18, pytest 9.2.0 and mutmut 3.8.1, with source_paths = ["src/"] in pyproject.toml. This is src/latency.py, lines 1 to 10:
def peak(samples):
best = samples[0]
for s in samples[1:]:
if s > best:
best = s
return best
def over_budget(samples, budget_ms):
return peak(samples) > budget_ms
# tests/test_latency.py
from latency import over_budget, peak
def test_peak_finds_largest():
assert peak([30, 90, 40]) == 90
def test_over_budget_when_peak_exceeds():
assert over_budget([100, 250], 200) is True
def test_within_budget():
assert over_budget([100, 120], 200) is False
All three tests pass. mutmut generated seven mutants and four were killed:
$ mutmut run
$ mutmut results
latency.x_peak__mutmut_2: survived
latency.x_peak__mutmut_4: survived
latency.x_over_budget__mutmut_2: survived
$ mutmut show latency.x_peak__mutmut_2
- best = samples[0]
+ best = samples[1]
$ mutmut show latency.x_peak__mutmut_4
- if s > best:
+ if s >= best:
$ mutmut show latency.x_over_budget__mutmut_2
- return peak(samples) > budget_ms
+ return peak(samples) >= budget_ms
(The show output is trimmed to the changed lines.) Two of the survivors use the same operator change, > to >=, one function apart.
Test the equivalence claim instead of arguing it#
The peak mutant looks harmless. When s == best, the original keeps best and the mutant replaces it with s, which holds an equal value, so the function returns the same number. That argument is the reason to suspect equivalence; it is not yet the evidence.
The evidence is a comparison of the two functions on inputs where they could differ, which for this mutant means inputs with ties. mutmut keeps a copy of the mutated module in its mutants/ directory, with the original and each mutant as separate functions (the names are mutmut internals, not a documented interface). This script (abridged; it imports mutants/src/latency.py as m) calls both on every list of length 1 to 6 drawn from the values 0 to 4:
inputs = [list(t) for n in range(1, 7) for t in itertools.product(range(5), repeat=n)]
diff = [xs for xs in inputs if m.x_peak__mutmut_orig(xs) != m.x_peak__mutmut_4(xs)]
ties = sum(len(set(xs)) < len(xs) for xs in inputs)
print(f"inputs={len(inputs)} with_ties={ties} differing={len(diff)}")
$ python -I compare.py ../proj
inputs=19530 with_ties=19205 differing=0
No input among 19,205 with ties separates them. The comparison covered integers, and the argument above extends it to any values where equal means interchangeable, which holds for the millisecond samples peak receives. It would not hold if peak were later changed to compare records by a key, because a tie would then return a different record. Write that condition into the exclusion so the next reader knows when to look again. mutmut's documented mechanism is a comment: "# pragma: no mutate: disable mutation on this single line".
if s > best: # pragma: no mutate (>= is equivalent: ties keep an equal value)
The disconfirmation: the same operator change is a missing test#
Extend the same script to the over_budget mutant with a budget of 3 and it prints:
$ python -I compare.py ../proj
inputs=19530 with_ties=19205 differing=0
over_budget budget=3 differing=4368 first=[3] orig=False mutant=True
The first difference is the simplest possible input, a single sample equal to the budget. The original says a peak of exactly 3 ms is within a 3 ms budget; the mutant says it is over. No test sends a peak equal to the budget, so neither answer is pinned down. The mutant survived because a test is missing, although the operator change is identical to the equivalent one above.
Deciding which answer is correct is a requirements question, and the test records the answer. Here the budget is the largest allowed peak, so the original is right. The remaining survivor, samples[0] to samples[1], is also a missing test: every existing input has its largest value after the first position. Two tests kill both:
def test_peak_when_first_sample_is_largest():
assert peak([90, 30, 40]) == 90
def test_peak_equal_to_budget_is_within_budget():
assert over_budget([120, 200], 200) is False
With the tests added and the pragma on line 4, a fresh run generates six mutants and kills all of them:
$ rm -rf mutants && mutmut run
$ mutmut results --all true
latency.x_peak__mutmut_1: killed
latency.x_peak__mutmut_2: killed
latency.x_peak__mutmut_3: killed
latency.x_peak__mutmut_4: killed
latency.x_over_budget__mutmut_1: killed
latency.x_over_budget__mutmut_2: killed
mutmut numbers mutants by position, so after the pragma removed one, the names shifted: x_peak__mutmut_4 in this run changes best = s to best = None, a different mutation from the one excluded. Compare survivors across runs by their diff from mutmut show, not by name.
Where equivalence claims usually go wrong#
A mutant on a boundary comparison is equivalent only when equality cannot occur or cannot change the result. Equality in measurements, prices, counts and timestamps is common, so the boundary mutant is more often a missing test than an equivalent one. A mutant on code that never runs is not equivalent either: it survives because nothing reaches it. mutmut already skips functions no test calls ("By default, mutmut will mutate only functions that are called"), and its mutate_only_covered_lines setting extends that to lines coverage.py did not record. A survivor on an unexecuted line inside a called function is a coverage question first. A mutant that only changes performance, such as break to continue in a search loop, can be behaviorally equivalent, and the README names that case as one where disabling mutation "could be relevant".
Decide from the input you found#
| What the finding says | Evidence to collect | Decision |
|---|---|---|
| Survivor on a comparison boundary | Whether equality can occur in real inputs; a comparison of original and mutant on equal values | If they differ, write a test at the boundary and state the intended result in it |
| Survivor that looks equivalent | An exhaustive or property-based comparison over the inputs that could differ, run against both functions | If nothing differs, add # pragma: no mutate with the reason and the condition that would undo it |
| Survivor on an index, constant or default | An input where that position or value matters (largest value first, empty list, single item) | Write the test with that input |
| Survivor on a line no test executes | Coverage for that line | Treat it as missing coverage, not as an equivalent mutant |
| Survivor that changes only speed or logging | Whether any caller observes the difference | Exclude it with a recorded reason, or test it if the cost matters |
A survivor is the automated form of breaking a covered line by hand, described in the guide to covered but unasserted lines. When the request for a stronger test comes from a reviewer, the guide to weak-assertion comments applies the same method.