What is contract testing?
Micro services are becoming popular nowadays in the modern era and one strategy to test micro services is Contract Testing
We can understand it better with an example. If I have build a house to live. I will be needing few amenities like electricity, water etc. I will go to an electricity agency and ask for electricity. The agency will grant me electricity. We will sign a contract regarding the charges. Here, I am the consumer and electricity agency is the provider.
Here in the below diagram we can see that there are two services customer and provider. They are able to communicate with each other with the help of API. The messages which they transfer are in the form of packets. This is introduction to contract testing

If we are designing a test automation strategy , we should show how to expend the effort according to the “Test Pyramid”.
If we think of the pyramid shape to come up with a fast and maintainable test suite. We should write many of small unit test.We should write more meaningful tests that test our application from end to end.

Contract tests fit in the “Service Tests” layer, as they execute quickly and don’t need to integrate to external systems to run. Hence it give you confidence that the systems you integrate are compatible with your code.
What are the types of contract testing?
Contract testing can be consumer-driven or provider-driven. In consumer – driven, the Consumers define their expectations and the Provider checks that they’re fulfilled it . In provider-driven, the Provider defines the contract, and Consumers check that they’re compatible with it.
Consumer-driven contract testing has two parts which is consumer and producer. This is where thinking about testing in microservices comes into picture. Here it ensures that the provider is compatible with the expectations that the consumer has. It has achieved by using explicit contract or by using implicit contract

Provider-driven contract testing are contract tests that are written by the maintainer of the dependency. They run as part of the maintainers pipeline and serve as executable documentation regarding how the maintainer believes their library or service.
Why consumer driven contract testing?
If we are talking about introduction to contract testing, consumer driven contract testing is the essential part.
We have already discussed above how consumer and provider are talking in terms of API. The provider is responsible for publishing an API and consumer is the one who is consuming that API.
The consumer will have a question “How does the API work?”
Benefits of Consumer driven contract testing are:
- It can be easily integrate with CI/CD pipeline
- We can see how easily providers consumed by the consumer
- We have better tools for consumer driven contract testing
What are the benefits of Contract Testing?
Benefits of Contract Testing as compared to e2e
- We considered as fast.They don’t need to talk to multiple systems.
- It is easy to maintain: you don’t need to understand the entire ecosystem to write your tests.
- It is easy to debug and fix
- We can reuse contract testing
- Each component can be independently tested.
- Contract tests run on developer machines prior to pushing code.
What are the tools available for contract testing?
There are many tools available in the market. Few of the popular tools are:
- PACT (www.pact.io) – Pact is an open-source language-agnostic consumer-driven language-agnostic framework with some helpful features. This has faster feedback time.

- Spring Cloud Contract (https://cloud.spring.io/spring-cloud-contract/) – This is a provider driven framework.

- Postman- Consumer driven contact testing can be easily tested using postman
Conclusion
Contract testing is a way to handle the testing challenges of microservices. With consumer driven testing, the consumer is in control. There are several testing tools available in the market. Postman is said to be an excellent tool as it is natural and flexible. I guess this will clear doubt regrading introduction to testing.
References
https://www.qentelli.com/thought-leadership/insights/what-contract-testing-and-why-should-you-try-it
