Welcome readers on this interesting article. If you are a software
QA/SDET professional, and interested or involved in API testing, then the term “Contract
Testing” may be familiar with you. Or, you may have heard about the term “PACT”,
most famous tool for Contract Testing. Here, I am not going to include a
definition of Contract testing, neither I will explain how to use “PACT” tool
in detail, I will focus only on the important use cases where we can apply it.
Avoiding surprise chaos between teams:
If you are part of an organization who are heavily using micro-services architecture to design their reusable common business components,
then you might have encountered chaotic scenarios when one department made some
changes to a micro-service, either by mistake or to enhance it for some specific
requirement, may have run their department specific unit and integration tests,
and published the service (dev-deployment, not to production) without thoroughly
assessing its impact on other departments who are consuming that service by
using the reusable component. If the concerned team forgot to inform the other
consumers (departments), then it can severely impact the development and
testing of other departments, as they will be surprised by the unexpected
change. If for some reason, the issue slips through other teams, it could
create bigger problem in production.
So, this type of services is a prime candidate for Contract
Testing. All consumers of these type of services should mutually define a set of
consumer interactions in an open API contract, based on business requirements and user
journeys that they are expecting from the provider service and keep a single
source of truth. Ideally, this contract document should be placed under version controlling.
Integrating third party APIs or providing a consumable service to other businesses:
Let’s say, you have an online retailer website, and you want to
integrate a third-party payment gateway. In this situation, you should seek a
consumer contract from the third-party provider and ensure that you are interacting
through the predefined interactions and providing right responses to the
third-party payment gateway for their consumption. Additionally, you can create a consumer contract
and share it with the third party, so that they can ensure that they are sending
payment acknowledgements in conformance with the given contract.
Faster dev-integration testing of APIs and messages:
Contract testing (using PACT tool) once integrated in CI/CD pipeline will
significantly shorten the build time for API’s and messages (MQ/Kafka etc.), as it replays the provider
test for each service in dynamic stubs and perform conformance check against
the consumer contracts. Thereby, it identifies the integration issues faster.
Remember, leveraging contract testing should not give an excuse to avoid end-to-end
functionality testing of the services or messages completely. Rather it will
help in optimizing the end-to-end functionality testing, test designers can
only focus on the business-critical services after running a risk assessment exercise.
Conclusion:
The ultimate need of contract testing is context dependent; it is
beneficial for above use cases. These are some important considerations for
Agile/Lean teams:
- it is not a sensible decision to enforce contract testing for each service/message, rather it should be done for most critical/important services.
- training and adoption of contract testing should be done at the very beginning of the project, otherwise it will be challenging.
- Contracts generated by “PACT” tool should be reviewed by a duo of Business Analyst and a Technical SME to ensure that it specifies the consumer interactions in conformance with the business requirements. Without adequate review process, it may happen that the contract test passes but it does not conform to the business requirements.
You can now clone the below repository and start learning about how to do automated contract testing using Java, Pact, Junit and Spring Boot:
https://github.com/susnigdha1/LearnPactContractTesting
Thank you for viewing this post.