Topic 198 / Advanced
Contract testing between services
Chapter 12.3 · Testing Strategies
What and why
Integration tests (→ 197) prove two real services work together, but require standing up both — expensive, and it doesn't scale past a handful of service pairs. Contract testing lets a consumer service record what it expects from a provider (a contract) and lets the provider verify, in its own CI, that it still satisfies every consumer's contract — without either side ever running the other live. Builds on: → 36 (API contract-first design: OpenAPI, AsyncAPI, contract validation, deprecation).
Diagram
consumer service ──writes expectations──► contract (stored, e.g. in a shared repo/broker)
│
provider service's CI ──fetches the contract──┘
│
runs the provider against
every stored expectation
│
pass ──► safe to deploy
fail ──► a real consumer would have broken
How it works
- A consumer writes down the exact requests it sends and the responses it expects, as a contract — this captures real usage, not the provider's full theoretical API surface.
- The contract is published somewhere both sides' CI pipelines can read it.
- The provider's CI, on every change, replays every stored consumer contract against the real provider code and checks every response still matches.
- A contract-test failure means a provider change would break a specific, known consumer — caught before deploy, without ever running that consumer's full stack.
- This decouples the two services' test suites: the provider doesn't need the consumer's code to test compatibility, and the consumer doesn't need the provider running to define what it expects.
Practical example
(illustrative). 10 services, each consuming 2–3 others on average. Full integration testing of every consumer–provider pair live would require standing up combinations across much of the 10-service graph in one test environment. Contract testing instead needs only each provider's own CI, run against its consumers' stored contracts — each provider verifies against roughly 2–3 contracts in isolation, with no other service started at all, cutting the test environment down from "most of the system" to "one service plus some stored expectation files."
When to use it
- Use when: multiple services evolve independently and changing one's API risks silently breaking others.
- Avoid when: only one or two services are involved — a direct integration or end-to-end test (→ 197) is simpler and cheaper to maintain than a contract-testing pipeline.
- Failure scenarios: a contract that was never updated after the consumer's real usage changed, giving false confidence that compatibility still holds; treating a passing contract test as proof the full system works end to end — it only proves this one provider–consumer pair agrees on this one recorded interaction.
- Simpler alternative: a shared, versioned API schema (→ 36) with compatibility rules enforced at review time, for systems small enough that a human can review every change against every known consumer.
Practice
(pattern kind — implement a minimal version; 20–30 min, two small local services, any language, no lab service required). Build two tiny HTTP services — a consumer and a provider. Write a contract file recording one request/response pair the consumer relies on. Write a test that runs only the provider and replays the contract against it; confirm it passes. Change the provider's response shape and confirm the contract test fails before starting the consumer at all. Done when the contract test catches the breaking change without the consumer ever running.
Key takeaway
Contract testing verifies a provider against its consumers' real, recorded expectations without running either side's full stack — faster feedback than integration testing, at the cost of only covering what was recorded. Done when you can: write a consumer-driven contract and show a provider test that fails on a breaking change and passes on a compatible one. Used next in → 199 (load, stress, spike and soak testing, the next category this topic's unit/integration levels don't cover).
References
None — this topic makes no external claims.
Source directory