{"canonical_url":"https://engineer.company/glossary/mutation-testing/","content_markdown":"# Mutation testing\n\n## What it is\n\nMutation testing makes small automated changes to an implementation and runs its\ntests against each change. The altered program is called a mutant. A rule might\nreplace an inclusive comparison with an exclusive one, invert a condition or\nremove a return value. The tool checks whether the suite reports a failure when\nthe change affects the behaviour the tests ought to protect.\n\nWhen a test fails for a mutant, the tool can say that mutant was killed. A\nsurviving mutant ran through the selected tests without producing a detected\nfailure. This gives a team a useful question about one part of its suite. Which\nrequired behaviour would distinguish the original program from the changed one?\nA surviving change can reveal a missing boundary assertion, a fixture that\nreaches only one path, or a requirement that deserves clarification.\n\nInspect what the tool actually changed. Some mutations do not alter observable\nbehaviour for any supported input. Such mutants can arise in expressions that\nare already equivalent under the application's constraints. Tools may also\nreport timeouts, build errors and code without coverage. Record each outcome\nseparately so a reader can distinguish a useful test result from an\ninfrastructure or build problem.\n\n## Why it matters\n\nA test can execute a line and still leave its crucial decision unchecked.\nImagine a reservation limit that permits a quantity equal to the maximum. A test\nmight exercise the route with a quantity well below the threshold and another\ntest well above it. Both tests can pass if someone changes the comparison to\nreject the exact boundary. A mutation that changes the comparison can expose the\nmissing case.\n\nThe same applies when the suite checks the wrong effect. A response test can\nconfirm that a notification request returns success while leaving the chosen\ndestination unverified. A mutant removes the destination selection or changes a\nfallback. If the test suite still passes, read the application contract and\ndecide whether it was supposed to prove delivery to a particular destination.\nImprove the assertion so the test proves the delivery contract.\n\nMutations also give feedback about test scope and cost. A large suite that runs\nslowly for every small change may be difficult to use routinely. Apply mutation\ntesting first where failures would affect important choices. Check the rules and\nselected tests around a pricing boundary, ordering rule or error contract. Keep\nthe scope clear so a person investigating a result knows which behaviour was\nexercised and which code did not enter that run.\n\n## What it looks like in practice\n\nTake a function that accepts a count within an agreed range. Give it ordinary\nvalues, then add tests for the lower and upper boundaries and just outside each\none. Run the mutation tool and inspect a surviving change. If it removed one\nboundary check, write the test in terms of the expected user result. Include the\ncorrect classification or error details at the boundary. Re-run the original\nsuite and the selected mutation to see whether the new evidence distinguishes\nthe two versions.\n\nAlso review mutants that change no observable behaviour. Suppose two internal\nbranches produce the same result for every supported input because of a\nvalidated earlier condition. The surviving change may identify redundant code or\na test with unnecessarily wide input assumptions. Discuss the intended contract,\nand record a decision grounded in that behaviour. Avoid adding assertions about\nirrelevant internal operations just to change the reported score.\n\nEngineer's\n[Go, SQL and browser test-suite case study](/portfolio/built-a-test-suite-across-go-sql-and-browser/)\nrecords use of Stryker mutation testing in the suite. It also describes separate\ncoverage tools, property-based inputs and browser tests. The illustrative\nboundary case gives a way to use that feedback in a review. Start with a\nbehaviour people rely on, inspect a small change to it and add an assertion\nwhose reason another maintainer can read.\n\n## Where it goes wrong\n\nA mutation score can be treated as a target without reading the cases behind it.\nA tool may count some timeouts as detection, or report no coverage separately. A\nnew rule set can change the denominator between runs. Keep the scope and tool\nversion in the record, and check the status of specific changes that matter.\nCompare results over the same conditions before attributing a different number\nto better evidence.\n\nAnother common problem is fragile tests. A mutant may be caught because a test\ndepends on incidental logging, call order or an implementation type a caller\nnever sees. Verify that the assertion describes a real obligation. When it\nchecks an internal decision, make the reason clear, such as protecting a\ntransaction boundary or preventing a second durable effect.\n\nConfirm that the tool runs the tests matched to the mutated code. A selection\noptimisation can shorten execution by choosing tests that touched a particular\nline during coverage collection. If important behaviour goes through generated\ncode, an integration process may see a different source file from the one the\nmutator selected. Start with a small change whose consequence is known and prove\nthat the fixture runs against the altered artefact.\n\nRe-run a surprising result in a clean environment. A test that depends on state\ncarried across runs may appear to detect a change when it actually fails on a\nleftover file or database row. Isolating mutations and testing the original\nbaseline give the team a clearer signal about why a particular case failed.\n\nFinally, plan the execution cost. Run a small set routinely when it fits the\nteam's change cycle, and use broader runs where they can finish with useful\nfeedback. Give failures a readable location and a relevant command. A mutation\nexercise helps when it ends with an explanation of the behaviour that was\nmissing from the suite and a focused test that now protects it.\n\n## Further reading\n\n- [PIT mutation testing](https://pitest.org/). Introduces JVM mutation testing\n  and its feedback.\n- [Stryker mutant states and metrics](https://stryker-mutator.io/docs/mutation-testing-elements/mutant-states-and-metrics/).\n  Explains how runs classify changes and calculate results.\n- [PIT mutation testing concepts](https://pitest.org/quickstart/basic_concepts/).\n  Discusses coverage, equivalent mutations and interpretation.\n\n\u003chttps://engineer.company/glossary/mutation-testing/\u003e\n","date_modified":"2026-11-25T07:18:46Z","description":"Use mutation testing to find where a test reaches a branch but misses the behaviour it should protect.","formats":{"gemtext":"https://engineer.company/glossary/mutation-testing/index.gmi","gopher":"https://engineer.company/glossary/mutation-testing/gophermap","html":"https://engineer.company/glossary/mutation-testing/","json":"https://engineer.company/glossary/mutation-testing/index.json","markdown":"https://engineer.company/glossary/mutation-testing/index.md","pdf":"https://engineer.company/glossary/mutation-testing/index.pdf"},"kind":"page","language":"en","title":"What is mutation testing?","url":"https://engineer.company/glossary/mutation-testing/","version":1}