Why Consumer Driven Contract Testing

Simply put, contract testing makes it easy for supplier teams to know that the changes made are safe and that consumer teams can work without worrying about being cut under their feet. In addition, if an API change is interrupted, providers can use contract tests to find out which of their consumers are affected and when it is safe to drive the change. Contracts allow for the independence of services; Paradoxically, they can also bind service providers and consumers in undesirable ways. Regardless of the function and role of the contracts we implement in our SOA, we subject our services to a form of „hidden“ linkage that we can rarely address systematically. The lack of programmatic understanding of how a service community has accepted a contract and the absence of constraints on implementation decisions made by service providers and consumers undermine the perceived benefits of company SOA activation. In short, the company is burdened with services. Contract testing is a technique for testing an integration point by analyzing each application in isolation to ensure that the messages it sends or receives correspond to a common understanding documented in a „contract.“ We lost faith in the API and realized that we should have started development with something that would ensure that the manufacturer and consumer used the same API. The most natural approach to testing supplier-consumer interfaces is end-to-end testing. In end-to-end tests (E2E tests), real servers or containers are configured so that providers and consumers (and any other services they need as dependencies, such as databases). B) are available in a production-type execution environment. Here is the test script that allows the contract collection to test the data structure mentioned above. It uses the tv4 library, which is part of the Postman Sandbox: you can now be sure that every time the API changes, the contract tests run and the right team can be notified if the contract is broken. This article compares three testing strategies that can be used to implement integration tests, and then describes how those strategies work with some common testing issues.

In this scenario, mobile and web application teams write their own contract tests and run those tests in their own CI pipeline. The complexity of your contract testing depends on your business case. One of the added benefits of writing consumer-focused contract tests is that you can easily see when a service is getting too big or has too many dependencies by looking at how many consumer contract collections that service has. PACT is an open source CDC testing framework. It also offers a wide range of support for languages such as Ruby, Java, Scala, .NET, Javascript, Swift/Objective-C. We will go through the two main steps with code examples. Let`s take out some definitions of the path. There are two main types of actors in contract reviews, suppliers and consumers.

The provider is an application responsible for publishing an API. A provider consumer is another application that uses (consumes) this API. Consumers of a provider will always have basic expectations of the API, such as: The definition of the contract here is slightly broader than what we could normally offer when we talk about services, but from the point of view of service development, it judiciously abstracts the important forces that impact our problem area. However, the definition is not intended to be exhaustive with respect to the types of elements that a supplier contract could contain: it simply refers to a logical set of exportable business function elements that are likely to be included in a service evolution strategy. Logically, this set of candidate elements is open, but in practice, internal or external factors such as interoperability requirements or platform restrictions can limit the type of elements that a contract can contain. For example, a contract belonging to a service that matches the WS-Basic profile is unlikely to contain policy elements. Contract testing brings many benefits, but it`s not trivially easy to do it right. In particular, the details on how to properly set up contract verification in your CI can become complicated. To learn more about it, stay tuned for the next part of this blog series.

While the test is running, the pact dummy server on which the provider stubs are installed is started. Consumer requests are executed on the server and compared to the defined expectations. In case of correspondence, a contract file is generated. Whenever the Covenant documentation refers to „contractual tests“, it refers to „contractual integration tests“ as described earlier on this page. In our example, we can say that the set of assertions generated by all consumers expresses the mandatory structure of the messages to be exchanged during the period during which the assertions remain valid for their parent applications. If the provider has this set of assertions, it can ensure that each message it sends is valid for each consumer, provided that the set of assertions is valid and complete. Writing API tests in Postman is explained in detail here: www.postman.com/use-cases/api-testing-automation/ Should we still have unit tests when we talk about microservices? Yes, of course, unit testing has proven to be a reliable, fast, and less expensive approach to testing your business logic. But here it can only guarantee the functionality of your own service. If you are using the consumer microservice, unit tests cannot guarantee that the provider service will function correctly. A very similar problem is the question of the proper fit of API operations for a particular consumer. When a vendor mandates an API contract, it may not be suitable for certain consumer use cases.

When the consumer defines the contract, it can be defined in a way that better suits their use case. If, on the other hand, when using CDC tests, a consumer decides that a particular API operation is no longer required, the consumer removes that operation from the consumer tests and therefore from the contract. This results in a failed provider test and you are automatically notified by your CI when an API operation is no longer required and you can act accordingly. If you are a consumer, such a specification lets you know exactly what data you want to retrieve from the API. You can write your tests to set expectations and confirm the data your service will use. This approach has two major advantages: The first thing to keep in mind is that document schemas are only part of what a service provider has to offer consumers so that they can take advantage of their features. We call the sum of these outsourced operating points the supplier contract. The following table summarizes the characteristics of the three types of contracts described in this article: Another important issue with testing is the time it takes between starting tests and getting results, and you can address it by fixing a bug or modifying a test. The shorter this feedback time, the more productive you can be. Using a test environment to enforce an implicit contract is best when the contract review process cannot be performed during provider build (for example.B. for a public API or OSS code library. While this won`t prevent the changes from being published, it will ensure that they are highlighted as soon as possible.

Contract testing is immediately applicable wherever you have two services that need to communicate: e.B an API client and a web front end. While a single customer and service is a common use case, contract testing shines in a multi-service environment (as is often the case with a microservices architecture). Well-trained contract testing makes it easy for developers to avoid version hell. Contract testing is the killer application for developing and deploying microservices. As with the contractual structure, we have several options when it comes to communicating expectations between suppliers and consumers. Since the consumer-centric contract model is independent of implementation, we could easily convey expectations through conversations with other teams or via email if we had the proper organizational setup. If the number of expectations and/or consumers becomes too large to be managed in this way, we can consider introducing a contractual service interface and implementing it in the infrastructure of connected systems. Regardless of the mechanism, it is likely that the communication will be done out of band and before any conversation that runs the commercial functionality of the system. .

Trackbacks and pingbacks

No trackback or pingback available for this article.