What is contract testing?

Answer

Contract testing verifies that a service (provider) and its consumers agree on the interface — without requiring both to be running simultaneously. Most relevant in microservices. The consumer defines what it expects from the provider in a contract; the provider verifies it satisfies that contract. Two types: Consumer-driven contract testing: the consumer writes the contract (expectations); the provider runs tests against it. Most common and recommended — Pact framework implements this. Provider-driven: the provider defines the contract; consumers verify they conform. Pact: the leading consumer-driven contract testing tool. Consumer generates a pact file (interactions: request → response); provider verifies each interaction against the real code. Pact Broker: central storage for pact files with compatibility matrix. Benefits: (1) Test integrations without running all services. (2) Catch breaking API changes early (CI fails before deployment). (3) Teams work independently. (4) Faster than full integration tests. Alternative: schema-based contracts (OpenAPI, GraphQL schema checks).