API Contract Test Cross Service Workflow
Implement cross-service contract testing to verify that service integrations remain compatible during independent deployments.
Objective
Prevent integration failures with automated contract testing between services.
Environment
- Multiple services with API dependencies
- A contract testing framework
- CI pipelines for each service
Steps
1. Define contracts
For each consumer-provider pair, define the expected request and response contracts.
Expected state: Contracts are defined for all service integrations.
2. Implement consumer tests
Write consumer tests that generate pact files from the consumer expectations.
Expected state: Consumer tests generate pact files.
3. Set up provider verification
Configure provider CI to verify pacts against the provider implementation.
Expected state: Provider CI verifies contracts on every change.
4. Use a pact broker
Publish pacts to a broker so providers can fetch and verify the latest consumer expectations.
Expected state: Pacts are published and verified through a broker.
5. Test the pipeline
Make a breaking change to a provider and verify the contract test catches it.
Expected state: Contract tests catch breaking changes before deployment.
Decision points
Pact or OpenAPI-based?
- Pact for consumer-driven
- OpenAPI for provider-first
Use Pact for consumer-driven contracts; OpenAPI for provider-first with generated stubs.
Expected outcome
Cross-service contract testing is implemented and catches breaking changes in CI.
Verification
Make a breaking API change and confirm the contract test fails in CI; revert the change and confirm tests pass.
Cleanup
Remove test pacts after verification; keep the contract testing infrastructure active.
Implementation Notes
-
Define contracts — For each consumer-provider pair, define the expected request and response contracts. Expected state: Contracts are defined for all service integrations.
-
Implement consumer tests — Write consumer tests that generate pact files from the consumer expectations. Expected state: Consumer tests generate pact files.
-
Set up provider verification — Configure provider CI to verify pacts against the provider implementation. Expected state: Provider CI verifies contracts on every change.
-
Use a pact broker — Publish pacts to a broker so providers can fetch and verify the latest consumer expectations. Expected state: Pacts are published and verified through a broker.
-
Test the pipeline — Make a breaking change to a provider and verify the contract test catches it. Expected state: Contract tests catch breaking changes before deployment.
Common Pitfalls
- Pact or OpenAPI-based? Options: Pact for consumer-driven / OpenAPI for provider-first. Recommendation: Use Pact for consumer-driven contracts; OpenAPI for provider-first with generated stubs.
Quick Reference
# Execute the workflow steps as documented above
echo "Workflow completed successfully"