Q34 of 38 · CI/CD & DevOps

How would you implement contract testing between microservices in a CI pipeline?

CI/CD & DevOpsSeniorci-cdcontract-testingpactmicroservicesapi-testing

Short answer

Short answer: Use a consumer-driven contract tool such as Pact. Consumers publish contracts to a Pact Broker when their tests run; providers verify against those contracts in their own CI. A broken contract fails the provider's build before any shared integration environment is involved.

Detail

Contract testing solves the integration testing bottleneck in microservices: you do not need both services running together to verify the interface. The consumer writes a Pact test describing the HTTP calls it makes and the responses it expects. The Pact framework records this as a contract (JSON) and publishes it to a Pact Broker.

The provider's CI fetches the contract, replays the expected requests against a running instance of itself, and verifies its responses match. If the provider renames a field or changes a response shape, the test fails in the provider's pipeline before the change reaches staging.

The deployment pipeline uses the Pact Broker's can-i-deploy CLI as a gate: it checks whether every contract the service participates in is verified and green for the target environment. A service that would break a consumer cannot be promoted.

// WHAT INTERVIEWERS LOOK FOR

Consumer-driven flow: consumer writes, provider verifies. Pact Broker as the contract store. The 'can-i-deploy' gate as the CI integration point. Why this is better than mocking the provider.