Definition
What it is
A contract test is an automated check that pins down the expectations between two independently deployed services. The consumer side writes down what it needs: fields, types, status codes, error shapes. The provider side verifies that what it actually ships still satisfies those expectations. When both sides pass, integration is safe. When one side drifts, the test fails before the change reaches production.
The term is often confused with integration testing. An integration test boots the real provider and the real consumer together and asserts end-to-end behaviour. A contract test does not need both services running at once. It captures one side's expectations as an artifact and replays them against the other side in isolation. That isolation is the whole point: it makes the check fast, cheap, and runnable inside a single service's CI pipeline.
Why it matters
Microservices break in a predictable way. A provider team renames a field, tightens a validation rule, or changes an error code. Their unit tests still pass because their code is internally consistent. The consumer team's unit tests still pass because they mock the provider with a stale fixture. Both pipelines go green. The failure surfaces in staging or, worse, in production.
Contract tests turn a coordination problem into a versioning problem. Instead of asking two teams to talk before every deploy, the contract becomes a machine-checkable artifact both sides verify independently. The conversation moves from "did you tell them?" to "did the contract pass?".
How it works
The mechanism has two roles and one artifact. The consumer is the service making calls. The provider is the service receiving them. The contract is the shared description of every interaction the consumer depends on.
graph LR C[Consumer] -->|records expectation| CT[Contract artifact] P[Provider] -->|replays contract| CT CT -->|consumer pipeline| CP[Consumer CI pass/fail] CT -->|provider pipeline| PP[Provider CI pass/fail]
A typical flow works like this. The consumer's test suite runs against a mock whose responses are generated from the contract. Every call it makes is recorded. The recorded interactions become the contract, stored in a shared repository or registry. The provider's pipeline fetches the contract, replays each recorded request against the real implementation, and asserts that the responses still match. If the provider's response shape drifts, the provider's pipeline fails. The provider team sees the break before merging.
Setting up a consumer-driven contract
Record on the consumer side
Write a test that calls a mock provider. Configure the mock to emit a contract file after each run.
Publish the contract
Push the contract artifact to a registry the provider can read. Tag it with the consumer version.
Verify on the provider side
Add a step to the provider pipeline that pulls the contract and replays each request against the real code.
Gate the merge
Fail the provider build when any recorded interaction no longer matches. Treat it like any other red test.
Common failures
A second failure mode is over-specifying. If the contract pins every field, every header, every whitespace difference, the provider cannot refactor without breaking dozens of consumers. Contracts should pin what the consumer actually depends on, no more. A contract that is too strict is as fragile as one that is too loose.
A third failure is testing only happy paths. Real integration breakage tends to appear in error handling: a 400 becomes a 422, a retry-after header disappears, an error body loses a field. Contracts should cover the failure responses the consumer actually branches on, not just the success path.
Origin
Ian Robinson described consumer-driven contracts in a 2006 paper for ThoughtWorks, framing the contract as something the consumer dictates rather than the provider. The idea matured as microservices architecture spread. Martin Fowler and Toby Clemson documented the pattern in 2013, and tooling like Pact turned it into a testable practice.
The contract is defined by the consumer, not the provider. The provider's job is to honour it.
— Ian Robinson, *Consumer-Driven Contracts: A Service Evolution Pattern*, 2006
Evidence
FAQ
- Does a contract test replace an integration test?
No. Contract tests verify interface shape and semantics between two services in isolation. Integration tests verify end-to-end behaviour across a live environment. Contract tests reduce the number of integration tests you need, but a small end-to-end suite still catches failures that span three or more services.
- Who writes the contract?
The consumer writes it. The consumer knows which fields, status codes, and error shapes it actually depends on. The provider verifies it. If the provider wrote the contract, it would encode the provider's view of the world, which is exactly what the consumer is trying to protect against.
- What happens when the contract changes?
The consumer updates its expectations, publishes a new contract version, and the provider's pipeline verifies against the new version. Old contract versions can stay valid until the consumer stops depending on them. Versioning is what makes this a coordination tool rather than a synchronisation ritual.
- Do I need a service registry to do contract testing?
Not strictly, but at scale you do. Without a registry, contracts are files moved by hand between repositories. That works for two services. It breaks for twenty. A registry stores contracts with versions and lets providers pull the ones they need.
- Is contract testing worth it for a monolith?
Usually not. Contracts solve coordination between independently deployed services. In a monolith, all code ships together, so there is no drift to catch. The pattern earns its cost when teams deploy independently.
Connections
● broader ● narrower ● related
Broader
Related
Used in the garden
No mentions yet.
Sources
Robinson, I. (2006). Consumer-Driven Contracts: A Service Evolution Pattern. ThoughtWorks. https://martinfowler.com/articles/consumerDrivenContracts.html
Fowler, M. & Clemson, T. (2013). Consumer-Driven Contracts. martinfowler.com. https://martinfowler.com/articles/consumerDrivenContracts.html
Pact (2023). Pact documentation. https://docs.pact.io/
ThoughtWorks (2017). Technology Radar: Consumer-driven contract testing. https://www.thoughtworks.com/radar/techniques/consumer-driven-contract-testing
Etymology
Origin: compound
contract(noun): from Latin contractus, agreement between two partiestest(noun): from Old French test, a trial or examination
Semantic network
● broader ● narrower ● related
Broader
Related
Sources
{"Robinson, I. (2006). Consumer-Driven Contracts":"A Service Evolution Pattern. https:\/\/martinfowler.com\/articles\/consumerDrivenContracts.html"}
Fowler, M. & Clemson, T. (2013). Consumer-Driven Contracts. https://martinfowler.com/articles/consumerDrivenContracts.html
Pact (2023). Pact documentation. https://docs.pact.io/
ThoughtWorks (2017). Technology Radar. https://www.thoughtworks.com/radar/techniques/consumer-driven-contract-testing