Skip to content

How it works

The consumer records its expectations, the provider replays them, and both sides validate against the same contract in their own pipelines.

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

  1. Record on the consumer side

    Write a test that calls a mock provider. Configure the mock to emit a contract file after each run.

  2. Publish the contract

    Push the contract artifact to a registry the provider can read. Tag it with the consumer version.

  3. Verify on the provider side

    Add a step to the provider pipeline that pulls the contract and replays each request against the real code.

  4. Gate the merge

    Fail the provider build when any recorded interaction no longer matches. Treat it like any other red test.