{
    "slug": "mutation_testing",
    "term": "Mutation Testing",
    "category": "testing",
    "difficulty": "advanced",
    "short": "A technique that automatically modifies source code and checks whether tests fail — surviving mutations indicate test gaps even where line coverage appears complete.",
    "long": "A mutation testing tool makes small changes (mutations) to the source code — flipping a condition, changing an operator, removing a statement — then runs the test suite. If the tests still pass, the mutation 'survived', meaning no test catches that change. Mutation score = killed mutations / total mutations. A 90% line coverage suite with 40% mutation score has poor actual test quality. Infection is the PHP mutation testing framework.",
    "aliases": [
        "mutation score",
        "Infection PHP",
        "mutant"
    ],
    "tags": [
        "testing",
        "quality",
        "tdd"
    ],
    "misconception": "100% code coverage means tests are thorough — mutation testing proves otherwise; covered code with no meaningful assertions produces surviving mutants.",
    "why_it_matters": "Mutation testing catches tests that assert nothing meaningful — they pass even when production code is wrong, providing false confidence in coverage metrics.",
    "common_mistakes": [
        "Running mutation testing on the full codebase at once — start with critical domain logic only; mutation testing is slow.",
        "Targeting 100% mutation score — some mutations are equivalent (same behaviour, different code) and should be ignored.",
        "Not interpreting surviving mutants carefully — some indicate test gaps, others are in unreachable code paths.",
        "Using mutation testing instead of good test design — it is a diagnostic tool, not a substitute for thinking about what to test."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "code_coverage",
        "test_driven_development",
        "unit_testing",
        "test_doubles"
    ],
    "prerequisites": [
        "code_coverage",
        "unit_testing",
        "phpstan_levels"
    ],
    "refs": [
        "https://infection.github.io/"
    ],
    "bad_code": "// Test with coverage but no real assertion:\npublic function testCalculate(): void {\n    $calc = new Calculator();\n    $result = $calc->add(2, 3); // Line executed — covered\n    $this->assertTrue(true);   // No assertion on result — mutation survives!\n    // Mutant: return $a - $b; — test still passes\n}",
    "good_code": "// Meaningful assertion — kills the mutant:\npublic function testCalculate(): void {\n    $calc = new Calculator();\n    $this->assertSame(5, $calc->add(2, 3));\n    $this->assertSame(-1, $calc->add(2, 3, subtract: true));\n    // Mutant: return $a - $b; now fails the first assertion\n}",
    "quick_fix": "Run Infection PHP on your unit test suite — a mutant surviving means your test didn't detect a change in behaviour; fix by adding assertions that test the actual output",
    "severity": "medium",
    "effort": "high",
    "created": "2026-03-15",
    "updated": "2026-04-19",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/mutation_testing",
        "html_url": "https://codeclaritylab.com/glossary/mutation_testing",
        "json_url": "https://codeclaritylab.com/glossary/mutation_testing.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Mutation Testing](https://codeclaritylab.com/glossary/mutation_testing) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/mutation_testing"
            }
        }
    }
}