liveBook · Manning

Provider-driven contract testing

As explained in Contract Testing in Action

Provider-driven contract testing is a contract-testing workflow in which the provider determines or contributes the contract expectations and compares them with consumer expectations.

A team may depend on a third-party API it cannot influence, or publish a public API without knowing every consumer. Provider-driven contract testing (PDCT), also called bi-directional contract testing, offers a contract-testing workflow for those cases. Instead of executing consumer expectations directly against provider code, it compares two published contracts: a consumer contract representing expected interactions and a provider contract representing capability, often an OpenAPI specification. section 11.0 section 11.1 section 11.4

PDCT is an alternative to consumer-driven contract testing (CDCT), not a replacement for it. It is intended to lower adoption friction by letting each side retain existing tests and tools: consumers derive contracts from test mocks or stubs, while providers publish an API specification that has already been tested. A broker performs cross-contract validation and records whether the contracts are compatible. section 11.1 section 11.1.2 section 3.7.2

A typical PDCT delivery flow is:

  • Generate and publish a consumer contract from existing consumer tests, mocks, or stubs. section 3.7.2 section 11.1
  • Generate and publish the provider contract, commonly an OpenAPI specification, with provider test results. section 3.7.2 section 11.1
  • Compare the consumer’s expected response shape with the provider’s declared response shape in PactFlow. section 11.1
  • Check the compatibility result with can-i-deploy before either side deploys changes. section 3.7.2 section 11.1
Figure 3.10 PDCT life cycle from JavaScript web application, including test mocks, through the broker to the Java API service with OpenAPI documentation, and finally using the can-i-deploy tool to verify the changes in the web application
figure

The lifecycle shows separate consumer and provider publications converging at the broker, followed by a deployment decision based on the comparison result. section 3.7.2

Web consumer and API provider

A JavaScript web application can publish a contract captured from its test mocks. Independently, a Java API service can publish tested OpenAPI documentation. PactFlow compares the two artifacts rather than running the consumer contract against the API code; the web team then uses can-i-deploy to decide whether its change is safe to release. section 3.7.2 section 11.4

Adapters support the workflow without requiring a new test framework. Consumer-side examples include Cypress, Mock Service Worker, Playwright, and WireMock; provider-side examples include Postman, Dredd, REST Assured, ReadyAPI, and SoapUI. The central trade-off is fidelity: compatibility is only as trustworthy as the uploaded consumer interactions and current provider specification. section 11.1.2 section 11.1.3

Choose PDCT after considering whether CDCT fits the project first. It can suit API-design-oriented workflows and teams constrained by third-party providers, but bi-directional testing is exclusive to PactFlow and was still a relatively new approach when introduced publicly in 2022. Keep provider specifications current and capture sufficient consumer interactions to avoid allowing breaking changes through. section 11.1.1 section 11.1.3

How Contract Testing in Action covers it

How deep it goes
Explains a complete publish-compare-deploy workflow and its limitations, but defers detailed adapter implementations to later examples.
Examples
Uses a JavaScript web application, Java API service, test mocks, OpenAPI documentation, broker comparison, and can-i-deploy.
What it assumes
Assumes basic integration-testing understanding while introducing PDCT terminology and workflow from first principles.
What you can do after
Operate/configure a two-contract publication and compatibility-check workflow before deploying consumer or provider changes.
How it connects
Connects contract testing with consumer-driven contract testing, OpenAPI, PactFlow, test mocks, API testing, and can-i-deploy.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Contract Testing in Action. The section links point to the parts of the book it draws on.