CodePark/ questions / Contract & end-to-end tests
START PRACTICINGPRACTICE
Backend question · Backend testing › Contract & end-to-end tests

What is consumer-driven contract testing, and how does Pact work?

A contract test checks that two services agree on the API between them, without deploying them together. In the consumer-driven form, each consumer (the service that calls the API) states:

  • the requests it makes
  • the parts of the responses it relies on

The provider (the service that serves the API) must satisfy every consumer's contract.

How Pact works:
1. Consumer side. The consumer's tests run against a Pact mock server. You declare interactions: "given this provider state, this request gets this response". Your real client code calls the mock. Pact then writes a pact file.
2. Sharing. The pact file is shared, usually through a Pact Broker.
3. Provider side. Pact replays each interaction against the real, running provider. It checks that the responses match.

Each side is tested on its own, and quickly. If the provider makes a breaking change, the provider's build fails before deployment.

  • Contract tests check API compatibility without deploying the services together.
  • Consumers define the interactions they rely on.
  • Consumer tests run against a Pact mock server and produce a pact file.
  • Provider verification replays the pact against the real provider.
  • Pact files are shared through a broker, so breaking changes fail early.