Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Pact is a contract testing tool. Contract testing is a way to ensure that services (such as an API provider and a client) can communicate with each other. Without contract testing, the only way to know that services can communicate is by using expensive and brittle integration tests.

Contract testing is immediately applicable anywhere where you have two services that need to communicate - such as an API client and a web front-end. Although a single client and a single service is a common use case, contract testing really shines in an environment with many services (as is common for a microservice architecture). Having well-formed contract tests makes it easy for developers to avoid version hell. Contract testing is the killer app for microservice development and deployment.

Pact is a consumer-driven contract testing tool. This means the contract is written as part of the consumer tests. A major advantage of this pattern is that only parts of the communication that are actually used by the consumer(s) get tested. This in turn means that any provider behaviour not used by current consumers is free to change without breaking tests.

Consumer testing

Consumer Pact tests operate on each interaction described earlier to say “assuming the provider returns the expected response for this request, does the consumer code correctly generate the request and handle the expected response?”.

Each interaction is tested using the pact framework, driven by the unit test framework inside the consumer codebase:

Image Added

  1. Using the Pact DSL, the expected request and response are registered with the mock service.

  2. The consumer test code fires a real request to a mock provider (created by the Pact framework).

  3. The mock provider compares the actual request with the expected request, and emits the expected response if the comparison is successful.

  4. The consumer test code confirms that the response was correctly understood

Provider testing

Image Added

Example

There is a good talk which describes the real benefit of this approach.

pact-workshop-js is a good example of contract testing. It uses simple consumer with one API call on the client to provider which is written in Node.js.

Client flow:

Image Added

Provider flow:

Image Added

Pact useful links:

Official site

...