← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

API Contract Testing

api_design Advanced

Also Known As

Pact consumer-driven contracts contract test API verification

TL;DR

Consumer-driven contract tests verify that a provider API matches what consumers expect — catching breaking changes before deployment, without end-to-end tests.

Explanation

Consumer-driven contract testing (Pact): the consumer defines a contract (the requests it makes and responses it expects), the provider verifies it can fulfil those contracts independently. Unlike integration tests, both sides run independently — no shared environment needed. The Pact Broker stores and shares contracts between teams. Benefits: catches breaking API changes in CI without a shared staging environment, enables independent service deployments, and documents the actual consumer expectations. PHP: pact-foundation/pact-php, pest-pact.

Common Misconception

End-to-end tests are better than contract tests — E2E tests require a full environment and are slow and flaky; contract tests run in isolation, are fast, and give specific failure messages about which contract was violated.

Why It Matters

A provider team changing a field name from user_email to email breaks a consumer that was never told about the change — contract tests catch this in the provider's CI pipeline before deployment.

Common Mistakes

  • Provider-driven contracts — the provider dictates what it sends, not what consumers need.
  • Not running contract tests in CI — contracts only help if violations are caught automatically.
  • Overly specific contracts — test the fields you use, not the entire response structure.
  • No Pact Broker — contracts stored locally cannot be shared between teams.

Avoid When

  • A single team owns both consumer and provider and deploys them together — integration tests are simpler.
  • The API surface changes so frequently that maintaining contracts costs more than the bugs they prevent.
  • Simple internal SDKs where the provider and consumer share the same codebase and test suite.

When To Use

  • Services owned by different teams or deployed independently — contract tests catch provider changes before they reach staging.
  • Public or partner APIs where breaking changes have downstream cost and manual coordination is slow.
  • CI pipelines between microservices to get fast feedback without spinning up the full environment.

Code Examples

💡 Note
The Pact consumer test defines exactly what fields and status codes the consumer depends on — the provider can add fields freely but cannot rename or remove what the contract specifies.
✗ Vulnerable
// No contract testing — breaking changes discovered in staging:
// Provider renames 'user_email' to 'email' in v2
// Consumer teams never notified
// Integration test catches it... in a 2-hour E2E test run
// Fix: roll back provider deployment, coordinate change
// Cost: 4 hours of engineer time
✓ Fixed
// Pact consumer test:
$builder->given('user 42 exists')
    ->uponReceiving('a request for user 42')
    ->with(['method' => 'GET', 'path' => '/users/42'])
    ->willRespondWith([
        'status' => 200,
        'body'   => ['id' => 42, 'email' => 'alice@example.com'],
    ]);

// Provider verification runs against actual implementation:
$verifier->verify(); // Fails if provider returns user_email not email
// Caught in provider's CI — before deployment
// Fix: coordinate field rename with consumer teams first

Added 16 Mar 2026
Edited 31 Mar 2026
Views 37
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 3 pings S 1 ping M 0 pings T 0 pings W 0 pings T 2 pings F 0 pings S 0 pings S 1 ping M 1 ping T 0 pings W 0 pings T 2 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 2 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Amazonbot 12 Perplexity 9 Ahrefs 3 Unknown AI 3 Google 2 Meta AI 1 ChatGPT 1 Backlinks.com 1
crawler 30 crawler_json 1 pre-tracking 1
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: High
⚡ Quick Fix
Use Pact for consumer-driven contract testing — the consumer defines what it expects, the provider verifies it satisfies those expectations without running the full stack
📦 Applies To
any web api
🔗 Prerequisites
🔍 Detection Hints
Integration tests that spin up entire stack to verify API contract; no way to detect breaking changes before deploying
Auto-detectable: ✓ Yes pact dredd openapi-validator spectral
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Medium Context: File Tests: Update

✓ schema.org compliant