{
    "slug": "unit_testing",
    "term": "Unit Testing",
    "category": "testing",
    "difficulty": "beginner",
    "short": "Automated tests that verify individual units of code (classes, methods) in isolation from external dependencies.",
    "long": "Unit tests isolate a single class or function, replacing real dependencies (databases, HTTP clients, file systems) with test doubles (mocks, stubs, fakes). They run fast (no I/O), pinpoint failures precisely, and are the foundation of test-driven development. PHPUnit is the standard PHP unit testing framework. Well-designed code (injected dependencies, small classes) is easy to unit test — if a class is hard to test, that's a signal its design needs improvement. Aim for high coverage of business logic, edge cases, and error paths.",
    "aliases": [
        "unit tests",
        "PHPUnit",
        "isolated testing"
    ],
    "tags": [
        "testing",
        "quality",
        "php"
    ],
    "misconception": "A unit test tests a single function or method in isolation. A unit is the smallest testable piece of behaviour — which may span multiple methods. The defining characteristic is speed and isolation from external dependencies, not strict single-method scope.",
    "why_it_matters": "Unit tests verify individual functions and methods in isolation — they run in milliseconds, pinpoint exactly which unit is broken, and provide a safety net for refactoring without fear of regression.",
    "common_mistakes": [
        "Tests that test multiple units — failing tests give no indication of which unit is broken.",
        "Tests with no assertions — they pass always and provide false confidence.",
        "Tests tightly coupled to implementation details — refactoring breaks tests even when behaviour is correct.",
        "No tests for edge cases (empty input, null, zero, max values) — bugs live in boundaries."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "test_driven_development",
        "dependency_injection",
        "code_coverage"
    ],
    "prerequisites": [
        "test_driven_development",
        "mocking_best_practices",
        "dependency_injection"
    ],
    "refs": [
        "https://phpunit.de/",
        "https://martinfowler.com/bliki/UnitTest.html"
    ],
    "bad_code": "// Test with no assertion — always passes:\npublic function testProcess(): void {\n    $service = new OrderService();\n    $service->process($order); // Called but nothing asserted\n    // PHPUnit reports: 1 test, 0 assertions — misleadingly green\n}\n\n// With assertions:\npublic function testProcess(): void {\n    $result = $service->process($order);\n    $this->assertSame('processed', $result->status);\n    $this->assertTrue($result->emailSent);\n}",
    "good_code": "use PHPUnit\\Framework\\TestCase;\n\nclass MoneyTest extends TestCase {\n    public function test_add_same_currency(): void {\n        $a = new Money(100, 'GBP');\n        $b = new Money(50, 'GBP');\n        $this->assertEquals(new Money(150, 'GBP'), $a->add($b));\n    }\n\n    public function test_add_different_currency_throws(): void {\n        $this->expectException(CurrencyMismatch::class);\n        (new Money(100, 'GBP'))->add(new Money(100, 'USD'));\n    }\n}",
    "quick_fix": "Test one unit of behaviour per test, arrange-act-assert pattern, inject all dependencies so they can be mocked — test the what not the how",
    "severity": "medium",
    "effort": "medium",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/unit_testing",
        "html_url": "https://codeclaritylab.com/glossary/unit_testing",
        "json_url": "https://codeclaritylab.com/glossary/unit_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": "[Unit Testing](https://codeclaritylab.com/glossary/unit_testing) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/unit_testing"
            }
        }
    }
}