End to end testing
To test interfaces between a client and an API the most used technique is end-to-end testing. In end-to-end tests (E2E tests), real servers or containers are set up so that client and provider are available in a production-like runtime environment.
To execute integration tests, the client is usually triggered by providing certain input in the user interface. The consumer then calls the provider and the test asserts if the results meet the expectations defined in the contract.
Limits of E2E testing
- Slow– The feedback loop of E2E testing is really long (setup takes a while and the tests run as well).
- All services need to be up before running the tests- This kind of tests requires a clean/dedicated integration environment. It’s really hard to get one in a lot of organizations at the moment.
Consumer-Driven Contract approach as an alternative
If we take a look at the definition of an integration test : “An integration test is a test between an API provider and an API consumer that asserts that the provider returns expected responses for a set of pre-defined requests by the consumer. The set of pre-defined requests and expected responses is called a contract.”
This is the main focus of the approach : CONTRACTS.
The whole idea behind CDC is to involve consumers of the future API in the creation of the API itself. API changes will be driven by consumers.
- Producer : Service that exposes an API.
- Consumer : Service that consumes the API of the producer.
- Contract : Agreement between producer and consumer on how the API will look like.
- Consumer Driven Contracts : Approach where the consumer drives the changes of the API of the producer.
How does it work ?
- Define API contract on the consumer side– Write Unit tests that define clearly what are the interactions and behaviors expected from the provider.
- Enforce the contract on the service provider side- Unit tests on the provider side ensure that the provider implementation respect the expectations (contract) defined by the consumer.
Benefits of CDC
CDC makes your life easier :
- Make integrating and testing a service in a microservice world easier
- Drive the providers implementation through the contract.
Start by enforcing the contract without the API, then design the API accordingly : you will write less code and avoid extra features.
- Help providers make changes without being scared of accidentally breaking their consumers
- Let consumer know that the APIs they consume won’t suddenly break
- Allows consumers to develop against API definitions before the provider API has actually been developed : Design first approach
How to start?
[button color=”red” link=”https://www.agilepartner.net/en/consumer-driven-contract-testing-made-simple-with-pact-part-2/”]READ PART 2 [/button]