Skip to content
IT glossary
TestingUpdated December 3, 2026

Contract testing

Контрактное тестирование

This type of testing checks whether two components can communicate as expected—for example, whether an API accepts the requests a client sends and returns responses the client can use.

Also known as: Pact

What a contract test protects

Suppose a mobile app reads available from an inventory API and uses it to enable the Add to Cart button. The app expects a JSON boolean. If the API changes that field from true to "true", tests in both repositories may still pass: the API team updated its assertions, and the app's hand-written stub still returns a boolean. A contract test connects those two sets of assumptions so the mismatch can be caught before release.

The app is the consumer because it uses the interface. The inventory service is the provider because it implements it. A contract records the parts of an interaction that matter to compatibility: for HTTP, that might include the method and path, a response status, and the presence and types of selected fields. For an event, it might cover the payload and metadata instead.

How Pact checks both sides

Pact is a tool for contract testing, not another name for the technique. A typical Pact workflow is consumer-driven: the consumer's tests describe the interactions its code relies on.

  1. The test runs the real client code against a Pact mock server. The mock checks the request against the expected interaction and returns an example response. The consumer test checks that the client handles it, and Pact writes the interaction and its matching rules to a contract file.
  2. During provider verification, the provider prepares the required state, such as a known item being in stock. Pact sends the recorded request to a test instance of the real API and checks the response against the contract. The consumer application does not have to be running.
  3. Teams can publish contracts and verification results to a Pact Broker. A release pipeline can then check whether the versions that will run together have been verified as compatible.

Example: check what the client actually needs

GET /inventory/SKU-42

HTTP/1.1 200 OK
Content-Type: application/json

{"available":true}

If the client handles both true and false and only needs a boolean for this interaction, use a type matcher. A string will fail verification, but the test won't tie the interface to a particular stock level. If the client handles a missing SKU differently, that is a separate interaction: verifying the 200 response tells you nothing about the shape of the 404 response.

What it doesn't replace

Contract tests are useful when services or clients ship independently. They don't establish that stock counts are correct, that a message reaches its destination in production, or that a shopper can complete checkout. Keep business logic tests and a smaller set of end-to-end tests for those concerns. A mock on its own isn't proof of compatibility either: the provider has to be checked against the same expectations.

In an interview

A common question is whether adding a response field breaks a contract. Explain that it depends on the consumer and the matching rules. Pact normally permits extra fields in a response body, so adding a field the client ignores usually passes. Removing a field the client requires or changing its type should fail verification.