What is contract testing?
Contract testing checks whether two applications meet agreed expectations for the requests, responses, or messages they exchange, even when tested separately.
Contract testing is a technique for checking whether two applications meet agreed expectations about the data they exchange. One party, the consumer, uses a function or data provided by the other, the provider. The contract describes what each expects, such as a request and its response over an API, the interface through which programs communicate. In the consumer-driven approach, the checks follow the consumer's needs: the consumer test covers the request and the consumer's handling of a simulated response; a separate verification compares the provider's actual response with the recorded expectations. Pact, an open source project, documents the same pattern for messages, which the consumer must handle and the provider must produce as expected. The two applications do not have to run together to verify the interactions described in the contract. A passing result, however, applies only to what the contract checks: it does not prove that the entire business process works or that every possible interaction has been anticipated.
See also: Data contractA formal agreement between data producers and consumers: schema, semantics and SLAs, versioned and automatically enforced in CI.
How it works in practice
In the Pact workflow, the consumer test produces a file of expected interactions. During verification, Pact sends each request to the provider and compares the response with what the consumer expects. Required preconditions, such as an existing customer record, must be in place before verification runs. Teams can share the file and run the checks in their automated integration and release pipeline, known as CI/CD. A shared contract repository, or broker, makes sharing easier but needs maintenance, and contract testing does not always require one.
An OpenAPI description documents the HTTP interface offered by the provider and can feed testing tools. On its own it does not prove that the service follows that description or reveal which fields an individual consumer uses. Consumer-driven contracts make these field-level dependencies explicit, as Ian Robinson explains. They complement the OpenAPI description rather than automatically providing a more complete one. A data contract, by contrast, covers agreements on shared data, a separate problem from checking a specific interaction between applications.
An enterprise example
Suppose a retailer's order service requests a price from its pricing service. The orders team checks that its application can read a numeric totalPrice field from the simulated response. The pricing team then verifies the same contract against its service: if the required field is missing or has a different type, the check flags the incompatibility before release, provided it runs in the release pipeline. The test does not prove that the price is calculated correctly. As the Pact guide to functional tests explains, the provider's business logic and side effects require dedicated functional tests.
Why it matters for decision makers
Contract testing pays off when separate services must change independently and their developers know the consumers, as within one company; when an API's consumers are unknown, the consumer-driven approach fits poorly. Contract owners must update expectations and decide where to keep results and versions. A test passing on one version does not establish compatibility with every other version: the Pact Broker matrix cross-references versions verified against each other and, if deployments are also recorded, shows whether a version is compatible with those already deployed to an environment. Contract tests complement functional and end-to-end tests.
Frequently asked questions
Related terms
- Data contractA formal agreement between data producers and consumers: schema, semantics and SLAs, versioned and automatically enforced in CI.
- Test automation pyramidThe test automation pyramid guides the mix of automated checks, favoring narrowly scoped tests and pairing them with checks on integrations and workflows.
- CI/CDContinuous Integration and Continuous Delivery: every software change is tested and shipped to production automatically and frequently.
- Database-per-serviceEach microservice owns its own datastore and no other service reads it directly, only through its exposed interfaces.
If it reminds you of a system nobody dares touch anymore, that is what I put back in order.
See Software Rescue