Contract testing is an essential technique for ensuring reliable communication between distributed systems. It involves testing the interactions between the components of a system by defining a set of agreed-upon contracts that specify the expected behavior of each component. This technique helps to catch any breaking changes that may occur during the development process, reducing the risk of system failure in production.
We in general generate integration tests for our applications to check whether microservices goes hand in hand.
Most famous integration testing is the e2e testing which test all microservices as whole as they will be when in production.
- Time Consuming
- Not easy to detect faults
- Not compatible with microservices changing demands as it might cause other microservices to break.
Thus Contract Testing came into play
- Pact is a popular contract testing tool that provides a framework for implementing contract testing in a variety of languages and frameworks, including Go
- Pact works by generating mock servers that simulate the behavior of a provider system, allowing the consumer system to test its interactions with the provider and generate a contract that specifies the expected behavior of the provider
#1 Generate/update a contract:
- cd /pkg/client
- go test
#2 Run provider test to validate it against the contract:
- cd /pkg/server
- go test