Contract Testing for Microservices

Contract testing verifies that a service provider meets the expectations of its consumers without running the full system. Each consumer defines the contract—specific API behavior it depends on. The provider validates against all consumer contracts in its CI pipeline.


Why Contract Testing


End-to-end testing is slow, brittle, and expensive for microservices. Deploying a full test environment with every service is impractical at scale. Contract testing provides rapid feedback with minimal infrastructure. A provider can verify contract compatibility in seconds, not hours.


Contract testing catches breaking changes before deployment. If a provider change would break any consumer, the contract test fails in CI. The provider team knows immediately and can adjust before the change reaches production.


Consumer Tests


Consumer tests define how the consumer uses the provider's API. A consumer test for a user service might specify: GET /users/123 should return a response with id, name, and email fields. The test records this expectation as a contract file.


Consumer tests use Pact's mock provider. The mock returns realistic responses, allowing the consumer to validate its own API usage without the real provider running.


Provider Verification


Provider tests verify that the real provider API satisfies all consumer contracts. The provider loads contract files from consumers and runs Pact verification against its actual API. Each endpoint, parameter, and response field specified in the contract is checked against the provider's actual behavior.


Provider verification runs in CI, typically before deployment. A failed verification blocks the release and notifies the provider team.


CI Integration


Publish consumer contract files to a Pact Broker after the consumer's CI passes. The Pact Broker maintains contract version history, webhook notifications, and can-i-deploy queries. Provider CI fetches the latest contracts, runs verification, and reports results back to the broker.


The can-i-deploy tool checks whether a provider version is compatible with the version of each consumer that will consume it. This prevents incompatible deployments.


Adoption


Start with one service pair. Choose a provider with multiple consumers to maximize value. Establish the Pact Broker as shared infrastructure. Train teams on writing consumer tests. Gradually expand contract coverage across the organization.