Skip to content
tezvyn:

Contract Testing: Test Interfaces, Not Integrations

Source: Wikipedia: Software testingHardHow cards are made

Contract Testing: Test Interfaces, Not Integrations

Contract testing ensures services work together without slow integration tests. It's a formal agreement where a 'consumer' defines its needs, and a 'provider' proves it can meet them.

The mental model

Think of contract testing as a legal agreement between two services, a 'consumer' and a 'provider'. The consumer dictates the exact terms: 'I will send you this request, and I expect this specific response structure back.' The provider's only job is to prove it can honor that agreement. This approach tests the interface between services without needing to test the entire integrated system at once.

How it works

The process is consumer-driven. First, the consumer's test suite generates a 'contract' file, typically a JSON document, which captures the interactions it expects to have with the provider. This contract specifies the request method, path, headers, and body, along with the expected response status code, headers, and body structure. This contract is then shared with the provider, often via a central repository called a 'pact broker'. The provider then runs tests against this contract, replaying the consumer's requests and verifying that its actual responses match the contract's expectations. If they do, the contract is fulfilled.

When to use it

Contract testing shines in a microservices architecture. It allows teams to deploy their services independently with confidence. If a provider's tests pass against all its consumers' contracts, it knows it hasn't broken any existing integrations. This replaces the need for slow, flaky, and expensive end-to-end integration test suites that require deploying multiple services just to run a test.

When not to use it

Contract testing is overkill for monolithic applications where all communication happens internally. It's also less useful for public APIs with a large, unknown number of consumers, as you can't possibly maintain a contract for every one. In that case, robust end-to-end testing and strict versioning policies are more appropriate. It's designed for testing interactions between services you control.

One canonical example

A mobile app (the consumer) needs to display a user's profile. It has a contract with the backend API (the provider) stating: 'When I send a GET request to /api/users/123, I expect a 200 OK response with a JSON body containing a string 'name' and an integer 'userId'.' The backend team can refactor their database or internal logic, but as long as their service still passes this contract test, they know they haven't broken the mobile app's profile screen.

Interview question

In a microservices architecture, what is the primary advantage of employing contract testing over traditional end-to-end integration tests?

  • a.It ensures that all dependent services are always available and running in a shared test environment.
  • b.It guarantees the internal business logic of each microservice is fully correct.
  • c.It enables teams to deploy services independently by verifying interface compatibility without full system integration.Correct
  • d.It provides comprehensive test coverage for all possible data flows across the entire distributed system.
Why?

Contract testing's main benefit is enabling independent deployment by verifying service interfaces in isolation, allowing teams to deploy with confidence without needing a fully integrated environment. End-to-end tests, in contrast, typically require all services to be deployed and running together, which contract testing aims to avoid.

Just read this? Test yourself on what you have been reading.

Read the original → en.wikipedia.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on testing — each one lists the topics its interview covers.

See open roles