> For the complete documentation index, see [llms.txt](https://yoan-thirion.gitbook.io/knowledge-base/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yoan-thirion.gitbook.io/knowledge-base/software-craftsmanship/testing/improve-the-design-and-testing-of-your-micro-services-through-consumer-driven-contract-tests/cdc-testing-made-simple-with-pact.md).

# CDC testing made simple with Pact

## What is Pact ?

{% hint style="success" %}
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.
{% endhint %}

Pact provides a testing button for your code, allowing you to safely confirm that your applications will work together without having to deploy the world first.

It is available on a lot of platforms and languages :

![](/files/-MBZGeBVGeLQ-rPBD7Wm)

Pact comes with its own specification regarding the format of the contracts between consumers and providers :

![](/files/-MBZGipyI_wBQFQK2FNs)

## How can we implement a new service with Pact ? <a href="#f329" id="f329"></a>

Because we use a CDC approach we start with the consumer and here is what we want as a consumer :

{% hint style="info" %}
*As a consumer, I want a Rest API that returns “Hello world v1” when I GET “api/v1.0/helloworld”*
{% endhint %}

### 1) Start with the consumer <a href="#id-15af" id="id-15af"></a>

Let’s implement a simple consumer by :

* Writing Test(s)
* Define interactions / Expectations

![](/files/-MBZGsz4C6BIEP5RthEX)

**Concretely** :

* Build your client : here we call it **consumer-js**

![](/files/-MBZH6eIBf52NvemM668)

* Describe and configure the interactions (behaviors and expectations)

![](/files/-MBZHFEpR5h2jwqJU2t1)

Once you have defined it you can run the tests. From those expectations Pact will create a contract file : a JSON file that will look like this :

### 2) Contract Driven Development <a href="#id-0d10" id="id-0d10"></a>

* Design Your **API** From The Contract
* Write a RED test

![](/files/-MBZI7X93PTObmkZSvzc)

#### **The RED test (in .NET Core)**

![](/files/-MBZIG6j4aqfr3gVTHZS)

The test will be launched and interactions will be verified based on the contract that has been defined on the consumer side.

![](/files/-MBZIWHjKMqSgquoVnVo)

#### **Make the test GREEN**

![](/files/-MBZIc3q9ZXD_RW4p4Wy)

## Features offered by Pact <a href="#f090" id="f090"></a>

In this simple example we have just checked a GET interaction with simple string matcher but you can achieve much more complex and more realistic verifications with the tool :

![](/files/-MBZIihTAsvt11EwQaYY)

## S.W\.O.T <a href="#eaa4" id="eaa4"></a>

Based on our experience with Pact here is our S.W\.O.T :

![](/files/-MBZIsDx96nSrwFBGqFs)

## Resources <a href="#abaa" id="abaa"></a>

Please find the code samples in this repository : [**https://github.com/agilepartner/pact-sandbox**](https://github.com/agilepartner/pact-sandbox)

To go further check the implementation guide in your favorite language : <https://docs.pact.io/implementation_guides>

## Conclusion <a href="#id-2692" id="id-2692"></a>

Pact is a really good tool to start a CDC approach. It is easy to use but there is one problem that needs to be solved before starting using it in enterprise :

{% hint style="danger" %}
**Where the hell do we store the contracts ?**
{% endhint %}
