QA Interview Hub · Knowledge base · Quizzes · Українська

API › Contract testing in practice

Bi-directional, schema-based and message contracts; adopting it in a team

Beyond classic Pact there are bi-directional contracts (a PactFlow feature), schema-based checks and message pacts for queues. Bi-directional compares the provider's OpenAPI with the consumer's pact statically: cheaper, but a weaker guarantee, as no real request is made. A message pact checks the message shape, not the transport. Adoption starts with one critical integration, through to can-i-deploy.

API · Contract testing in practice

Contract testing: start with the whole system or with one integration?

Classic CDC works well when consumers are known and ready to write contracts. And what if the provider already has OpenAPI, the team is not ready to set up provider states, the interaction goes through a message queue, or there are simply too many consumers? The lesson goes through the options next to Pact: bi-directional contracts, a schema-first approach, fuzzing from OpenAPI and message pacts, and ends with advice on introducing it in a team.

How it works and why it is designed so

Bi-directional contract testing (a PactFlow feature from SmartBear; Pact OSS does not have it) removes the need for provider states and verification of every pact. The provider publishes its OpenAPI specification together with the results of its own tests ("the specification really matches the code"), the consumer publishes a pact with its expectations (they can be generated from mocking tools such as Mock Service Worker or Cypress), and PactFlow statically compares them for compatibility. Plus: a low entry barrier, the provider does not have to maintain states. Minus: a weaker guarantee. The correctness of the specification depends on what confirms it, and the comparison does not make a real request.

Consumer-driven or bi-directional: two legitimate options

Consumer-driven (Pact)

A stronger guarantee

A real request against the provider's real code with states. More work: states, verification in the provider's CI.

Bi-directional

A lower entry barrier

A static comparison of the provider's OpenAPI with the consumer's pact. No states needed, but the guarantee is weaker: a spec is only as true as its tests.

Figure 1. The choice depends on how much effort the team is ready to put into states and verification.

Other approaches alongside

  • Provider-driven (schema-based). When there are many consumers or they are unknown, the provider's OpenAPI is the basis: a specification linter, a breaking-change check between versions (for example oasdiff) and client generation. This complements CDC rather than competing with it.
  • Schema-based fuzzing. Tools such as Schemathesis read OpenAPI and generate hundreds of requests (boundary values, missing fields, wrong types), checking that responses match the schema and the server does not return 500. The name of the address flag depends on the major version of the tool, so read the documentation of the installed one. This finds mismatches between the specification and reality, but does not check what a specific consumer needs.
  • Asynchronous interactions. For queues and events (Kafka, SQS, RabbitMQ) Pact has message pacts: the contract describes the shape of a message. The consumer checks that its handler accepts such a payload; the producer proves in verification that its code generates a message of that shape. The transport does not take part, so the configuration of topics and the broker is checked separately.

Worked example: how to adopt it without getting stuck

A typical failed start: a team decides to describe all twenty integrations with contracts, writes three dozen pacts in a month, none of them sits in a blocking pipeline, and nobody trusts the results. The working order is the opposite: one critical integration, the full cycle up to can-i-deploy, and only then the next one.

The order of adoption

1. One critical integrationfor example frontend → orders-api2. Broker, names, versionsSHA + branches + environments3. Verification and can-i-deployin the provider's CI and before a deploy4. Only then expandthe next pairs of services
Figure 2. A full cycle on one pair beats half a cycle on twenty.

The QA role: helping to write "thin" contracts, watching that states are independent and showing the value through metrics: breaking changes caught, a smaller e2e set, time from PR to release.

Typical traps

  • An over-specified contract: exact values, extra fields, the order of elements. The provider fails with no real breakage and the team stops trusting the tests.
  • Shared fixtures and dependent provider states: the state "an order exists" leans on data from another test and verification is unstable.
  • Verification against the wrong version: the provider's main is checked, although another version runs in production. Cured by selectors and can-i-deploy --to-environment.
  • A contract without can-i-deploy: the pacts exist but nobody blocks a deploy.
  • Verification against the provider's real dependencies: that is an integration test again. Dependencies need stubbing.

Check yourself

A team has the provider's OpenAPI and no wish to maintain provider states. Which approach do you consider first and at what cost?

Answer

Bi-directional: PactFlow compares the provider's spec with the consumer's pact statically. The cost: a weaker guarantee, because the comparison makes no real request and the spec is only as true as the provider's tests confirm.

Why does a message pact not prove that a Kafka topic is configured correctly?

Answer

The contract describes only the shape of a message: the consumer checks the handler, the producer checks the generation. The transport takes no part, so topics, permissions and broker settings are checked by other tests.

In an interview. Junior: Besides Pact there are bi-directional contracts with OpenAPI, schema fuzzing and message pacts for queues. It is better to adopt it from one integration. Middle/Senior: I choose by guarantee and cost: CDC gives a real verification with states, bi-directional is cheaper but static, a schema with oasdiff suits unknown consumers. I start with one critical link and take it to can-i-deploy, and show success with metrics: breaking changes caught and fewer e2e tests.

More articles in this chapter