Integration is one of the most important aspects of a well-functioning IT environment. It is the glue that holds everything together, but testing it can be complex. Contract-based testing is a way to test integration between applications. It tells you whether two applications can communicate with each other without the need for a dedicated test environment.
What is contract-based testing?
With contract-based testing, we test entire applications in isolation. We don’t need special servers or test environments to do this. We can run these tests directly on our laptops or in a pipeline.
Contract-based testing is not just a testing method; it is primarily a way of thinking. This means it is not limited by technical details, such as the integration protocol being used. Contract-based testing therefore works with the most common protocols: REST, Kafka and SOAP.
The headaches of end-to-end testing
Testing entire applications is generally considered a task for end-to-end tests (also known as integration chain tests) in a dedicated test environment.
End-to-end environments are powerful. Almost any type of test you can imagine can be performed in an end-to-end environment. Unfortunately, end-to-end testing also comes with its fair share of headaches.
It is fragile: any change to the end-to-end environment can cause tests to fail incorrectly. It is slow, particularly when developing tests, which can take a long time. This becomes even more frustrating when multiple applications need to be synchronised before tests can pass.
Maintenance is also time-consuming, especially because different parts of our tests are spread across the entire environment.
Interestingly, all these problems are symptoms of tight coupling during testing. We intertwine our applications and teams so closely that managing tests effectively becomes almost impossible.
With contract-based testing, applications are coupled only through contracts. This significantly reduces coupling and helps us avoid these headaches.
However, contract-based testing is not necessarily a replacement for end-to-end testing. End-to-end testing is not a bad approach, but it is time-consuming and should be used sparingly.
I estimate that contract-based tests can completely replace approximately 70% to 100% of existing, time-consuming end-to-end tests. This usually leaves only a few critical end-to-end tests.
Contracts
The name contract-based testing immediately tells us that this approach relies heavily on contracts.
A contract is the documentation of an application interface that serves as the shared understanding between the interface provider and its consumers.
This shared understanding is always the single source of truth for the interface. It contains everything both sides of the integration need to understand and use the interface.
These simple principles turn the shared understanding into a formal requirements document, but not yet into a contract.
A contract adds one essential requirement: it must always be readable by both humans and machines. This allows us to use contracts to provide technical guarantees about functionality designed for people.
There are two approaches to contracts. The difference lies in who writes the contract, which has major implications for what we can and cannot do with it.
These approaches are so fundamental to contract-based testing that they have their own names.
“Contract-based testing tells you whether two applications can communicate with each other without the need for a test environment.”
Provider-driven contract-based testing
In this approach, the provider writes the provider contract.
The provider ensures that its interface implementation fully complies with the contract. The provider then shares the contract with its consumers through a central contract repository.
The consumers subsequently use the contract as part of their tests.
All consumers use the same contract, making this approach ideal for popular and publicly accessible interfaces.
A provider contract contains the definitions and documentation of the interface. It describes all technical details, such as endpoints and data structures.
It also includes information intended for people, such as descriptions and examples.
Writing a provider contract is therefore similar to writing regular interface documentation, except that it follows a standardised format.
Only the provider can modify a provider contract. A high-quality provider contract is also as specific as possible, leaving no room for assumptions.
The provider’s perspective
The first step for the provider is to write the provider contract.
The provider uses the contract to gather feedback from future consumers and as a specification for its own user stories.
The provider then builds the interface based on the contract.
Part of this development process involves testing whether the implemented interface meets the specifications defined in the contract.
This step is crucial because it allows consumers to trust the contract.
The provider must therefore be strict when performing these tests. Any discrepancy between the implemented interface and the contract must be resolved.
The final step for the provider is to publish the contract to the central contract repository.
The consumer’s perspective
Consumers wait until the provider contract becomes available in the central contract repository.
This indicates that the contract is ready for use.
They download the contract and use the information it contains to build their side of the integration. Consumers never deviate from the contract.
To test their side of the integration, consumers write tests using stubs to simulate the provider.
All stubs are then validated against the provider contract. Any discrepancies between the stubs and the contract must be resolved.
This prevents the stubs from containing major errors.
The contract is downloaded again from the central contract repository during every test run. This prevents the stubs from becoming outdated.
Advantages
The provider-driven approach is an excellent way to guarantee the technical correctness of an integration.
This may sound limited, but experience shows that most tests fall into this category.
All tests are performed in isolation, making them fast to develop and execute on both sides of the integration.
In this approach, all consumers use the same provider contract.
As a result, the approach scales extremely well as the number of consumers increases.
For the provider, there is no difference between having one consumer or one hundred consumers.
This makes the approach ideal for popular and publicly accessible interfaces.
Disadvantages
The provider-driven approach involves one-way communication from the provider to its consumers.
As a result, a provider contract can feel like a set of terms and conditions: “Accept it or leave!”
This is particularly frustrating because the provider often does not fully understand the consumers’ requirements.
Furthermore, we cannot perform functional tests across applications.
If our test requires data to flow between applications, we will need an end-to-end test.
Consumer-driven contract-based testing
In this approach, all consumers write their own consumer contracts.
These contracts allow consumers to dictate the functionality of the interface.
Consumers ensure that their applications can handle all the functionality specified in their contracts.
They then share their contracts with the provider.
The provider uses all these contracts to ensure that its interface can deliver the required functionality.
Each consumer writes its own contract, making this approach ideal for interfaces tailored to specific needs.
A consumer contract contains request-response pairs.
These take the form of: “If I send request A, you respond with response B.”
In this way, the contract dictates what the interface should look like and how it should behave.
Request-response pairs may sound familiar if you have ever written stubs. Stubs also work with request-response pairs.
This makes the consumer contract a source of stubs for the consumer and therefore a fundamental part of their tests.
The provider uses these pairs as test cases.
A consumer contract can only be modified by the consumer who wrote it.
A high-quality consumer contract contains as little as possible while still including everything the consumer needs from the interface.
This makes it a reflection of how the interface is actually used.
The consumer’s perspective
The first step for each consumer is to write a consumer contract.
The consumer uses the contract as a specification document for the provider and for its own user stories.
Some parts of the contract may need to be negotiated with the provider.
Once both parties agree, the consumer builds its side of the integration based on the contract.
The consumer then tests its side of the integration against the contract.
Any discrepancies between the implemented integration and the contract must be resolved.
The final step for the consumer is to publish the contract to the central contract repository.
The provider’s perspective
The provider waits until the consumer contracts become available in the central contract repository.
The provider downloads all contracts for the interface from the repository.
These contracts are used as specifications when building the interface. The provider never deviates from the contracts.
To test the implemented interface, the provider once again uses the consumer contracts.
Each contract contains request-response pairs.
The provider uses each pair as a black-box test.
When all tests pass, the provider can be confident that the implemented interface meets the consumers’ specifications.
To ensure all tests pass, the provider must create and maintain the necessary test dependencies, such as data, stubs and other contracts.
All contracts are downloaded again from the central contract repository during every test run.
This prevents the provider from testing against outdated contracts.
Advantages
The consumer-driven approach is an excellent way to guarantee the technical correctness of an integration.
It can also provide some assurance of functional correctness, although not everyone agrees that this is a good idea.
All tests are performed in isolation, making them fast to develop and execute on both sides of the integration.
In this approach, all consumers write their own consumer contracts.
This allows consumers to specify very clearly and precisely what the interface must do.
At the same time, the provider can use these contracts to understand exactly how its interface is being used.
Based on this feedback, the provider can make informed decisions about the interface.
Disadvantages
Consumers have considerable control over the interface.
This can create problems for the provider, and most of these problems can only be resolved through communication.
The biggest potential issue is that any consumer can cause the provider’s release pipeline to fail.
In that situation, the provider cannot deploy new code to production.
The provider must wait until the consumer responsible for the faulty contract updates it.
In this approach, every consumer writes its own contract, and the provider must create test dependencies for every test defined in those contracts.
Maintaining test dependencies for multiple consumers can require considerable effort.
As a result, the testing workload for the provider increases.
Furthermore, testing against multiple contracts written by different consumers can lead to duplicate or conflicting tests.
Finally, performing functional tests across applications is not always possible.
Even when it is possible, it can become a potential pitfall. Some argue that this type of functional testing is an anti-pattern.
Conclusion
In this article, I have provided a broad overview of contract-based testing.
It is a powerful way to test integration between applications.
By moving away from our reliance on end-to-end environments, we can write faster and less complex tests.
We achieve this by testing both sides of an integration against the same contracts.
There are two approaches to working with these contracts.
With provider-driven contract-based testing, the provider writes the contract. This approach scales extremely well as the number of consumers increases.
With consumer-driven contract-based testing, each consumer writes its own contract. This approach offers significant process-related advantages.