{
    "slug": "contract_testing",
    "term": "Consumer-Driven Contract Testing",
    "category": "testing",
    "difficulty": "advanced",
    "short": "A testing approach where the consumer of an API defines a contract of what it expects, and the provider verifies it satisfies that contract — enabling independent deployment of microservices.",
    "long": "In microservices, integration tests require both services running simultaneously — slow, fragile, and hard to maintain. Consumer-Driven Contract Testing (CDC) with Pact solves this: the consumer writes tests defining what request/response it expects, generating a contract (pact file). The provider then runs its own verification against the contract without needing the consumer running. Both can deploy independently once the contract is satisfied.",
    "aliases": [
        "CDC testing",
        "Pact",
        "contract test"
    ],
    "tags": [
        "testing",
        "microservices",
        "api",
        "contracts"
    ],
    "misconception": "Contract testing replaces integration testing — it verifies API contracts are compatible, not that the full business flow works end-to-end; both are needed.",
    "why_it_matters": "Without contract testing, microservices break each other silently when APIs change — contract tests catch breaking changes before deployment, not after an incident.",
    "common_mistakes": [
        "Provider teams writing contracts rather than consumers — the whole point is the consumer defines what it needs.",
        "Not publishing contracts to a broker (Pact Broker) — contracts stored locally cannot be verified by the provider CI.",
        "Contracts that are too strict — specifying exact response body structure breaks on innocuous additions.",
        "Not running contract verification in the provider's CI — contracts only help if the provider actually checks them."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "integration_testing",
        "microservices",
        "api_design",
        "test_driven_development"
    ],
    "prerequisites": [
        "api_contract_testing",
        "integration_testing",
        "microservices"
    ],
    "refs": [
        "https://docs.pact.io/",
        "https://github.com/pact-foundation/pact-php"
    ],
    "bad_code": "// No contract testing — provider changes break consumer silently:\n// Provider team renames 'user_id' to 'id' in response\n// Consumer code: $response['user_id'] — now null\n// Discovered in production, not in CI\n// Both teams deployed independently with no coordination",
    "good_code": "// Consumer defines contract (Pact PHP):\n$builder->uponReceiving('a request for user 42')\n    ->with(['method' => 'GET', 'path' => '/users/42'])\n    ->willRespondWith([\n        'status' => 200,\n        'body' => ['user_id' => 42, 'name' => Matchers::like('Alice')]\n    ]);\n// Pact file published to broker\n// Provider CI verifies: GET /users/42 returns user_id (not id)",
    "quick_fix": "Write Pact consumer tests that define what your PHP service expects from each dependency — the provider runs these expectations as part of its own test suite",
    "severity": "medium",
    "effort": "high",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/contract_testing",
        "html_url": "https://codeclaritylab.com/glossary/contract_testing",
        "json_url": "https://codeclaritylab.com/glossary/contract_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": "[Consumer-Driven Contract Testing](https://codeclaritylab.com/glossary/contract_testing) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/contract_testing"
            }
        }
    }
}