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.