Measure Assertion Strength with Seeded Mutations
Core idea
Mutation testing introduces small deliberate changes (mutants) and runs a test suite against them. A mutant is killed when a previously passing suite detects it; a surviving mutant suggests missing checks, or may be equivalent under the contract. The examples manually seed plausible changes rather than run a mutation-testing engine.
def correct(n): return n >= 5def mutant(n): return n > 5def suite(fn): return fn(6) is True and fn(4) is Falseprint(suite(correct), suite(mutant))def stronger(fn): return suite(fn) and fn(5) is Trueprint(stronger(correct), stronger(mutant))The weak suite misses the equality boundary. Adding that case kills the mutant.
Expected output:
True TrueTrue FalseRecognize a possible equivalent change
def original(n): return n * 2def changed(n): return n + nsamples = range(-3, 4)print(all(original(n)==changed(n) for n in samples))For the integer domain these expressions are equivalent; the sampled check alone is not a proof. Equivalent mutants should not be treated as missing assertions automatically.
Expected output:
TrueCommon mistake
Do not count an already failing baseline as killing a mutant. A mutation score depends on which mutants are included and how timeouts or equivalent changes are handled.
Practice
Start with a passing test for a nonnegative quantity rule, manually replace >= with > and add a zero case. If mutmut is installed in an isolated version-controlled project, configure its source and tests according to the installed version documentation, run mutmut run and inspect surviving mutants. No mutmut run is claimed here.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Mutation testing
Check your understanding of mutation testing.
What is printed?
def f(n): return n>=0def g(n): return n>0print(f(0),g(0))What is printed?
def a(n): return n+1def b(n): return n-1print(a(2)==b(2))When is a mutant killed?
What can a surviving mutant indicate?