CDC testing made simple with Pact
Last updated
Last updated
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 :
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”
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 :
Design Your API From The Contract
Write a RED test
The test will be launched and interactions will be verified based on the contract that has been defined on the consumer side.
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 :
Based on our experience with Pact here is our S.W.O.T :
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
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 ?