TECH NEWS

RDV Tech Agile Partner – Consumer-Driven Contract testing made simple with Pact (Part 2/3)

By Yoan Thirion, Agile coach and Craftsman at Agile Partner, in collaboration with Wilfried Vandenberghe.

November 21, 2019

Start Consumer-Driven Contract testing with Pact!

What is pact?

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.

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:

 

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

 

How can we implement a new service with pact?

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

As a consumer, I want a Rest API that returns “Hello world v1” when I GET “api/v1.0/helloworld”

1) Start with the consumer

Let’s implement a simple consumer by:

  • Writing Test(s)
  • Define interactions / Expectations

 

Concretely :

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

 

  • Describe and configure the interactions (behaviors and expectations)

 

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

  • Design Your APIFrom The Contract
  • Write a RED test

 

The RED test (in .NET Core)

 

 

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

 

 

Make the test GREEN

 

 

Features offered by pact

 

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:

 

S.W.O.T

 

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

 

 

Resources

Please find the code samples in this repository : https://github.com/agilepartner/pact-sandbox

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

 

Conclusion

 

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:

 

Where the hell do we store the contracts?

Find answers in part 3

Want to know more about CDC?

Read our first article

 

Watch video

In the same category