liveBook · Manning

Contract testing

As explained in Microservices Patterns, Second Edition

Testing that verifies services and their clients or collaborators conform to agreed interaction contracts without requiring them to be tested together.

Contract testing checks that a service and its clients agree on an interaction API without testing them together. It supports independent deployment while detecting changes that break client expectations or client usage that violates the contract. section 7.3.1

Testing a service in isolation also requires accurate simulations of its clients and collaborators. Simple, well-defined, stable APIs make these simulations easier to maintain; contract tests check that the service implements and uses the expected APIs. section 7.3.1

In consumer-driven contract testing:

  • Client teams provide contracts that check the service API against their clients’ expectations. section 8.1.5
  • The provider team adds those contracts to its automated test suite and runs them when the service implementation changes. section 8.1.5
  • Client-side contract tests check that clients can communicate with the service. Run checks on developer machines and in the deployment pipeline. section 7.6.2 section 8.1.5

An API change between services

The Order Service depends on the Consumer Service. A contract test can fail if a Consumer Service API change breaks what the Order Service expects, or if the Order Service uses the API in a way that violates the contract. If every client supplies contracts, a change that passes them—including removing a field no client depends on—can be treated as non-breaking. section 7.3.1 section 8.1.5

Contract tests add maintenance overhead, but avoid relying solely on complex, slow, brittle end-to-end tests for interaction checks. Schema comparison is another way to detect breaking changes; consumer-driven contracts instead check compatibility against client expectations. section 7.3.1 section 7.6.2 section 8.1.5

How Microservices Patterns, Second Edition covers it

How deep it goes
Explains provider and client checks and the consumer-provided contract workflow, while deferring detailed testing patterns.
Examples
Uses an Order Service and Consumer Service interaction and a field removal that passes all client contracts.
What you can do after
Practice consumer-driven contract testing by supplying client contracts and incorporating them into the provider’s automated checks.
How it connects
Connects contract testing to isolated testing, test doubles, end-to-end testing, and API evolution.

Also explained in

Compare how 7 books explain contract testing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Microservices Patterns, Second Edition. The section links point to the parts of the book it draws on.