← Workflows

阅读中文版本

Contract Test Adoption Workflow

Introduce consumer-driven contracts so service changes are validated against real consumer expectations.

advanced~75 mintestingcontractintegration

Objective

Adopt contract testing to catch breaking changes before integration.

Environment

  • A contract testing tool
  • Producer and consumer services
  • CI access for both sides

Steps

1. Identify the critical interactions

Choose the service interactions where breakage is most costly.

Expected state: A focused set of contracts is selected.

2. Write consumer expectations

Have consumers define the parts of the API they depend on.

Expected state: Contracts express real consumer needs.

3. Verify producers against contracts

Run producer tests that assert they satisfy all consumer contracts in CI.

Expected state: Producers cannot merge breaking changes.

4. Publish and version contracts

Publish contracts to a broker and version them as consumers evolve.

Expected state: Both sides track compatible versions.

Decision points

Contract tests or full integration tests?

  • Contract tests for interfaces
  • Integration tests everywhere

Use contracts for interface stability and integration tests where behaviour matters.

All consumers or critical ones?

  • Start with critical consumers
  • Every consumer at once

Start with the most critical consumers to show value quickly.

Expected outcome

Service changes are validated against real consumer expectations, preventing integration breakage.

Verification

Confirm a producer change that violates a consumer contract fails CI and passes once compatible.

Cleanup

Retire contracts for removed consumers and prune the broker.

Overview

Adopt contract testing to catch breaking changes before integration.

This workflow is reusable across teams and environments; adapt the commands to your tooling while keeping the sequence and checkpoints intact.

Environment and prerequisites

  • A contract testing tool
  • Producer and consumer services
  • CI access for both sides

Procedure

1. Identify the critical interactions

Choose the service interactions where breakage is most costly.

Expected state: A focused set of contracts is selected.

2. Write consumer expectations

Have consumers define the parts of the API they depend on.

Expected state: Contracts express real consumer needs.

3. Verify producers against contracts

Run producer tests that assert they satisfy all consumer contracts in CI.

Expected state: Producers cannot merge breaking changes.

4. Publish and version contracts

Publish contracts to a broker and version them as consumers evolve.

Expected state: Both sides track compatible versions.

# verify a producer against consumer contracts
pact-verifier --provider api --pact-broker https://broker.example.com

Verification

Confirm a producer change that violates a consumer contract fails CI and passes once compatible.

Decision points

Contract tests or full integration tests?

  • Contract tests for interfaces
  • Integration tests everywhere

Recommendation: Use contracts for interface stability and integration tests where behaviour matters.

All consumers or critical ones?

  • Start with critical consumers
  • Every consumer at once

Recommendation: Start with the most critical consumers to show value quickly.

Common failures

  • No contracts means breakage is found in production.
  • Contracts that over-specify become brittle.
  • Unversioned contracts cannot evolve.