What is consumer-driven contract testing with Pact?
Answer
Consumer-driven contract testing is a testing approach where the API consumer (client) defines the contract — the expected requests and responses — and the provider (server) verifies that it meets those expectations. Pact is the most popular framework for this. The consumer writes a Pact test that defines the interactions: "when I send GET /users/1, I expect a response with status 200 and body { id: 1, name: String }". Pact generates a contract file (the "pact"). The provider then runs a verification step that plays back all consumer pacts against the real API and confirms it satisfies each one. This catches breaking changes before deployment — the provider CI fails if any consumer contract is broken. Pact Broker stores and versions pacts, enabling teams to track compatibility across versions. Unlike end-to-end tests, contract tests are fast (no running full stack), isolated, and pinpoint exactly which consumer is affected by a change.
Previous
How do you decide between REST, GraphQL, and gRPC for a new API?
Next
What are backward compatibility strategies and Postel's Law in REST API evolution?
More REST API Design Questions
View all →- Advanced What is the Richardson Maturity Model and what are its four levels?
- Advanced How do you decide between REST, GraphQL, and gRPC for a new API?
- Advanced What are backward compatibility strategies and Postel's Law in REST API evolution?
- Advanced What is event-driven REST and when do you use webhooks vs SSE vs WebSockets?
- Advanced How do you optimize REST API performance — N+1 avoidance, sparse fieldsets, and response compression?