Contract Testing with Pact


What is contract testing?

As you probably already know, we have a concept called the test pyramid in the software testing world. If you google this, you will find slightly different versions. Most agree that unit tests are at the bottom of the pyramid and E2E tests are at the top. But there seems to be no clear agreement on how the order of the tests should be in the middle section of the pyramid. Every source has a slightly different version. Some also have expressed the opinion that the whole concept of the pyramid is not very helpful because it’s biased in favor of unit testing. Not everybody agrees that unit testing is the best type of testing, particularly in the case of microservices where usually most of the complexity is not in the “units” but rather in the interaction between the microservices, which is somewhat not really grasped/covered by unit testing. The reason why usually the interaction between microservices is more complex than the microservices themselves is that it is recommended to scale a microservice-based architecture by increasing the number of microservices and by keeping each microservice rather simple. So as the system grows the interactions between the ever-increasing number of rather simple microservices are the part that is getting most complex.

Contract testing is a type of testing that belongs somewhere between unit testing at the bottom and E2E testing at the top. It has aspects of integration testing because we actually start the service (provider) at least partly and run tests against it and also we actually test parts of the client (consumer) against a mock service. So it really goes beyond unit testing. But it’s not a full integration/API test involving a fully launched service (provider) directly communicating with a fully launched client (consumer). There is never direct communication between the real service (provider) and the real client (consumer) in contract testing. Because the service (provider) and the client (consumer) do not have to be fully up and running for contract testing, it can normally be done in the build pipeline right after the unit tests and before having the services actually deployed.

Contract testing is especially useful for microservice architectures because this type of testing is focused on the interaction between microservices which is usually the most complex part of a microservice architecture.

to be completed