{
    "slug": "code_coverage",
    "term": "Code Coverage",
    "category": "testing",
    "difficulty": "beginner",
    "short": "A metric measuring the percentage of code executed by a test suite — useful for identifying untested paths, not a quality guarantee.",
    "long": "Code coverage (line, branch, path, mutation) measures which code was executed during tests. PHPUnit integrates with Xdebug or PCOV to generate coverage reports. High coverage does not guarantee correctness — tests can execute code without asserting meaningful outcomes. Mutation testing (Infection PHP) goes further, introducing bugs into the code and checking whether tests fail. Rather than chasing 100% line coverage, focus coverage efforts on business logic, security-sensitive code, and complex conditionals where untested branches pose real risk.",
    "aliases": [
        "test coverage",
        "line coverage",
        "branch coverage"
    ],
    "tags": [
        "testing",
        "metrics",
        "quality"
    ],
    "misconception": "100% code coverage means the code is fully tested. Coverage measures which lines execute during tests, not whether the tests assert meaningful outcomes. A test that calls every line without asserting anything achieves 100% coverage and proves nothing.",
    "why_it_matters": "Coverage metrics show which code is exercised by tests — they reveal untested paths and give a floor for confidence. However, 100% coverage does not mean the code is correct, only that every line was executed at least once.",
    "common_mistakes": [
        "Setting 100% coverage as a target — teams write meaningless tests just to hit the number.",
        "Counting coverage on generated code, migrations, and config files — only measure what matters.",
        "Using line coverage instead of branch coverage — a line with an if can be covered without testing the false path.",
        "Treating coverage as sufficient QA — coverage measures quantity of testing, not quality."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "unit_testing",
        "test_driven_development",
        "static_analysis"
    ],
    "prerequisites": [
        "unit_testing",
        "test_coverage_types",
        "phpstan_levels"
    ],
    "refs": [
        "https://phpunit.de/documentation.html",
        "https://infection.github.io/"
    ],
    "bad_code": "// Test that covers code without testing behaviour:\npublic function testUserCreation(): void {\n    $user = new User('Alice', 'alice@example.com');\n    $this->assertInstanceOf(User::class, $user); // Just checks the object was created\n    // 100% line coverage — zero meaningful assertion about User's behaviour\n}",
    "good_code": "# phpunit.xml — require 80% line coverage on CI\n<coverage>\n  <report>\n    <clover outputFile='build/coverage.xml'/>\n    <html outputDirectory='build/coverage-html'/>\n  </report>\n</coverage>\n\n# Enforce minimum in CI\n$ phpunit --coverage-clover=coverage.xml\n$ php vendor/bin/coverage-check coverage.xml 80",
    "example_note": "100% coverage is not the goal — meaningful tests covering real behaviour are. A poorly written test can hit a line without actually asserting anything.",
    "quick_fix": "Use PHPUnit with Xdebug or PCOV for coverage — aim for 80% line coverage on business logic; use mutation testing (Infection) to verify the coverage is meaningful",
    "severity": "medium",
    "effort": "medium",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/code_coverage",
        "html_url": "https://codeclaritylab.com/glossary/code_coverage",
        "json_url": "https://codeclaritylab.com/glossary/code_coverage.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": "[Code Coverage](https://codeclaritylab.com/glossary/code_coverage) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/code_coverage"
            }
        }
    }
}