• Rahul Lahiri

Part 3 - Contract-driven Microservices Development

In Part 1 of the article, we covered the different types of testing at a service level for microservices. In Part 2 of the article, we went over how Mesh Dynamics API Studio supports functional service testing with isolation from producers from the service owner’s perspective. In Part 3, we will go over how Mesh Dynamics can be used for both initial API development using a contract defined between the producer and the consumer, as well as consumer-driven contract testing of producer services.


The overall API development and testing maturity timeline looks like:


While the API is still at a specification stage, and a working version is not available, the consumers are relying on the sample contract mock provided by the producer to do the consumer API development. At this stage, the only goal is to make sure that the consumer is consuming the contract correctly.


Once the API is available in the dev cluster, the consumers can start using the live API for their functional testing. In turn, the consumer can use the functional tests to validate the contracts prior to each subsequent release.


Contract-driven API development

Contract-driven API development is most useful when the consumer is ready for the new or modified API contract, but the producer API is still under development. Once the producer API is available in the dev cluster, the consumer can use the actual API, and test for both contract and functionality.


You can use contracts to guide API development when:

  • a new API is being built, and only the API spec is available,

  • new parameters are being added to an existing API. The spec for the change is available, but the API itself is not,

  • and the consumer API is being built in parallel before the producer API is ready.

For development using the contract, the producer service owner should specify the contract, while the consumer service owner should generate the test cases to ensure that their code consumes the producer API correctly. In API Studio, the contract can be provided as a single request/response pair in a collection.


Parallel development can be achieved using the following process with Mesh Dynamics API Studio:

  1. Producer creates a collection for their service with one request per API. The response included describes the contract for each API.

  2. Producer updates the collection any time the contract is updated for any of the APIs.

  3. Consumer creates test requests that validate the use of the response schema but not the parameter values. API Studio auto-creates mocks with the provided API examples, and consumers can use the mock until the API is available.

Consumer-driven contract testing of producer API

Let’s say the producer service is P and the consumer service is C. In practice there will typically be multiple consumers (C1, C2, …).


Each consumer service (C1, C2, …) will create its own test requests for functional testing for the service. Some of the test API requests will generate egress requests to the producer service P. Any given consumer will typically utilize only a few of the fields in the producer service responses. The consumer can mark the fields that they use using the configurable comparison rule feature in the API Studio.


When producer P wants to release a new version, the developer of the producer service P can use the existing service tests from all the consumers to ensure that the producer service meets the contract for all the consumers. API Studio makes it easy to validate the contract for all the consumers using current test cases created by consumers. To do this, follow the steps below. (Note that the process outlined below requires running the test from a script.)


  1. Select all service tests associated with the application.

  2. Select the producer service as the service under test.

  3. Set the assertion configuration to validate the structure elements only, and not the values.

  4. Run the test.

The process leverages all existing tests. As tests are updated by the consumers, the tests run to validate the contract automatically updates. By using the built-in filtering by service capability in Mesh Dynamics API Studio, consumer-driven contract tests can be run automatically on each producer service in the application. By changing the assertions to validate the presence of the items, the contract tests can be automated in a scalable way. The contract tests can be run from the developer laptop with no additional resources.


Conclusion

We have outlined the following needs for microservices development, and how we support them with API Studio.

  • Consumer API development using the contracts for producer APIs, or the live services in the dev cluster

  • Consumer-driven service test development to validate contract and functionality of consumer APIs

  • Consumer-driven validation of producer API contracts using all consumer service tests before code submit

Please contact us at solutions@meshdynamics.io to learn more about integrated contract and service testing with Mesh Dynamics.


©2020 Mesh Dynamics. All Rights Reserved.

PRODUCT

RESOURCES

COMPANY

FAQ

GET IN TOUCH

Request Demo

  • LinkedIn
  • Twitter
  • YouTube