Consumer-driven contracts: where they sit in the test pyramid
Consumer-driven contracts catch mismatches between services without a running environment. The consumer describes the minimum it needs from the provider, and the provider verifies in its own CI that its code meets it. This checks the shape of communication, not business logic, so e2e and functional tests remain. It fits known consumers and a frequently changing API; for public APIs OpenAPI is used.
API · Contract testing in practice
Frontend and backend are green on their own but fail together on staging. Where do you catch it earlier?
The backend team renamed the fullName field to name, its tests are green. The frontend team changed nothing, its tests are green too, because they run on a mock with the old shape. The connection breaks on staging or in production, when a user sees an empty name in the profile. The lesson explains why such bugs live at the seam between services, how consumer-driven contracts catch them and where this kind of test sits in the pyramid.
How it works and why it is designed so
The classic answers have weak spots. e2e tests are slow, flaky and need an environment with all dependencies running. Integration tests against a real provider break on foreign data and an unavailable service. Mocks in the consumer's unit tests drift from the real API over time, and everything is green until production falls.
Contract testing closes this gap. In the consumer-driven contracts (CDC) approach the consumer describes in its own test the minimum it needs from the provider: a request and the shape of the response. That description is stored as a contract, and the provider checks in its own CI that the real code honours it. No test needs both services at once, so everything is fast and deterministic. The consumer's mock and the provider's verification rest on the same document, so they cannot drift apart.
Contracts in the test pyramid
Worked example: a renamed field, two ways
The consumer reads the fullName field from the GET /users/7 response. The contract records this as the consumer's requirement:
{
"request": { "method": "GET", "path": "/users/7" },
"response": {
"status": 200,
"body": { "fullName": "Ann Lee" }
}
}
This is a simplified fragment: a real pact file carries metadata and matchers (details in the next lessons). When the provider renames the field and opens a PR, its CI replays this request against the new code. The response holds name, not fullName, so the verification is red and the report shows the consumer and the field. Compare where the same bug is found without a contract:
Where a renamed field is found
Finds it, but late
All services are needed together. The failure shows after the merge, and the cause is not obvious: data, network or a renamed field.
Finds it in the PR
The provider verifies the consumer's pact without running its neighbours. A red PR says which field of which consumer is broken.
Limits and alternatives
Contract tests check the shape of communication, not business logic. They do not replace:
- the provider's functional tests: a contract says that
statusis a string, not that an order is paid correctly; - a small number of e2e tests on critical paths: they check configuration, network, authorisation and real data;
- an OpenAPI schema and a breaking-change check where there are many unknown consumers (a public API): CDC needs the consumer to publish its expectations.
When CDC fits. The teams know each other and work in one company (microservices, a BFF and a frontend, a mobile client and a backend), the API changes often, and e2e has become the bottleneck of a release. When it does not: one monolith, one team on both sides or unknown consumers.
The business result is easy to measure: fewer incidents caused by API changes, independent service releases without an "alignment meeting" and a shorter pipeline, where e2e stays a small set on critical paths.
Common mistakes
- Treating a contract as a replacement for e2e: configuration, network and authorisation stay unchecked.
- Introducing CDC inside one monolith with one team: overhead with no gain.
- Describing business rules in the contract: it turns into a duplicate of functional tests.
Check yourself
Why does a consumer's contract test not need a running provider?
Answer
In the test the consumer works with a mock provider that replays the contract, while the provider separately verifies the same contract against real code in its own CI. The two sides are linked by the contract file, not by a live connection.
A public API has hundreds of unknown consumers. Is CDC suitable as the main approach?
Answer
No: CDC requires every consumer to publish its expectations. Here the provider's OpenAPI schema, a breaking-change check between versions and a versioning policy work.