A contract is the set of interactions a consumer relies on: these requests, answered with responses of this shape and these status codes. Contract testing checks each side against that contract separately — the consumer against a mock built from it, the provider against the requests recorded in it — so incompatibilities are found before deployment rather than in an end-to-end environment.
In consumer-driven contract testing (Pact is the best-known tool), each consumer's tests produce a contract file; the provider's build replays every contract against the real service and fails if any expectation breaks. In schema-based contract testing, responses are validated against the API's OpenAPI document instead.
That contract tests check behaviour. They check shape and protocol — that a field exists, has a type, comes with a status. Whether the price in that field is correct is a functional test's job. Teams that replace functional tests with contract tests ship well-formed wrong answers.
Download any API's OpenAPI document from its docs page and validate the responses you get from Try it out against it; the assertion language can also pin a contract point by point, such as body.data.0.id exists.
All glossary terms · Testing techniques · All 39 mock REST APIs
Last updated