Loading…
Transcript

Pact

Mock HTTP Server

Pact File

✳✱*

3. Basket API tests define mocks and the tests are run. This generates a Pact file.

4. This Pact file is used by the Provider Order API tests.

Mock Order API

2. Mock Order API sends back the prearranged mock response.

Chaining Pact Tests

Basket API

How does the Provider know to run tests on Client changes?

1. Basket API tests make a request to the Mock Order API.

Order API

Basket API

Order API

With Pact Provider Tests!

With Pact Consumer Tests!

3. Order API independently has some contract tests. These verify that the Order API returns what other APIs (like Basket API) expect from it.

  • If you use the Pact Broker a webhook can be setup.
  • Using Swagger? Atlassian created a tool to compare Pact files to Swagger Specs. https://goo.gl/YUo4Nt

1. Basket API has Pact Consumer tests defining the relationship with Order API (requests and responses).

2. The Pact tests define this relationship using mocks.

5. Order API has one Pact Provider test.

  • Consumes the Pact file generated by the Basket API.
  • Plays back the requests against itself and verifies the real responses match the mocked ones.

Example 2: Contract Testing without Pact

Example 3: Contract Testing with Pact

An Example - My Web Store

Sharing Pact Files

✳✱*

  • Think of a Web Store of many services working together. Lets focus on two:
  • Basket API
  • Order API
  • We could test the Basket API and the Order API in three ways.
  • Integration Tests
  • Contract Tests
  • Pact Contract Tests

There are three options to share the Pact files generated by your tests:

1. By directory path. - Good if your Consumer and Provider are in the same folder.

2. S3 Bucket or similar - Every test can access all the pacts but some management overhead.

3. Pact Broker - Pact's own service for managing Pact files.

Basket API

Order API

1. The Basket API tests make requests to the Orders API.

2. Order API responds with

the information the test needs

This works... but:

  • Basket API tests need the Order API to be running.
  • What happens if it isn't available...
  • Tests fail.
  • With contract tests we can make the tests loosely dependent on each other.

Why Not Integration Tests?

QA Meet Up

Contract Tests? Pact?

Contract tests are tests created by a Provider API which verify the data objects they return for responses.

The Good

  • Trustworthy

The Bad

  • Expensive
  • Slow
  • Unreliable
  • Broad Scope of Failure

Example 1: Intergration Testing two Services

Contract Testing with Pact

Pact is a Framework for creating Contract tests which are validated by both a API Provider & the API Consumer

Thomas Shipley