Contract/Pact Test

As we understand API is designed to be reusable by multiple consumers. As an API development engineer, have you ever face problems like :

  • If I change this API who am I going to affect?
  • If I remove this particular field from the payload, will it affect any consumer out there?

Contract testing might be the answer to overcome these problems.

What is Contract/Pact Test?

Overview

  • It involves two parties - the Consumer (Client) and Provider (API)
  • Contract/Pact is an agreement between Consumer and Provider.
  • Contract/Pact test is to ensure the agreement between the consumer and provider is always comply, else the test will be failed.
  • Pact Broker is repository to efficiently store and maintain all contract/pact file.

Why we need Contract/Pact Test?

  • It allows API Engineer to easily identify affected consumer when any changes are needed.
  • It increases the confident level as well for every code that engineer commits and going to deploy/release to production.

Example of how it works?

How it works

  • From the chart above, any changes on GET candidate API, engineer able to find the impacted consumer easily.
  • Example, a user story of changing “name” fields to “firstName” and new fields of “lastName”, with contract test, we will easily identify JobStreet.com and JobsDB.com will be impacted, and collaboration with the correct parties can be carried out accordingly.
  • Any behavior changes of API which returning different results will be detected as well when test is executed, thus increase the confident level of code changes without impacted the API behavior.

What we have achieved so far?

  • PHPact Framework - PHP Pact Verification Framework.
  • PACTRACT - The PACT/CONTRACT & TestSuite.xml Generator along with a deployer to dedicated Pact Broker.
  • PACTRACT_REPO - PACT/CONTRACT & TestSuite.xml Repository.

What we have learned and evolved from the Contract/Pact test?

  • Beside focusing the test between the CONSUMER and PROVIDER fully related with the interested payload only, We introduce another type of BEHAVIOURAL contract which contains the full (Or close to full) behaviour/functionality of that particular API.
  • The main objective of BEHAVIORAL contract is to create a living documentation of the API with a higher readability contents, so that everyone able to understand and review the behaviour or functionality of the API.

High level activities involved

Activities

Where are we now?

  • It is just the beginning of the journey of Contract Testing in SeekAsia, there are still a lot of improvement and fine tuning going on.
  • If you wish to know more about contract/pact testing or do you have any feedback or suggestion, feel free to contact chialun.cheng@seekasia.com