Microservices testing gets difficult at the boundaries between services. Unit tests prove each service on its own and end-to-end suites are slow and brittle, so teams need a check that says when one service's change will break another before either is deployed.
Startup founders and engineering leaders running several services owned by different teams will find how consumer-driven contract tests divide the work, where they sit in the pipeline and which failures should stop a build.
What is consumer-driven contract testing in microservices testing?
Consumer-driven contract testing is a microservices testing method in which each consumer records the requests it sends and the responses it relies on, and the provider's build replays them against the real service. Agnotic Technologies sets it up so a breaking API change fails the provider's pipeline before deployment, not in production.
Several services, several teams?
We set up contract tests, a broker and deploy gates in your pipelines.
Pact's documentation describes the flow: the consumer's test sends real requests to a mock provider, Pact writes each interaction to a pact file, and provider verification sends every request to the real provider and compares the actual response with the minimal expected response.
The word consumer-driven matters. The provider is tested against what its consumers actually use, not against every field its schema happens to expose, so it stays free to change anything nobody reads.
Where do contract tests fit among other microservices tests?
Contract tests sit between unit tests and end-to-end tests. They check the agreement at one boundary at a time, without starting the whole system.
| Layer | What it proves | What it cannot prove |
|---|---|---|
| Unit tests | Logic inside one service is correct | That a neighbour sends what this service expects |
| Component tests | One service works with its own database and stubs | That the stubs match the real neighbours |
| Contract tests | Requests and responses at one boundary still match | That a multi-service business flow works end to end |
| End-to-end tests | A few critical journeys work in a deployed environment | Fast feedback on every change |
With contract tests in place, the end-to-end suite can shrink to the handful of journeys that matter most to the business, which makes it faster and easier to keep green.
What does each side own in a consumer-driven contract?
The consumer team owns the expectations and the provider team owns meeting them. Neither side edits the other's tests.
- Consumer owns: the interactions it relies on, written as tests against a mock provider, and publishing the resulting contract with its version and branch.
- Provider owns: the verification job in its own pipeline, the provider state setup, and fixing or negotiating any failure.
- Both own: the conversation when a consumer needs a field the provider does not have yet, which starts as a pull request rather than a broken build.
- Platform team owns: the broker, the pipeline templates and the deploy gate, so every service follows the same rules.
Pact's documentation defines provider states as the preconditions the provider needs to generate the expected response, and lets the provider team set up that data before each interaction is replayed.
How does a contract move through the pipeline?
A contract moves from the consumer's build to a broker, from the broker into the provider's verification job, and the recorded results decide whether either side may deploy.
- Step 1, consumer build: tests run against a mock provider and write the contract.
- Step 2, publish: the contract goes to the broker, tagged with the consumer version and branch.
- Step 3, provider verification: the provider build pulls the contracts it must satisfy and replays each one.
- Step 4, results: pass or fail is published back to the broker against both versions.
- Step 5, deploy gate: before either service deploys, the pipeline asks whether that version is compatible with what already runs in the target environment.
Pact's can-i-deploy command checks that a successful verification result exists between the version about to be deployed and the versions of the applications it integrates with in the target environment. It returns exit code 1 when the answer is no, and the documentation pairs it with record-deployment after each deployment.
When should the build fail?
The build should fail when a change would break a consumer that is already deployed, and pass for changes no deployed consumer depends on. Getting that split right is what makes teams trust the tests.
| Situation | Which pipeline | Outcome |
|---|---|---|
| Provider removes a field a deployed consumer reads | Provider | Verification fails and the deploy is blocked |
| Provider adds a new optional field | Provider | Passes, no consumer is affected |
| Consumer expects a field the provider has not built | Consumer deploy | Blocked until a passing provider version is live |
| Consumer feature branch adds an expectation | Provider | Reported on the branch, main is not failed |
| Provider state missing for a new interaction | Provider | Fails with a message naming the missing state |
How do you test asynchronous messages between services?
Asynchronous messages get contracts too. The consumer describes the message it expects to receive, and the provider proves that the code which builds the message produces something that matches, without a queue running during the test.
- Contract the payload, not the transport: the topic or queue is configuration, the message shape is the agreement.
- Version the event type: a new event version is a new contract, and consumers move to it on their own schedule.
- Keep producers honest: verification calls the same function the service uses to build real messages.
How should contract tests be written so they stay useful?
Contract tests stay useful when each interaction states only what the consumer needs, using type and format matchers instead of exact values. Over-specified contracts break on harmless changes and teach teams to ignore failures.
- Match types and formats, such as an ISO date or a UUID, rather than fixed example values.
- Assert only the fields the consumer reads, and leave the rest of the response out.
- Write one interaction per behaviour the consumer depends on: found, not found, unauthorised.
- Name provider states as business facts, for example an order exists with two items, rather than as database rows.
What are the common mistakes with microservices contract testing?
The common mistakes with microservices contract testing either turn the contract into a copy of the provider's schema or skip the deploy gate, which removes the safety the tests were meant to add.
- Writing contracts on the provider side, which tests what the provider returns rather than what consumers use.
- Using exact values everywhere, so ordinary data changes break verification.
- Running verification but deploying anyway, without asking whether the versions are compatible.
- Not recording deployments, so the broker cannot tell which consumer versions are live in each environment.
- Testing the provider's business rules through contracts, when they belong in the provider's own suite.
A worked example: an orders service and two consumers
An orders service is read by a web storefront and by a fulfilment service. The storefront reads order status and total; fulfilment reads line items and the shipping address.
The orders team renames the total field. Verification fails for the storefront's contract and passes for fulfilment's, because fulfilment never read the total, and the provider build is blocked.
The orders team adds the new field beside the old one and deploys. The storefront moves to the new field and its contract changes, and only once that storefront version is live does the orders team remove the old field.
How do you add contract tests to services that already run in production?
Add them one boundary at a time, starting from the consumer side, and run provider verification in report-only mode until the existing contracts pass. Turning on the deploy gate before that point blocks every release on day one.
- Write consumer contracts for the calls the consumer makes today, taken from its client code rather than from the provider's documentation.
- Run provider verification on every provider build and publish results, without failing the build at first.
- Fix or renegotiate each failing interaction; many turn out to be fields the consumer stopped using long ago.
- Record current deployments in every environment, so the broker knows which versions are live.
- Switch verification and the deploy gate to blocking once the boundary is green, then move to the next one.
Report-only mode is not wasted time. The first verification run usually shows which assumptions between teams were never written down, and those conversations are cheaper before the gate is enforced.
What does contract testing mean for your build plan?
Contract testing adds a broker, a verification job per provider and a deploy gate per pipeline. Start with the boundary that breaks most often, prove the flow there, then roll the templates out to the other services.
- Pick a first pair of services with a history of integration breakages.
- Decide where the broker runs and who administers it.
- Add the deploy gate and deployment recording to every environment, pre-production included.
- Agree the rule for breaking changes: expand first, migrate consumers, then remove the old shape.
How does Agnotic set up microservices testing?
Agnotic writes the first consumer contracts with your teams, adds provider verification and the deploy gate to each pipeline, and sets the rules for introducing breaking changes. Your teams keep ownership of their own contracts after handover.
Contract tests that block the right deploys
Consumers state what they use. Providers prove it before release.
Related reading on Agnotic
Testing practice, pipelines and service design.


