Your checkout service deploys on Friday afternoon. The payments service renamed a field on Thursday, and nobody told you. Saturday morning, orders start failing, and the two teams spend the weekend arguing about whose change broke what. That is the failure contract testing microservices teams want to prevent: it catches the mismatch while both teams are still at their keyboards, not after customers hit it.
Why microservices teams keep breaking each other
Microservices let teams ship independently, but that independence has a price. Every service depends on the behaviour of services it does not own. A field gets renamed, a required value becomes optional, an error response changes shape, and the consumer finds out from a production alert. The provider team did nothing wrong by their own tests. Their unit tests passed. Their service works. It just no longer works the way the consumer expected.
Integration tests in a shared staging environment look like the answer, but they are slow, flaky, and hard to keep green when ten teams deploy on different schedules. A staging failure tells you something broke, but not which team owns the fix, and not whether the break came from a real contract change or a stale environment. Teams start ignoring the red builds, and the safety net turns into noise. That is why documenting breaking API changes matters, but documentation alone does not catch a break before it ships. You need a check that runs in CI and fails the build that introduces the mismatch.
What a contract actually is
A contract is a written agreement about how two services talk to each other. It describes the requests a consumer sends and the responses it expects back: endpoints, fields, types, status codes, and which fields are required. It says nothing about how the provider stores data or runs its business logic. The provider can rewrite its internals however it likes, as long as the responses still match the contract.
The contract lives in code, usually as a file the consumer team owns and both teams can read. When the consumer needs a new field, it updates the contract first, then the provider implements it. When the provider wants to remove a field, it checks the contract first and sees who still depends on it. The contract becomes the single place where the two teams agree on the interface, instead of that agreement living in a chat thread or someone’s memory. If the provider team wants to change something without breaking consumers, the discipline of making backward-compatible API changes pairs naturally with the contract: the contract shows exactly which fields consumers depend on.
Contract testing microservices with consumer-driven contracts
The most common pattern uses consumer-driven contracts. The consumer writes down what it needs, and the provider proves it can deliver it. Here is how a typical round works.
- The consumer team writes a contract that describes the requests it sends and the responses it expects, including example values.
- The consumer runs the contract against a mock of the provider in its own tests. If the mock matches the contract, the consumer knows its code handles the agreed responses.
- The contract file is shared with the provider, usually through a broker service or a shared repository.
- The provider runs the contract against its real service in CI. Every request in the contract is replayed, and every response is checked field by field.
- If a provider change breaks any expectation, the build fails before the change ships.
The key point is that the two sides never need to run at the same time. The consumer tests against a mock, the provider tests against the contract, and the contract file is the only thing they share. That removes the staging environment from the critical path entirely. Tools such as Pact popularised this pattern, so you will often see it called pact contract testing, but the idea matters more than the tool: any framework that lets consumers publish expectations and providers verify them does the job.
Contract testing vs integration testing and end-to-end tests
Contract testing does not replace other tests. It fills a gap they leave open. Unit tests check one service in isolation. Integration tests check a few services running together. End-to-end tests check the whole system through the front door. Contract tests check the agreement between two services, and nothing else.
| Test type | What it checks | Speed | What breaks it |
|---|---|---|---|
| Unit test | One function or class | Milliseconds | A logic error in that unit |
| Contract test | The interface between two services | Seconds | A changed field, type, or status code |
| Integration test | Several services running together | Minutes | Any failure in the chain, including environment drift |
| End-to-end test | The full user journey | Minutes to tens of minutes | Anything, which is the problem |
Contract tests run fast because they need only one real service at a time, plus a mock or a contract file. They also give a precise answer. An integration test that fails tells you something is wrong somewhere. A contract test that fails tells you the provider changed the amount field from a number to a string, and the checkout consumer still expects a number. That precision is what makes the failure actionable. For teams deciding where contract tests fit in their suite, integration testing still has its place for checking real services working together, but it is a poor tool for policing interface changes. Contract tests do that job faster and with a clearer verdict.
How provider contract verification works in CI
Provider contract verification is the step that makes the whole pattern work. Without it, contracts are just documents that drift out of date like any other documentation. The verification step runs the provider’s real service against every contract its consumers have published, and it runs on every pull request that touches the provider.
A practical setup looks like this. The provider’s CI pipeline starts the service, fetches the latest contracts from the broker, and replays each request. The broker tracks which provider version passed which consumer version, so the provider team can see, before merging, whether a change breaks any consumer that is still live. Some teams add a rule that blocks deployment when a consumer on a supported version would break. Others only warn. Either way, the break becomes visible at the pull request, where the author who made the change is still around to fix it.
Versioning still matters. A contract does not remove the need to think about compatibility; it gives you the evidence to decide. When a breaking change is genuinely needed, the versioning strategy decides how it ships, and API versioning strategies are the usual way to roll it out without stranding consumers on the old shape overnight. The contract test then checks that the old version keeps working for consumers that have not migrated yet.
Mistakes that make contracts useless
Contracts fail when teams treat them as paperwork instead of tests. The most common mistakes are easy to spot.
- Testing too much. A contract should cover the fields the consumer uses, not every field the provider returns. Overspecified contracts break on harmless changes, like a provider adding a new optional field, and teams learn to ignore the failures.
- Testing too little. A contract that only checks status codes will pass while the response body quietly changes shape. Include the fields the consumer reads, with their types.
- Letting contracts rot. If verification does not run in CI, contracts drift within weeks. A contract that nobody runs is worse than no contract, because it gives false confidence.
- One team owns everything. The consumer writes the contract because it knows what it needs. If the provider writes it, the contract describes what the provider happens to return, and the consumer’s real expectations never get captured.
- Treating a green contract test as a guarantee. A contract test proves the interface matches. It does not prove the provider returns correct data, handles load, or behaves correctly under failure. Keep your other tests.
When you can skip contract testing
Not every team needs it. If you run a single service, or a small monolith with a handful of internal modules, the overhead buys you little. The same goes for teams where one person owns both sides of an interface and deploys them together. Contracts pay off when services are owned by different teams, deploy on different schedules, or change often enough that the interface drifts without anyone noticing. A good starting point is one contract between your two most tightly coupled services. If that contract catches a real break within a month, expand to the next pair. If it sits green and quiet, you have either a stable interface or a contract that checks too little, and the verification logs will tell you which.