Skip to content
SDETRole
SDETRole

API Testing interview question

What is consumer-driven contract testing and how does it differ from schema validation?

advanced 5+ yrs #contract-testing#pact#microservices#openapi

Short answer

Consumers record the interactions they actually rely on as contracts (for example with Pact); the provider replays them against its real code. Schema validation only checks a response matches a spec. Contracts catch breaking changes for real consumers before deploy, without a shared end-to-end environment.

Answer

The problem it solves

In a microservice system, service A calls service B. If B renames a field, A breaks in production. Catching that with end-to-end tests requires deploying every service together in a shared environment, which is slow, flaky and tells you about the break late. Contract testing moves that check into each service’s own pipeline.

How consumer-driven contracts work (Pact)

  1. Consumer side: the consumer team writes a unit-level test of its API client against a Pact mock server. The test declares the interaction: “given order 42 exists, when I GET /orders/42, I expect 200 with id (number) and status (string)”. Matchers describe types and formats rather than exact values.
  2. Running the test produces a pact file (JSON) listing every interaction the consumer relies on.
  3. The pact is published to a Pact Broker (self-hosted or PactFlow), tagged with the consumer version and branch.
  4. Provider side: the provider’s pipeline fetches all pacts from its consumers and replays each request against the real provider code, comparing responses. Provider states (“order 42 exists”) map to setup hooks that seed data.
  5. Before deploying either side, can-i-deploy asks the broker whether this version has verified contracts with the versions of its counterparts in the target environment.
pact-broker can-i-deploy \
  --pacticipant order-service \
  --version "$GIT_SHA" \
  --to-environment production

When a consumer publishes a changed pact, a broker webhook can trigger provider verification so the feedback is quick.

Contract testing vs schema validation

Schema / OpenAPI validation Consumer-driven contracts
Source of truth Provider’s spec Consumers’ real usage
What it checks Response matches the documented shape Provider still satisfies every consumer’s expectations
Detects Provider drifting from its own spec Changes that break a specific consumer
Unused fields Treated like any other Free to change if no consumer uses them

Schema validation is necessary but does not tell you whether a change is breaking for someone. The spec can be updated alongside a breaking change and every schema test still passes. Contracts also tell the provider exactly which fields are safe to remove.

There is also provider-driven or bi-directional contract testing, where the provider’s OpenAPI spec is compared with consumer pacts. It is easier to adopt but weaker, because the provider’s real behaviour is not replayed.

Limits and pitfalls

  • Contracts test the interface, not business logic. “Returns a status string” passes even if the status is wrong.
  • Over-specifying with exact values makes contracts brittle; use type matchers.
  • Teams need to own the process: consumers write pacts, providers keep verification green. Without that, it becomes shelfware.
  • Asynchronous messaging (Kafka events) needs message pacts, which work but need extra setup.
  • You still want a small number of end-to-end smoke tests for critical journeys and configuration issues.

What interviewers look for

  • The consumer-to-provider flow: mock, pact file, broker, provider verification.
  • A clear, non-hand-wavy difference from schema validation.
  • How it gates deploys in CI (can-i-deploy, versions, environments).
  • Honest limits and how it fits with a reduced end-to-end layer.

A strong answer covers

  • Consumer writes tests against a mock provider that generate a contract (pact file)
  • Provider verifies the contract against its real implementation, with provider states
  • Difference from schema validation: tests what consumers actually use, not the whole spec
  • Broker and can-i-deploy gating in CI with versioning
  • Limits: does not test business logic or replace a few end-to-end tests
Practise answering under time pressure

Type to search across SDETRole.

Full-text search by Pagefind · runs in your browser Open search page →