Blog

November 18, 2026 · Ihor Stefaniv · 2 min read

Contract testing for microservices: Pact explained simply

End-to-end tests across ten services are slow and fragile. Consumer-driven contracts catch breaking API changes in the provider's CI — before anything is deployed.

The problem

The orders service renames a field from total to amount. Its own tests pass. The mobile app and two other services break in production. End-to-end tests could have caught it — if they ran against the exact combination of versions, which in a microservice world they rarely do.

The idea

A contract is the list of requests a consumer makes and the response shape it relies on. The consumer writes it; the provider proves in its own pipeline that it still honors it.

Contract testing flow

  1. The consumer's test runs against a Pact mock and records the interactions → a contract file.
  2. The contract is published to a broker (Pact Broker or PactFlow).
  3. The provider's CI pulls every consumer's contract and replays it against the real provider.
  4. If the provider broke a field a consumer uses, the provider's build fails — before deploy.

What a consumer test looks like

await provider
  .given("order 42 exists")
  .uponReceiving("a request for order 42")
  .withRequest({ method: "GET", path: "/orders/42" })
  .willRespondWith({
    status: 200,
    body: { id: like(42), status: term({ matcher: "new|paid|shipped", generate: "paid" }), total: decimal(112.5) },
  });

Matchers (like, term, decimal) say "a number of this type" rather than "exactly 42" — contracts describe shape, not data.

What contracts do and don't replace

They catch They don't catch
Renamed or removed fields Business logic errors in the provider
Type changes (string → number) Performance problems
Changed status codes for agreed scenarios Infrastructure and configuration issues
Breaking changes before deploy User journeys across the UI

Keep a thin layer of end-to-end tests for critical journeys; let contracts handle compatibility between services.

"can-i-deploy"

The broker knows which versions verified which contracts. Before deploying, a pipeline asks:

pact-broker can-i-deploy --pacticipant orders-service --version $GIT_SHA --to-environment production

If any consumer in production would break, the answer is no.

Getting started

Pick one consumer–provider pair with a history of breaking changes, write contracts for its 3–5 most used endpoints, and wire verification into the provider's CI. The API fundamentals behind this are in API testing in depth.

Read next