Part 2 - Functional testing with service isolation using Mesh Dynamics
In Part I of the article, we covered the different types of testing for microservices, which includes contract testing, and functional testing with and without service isolation. In the second part of the article, we will discuss how Mesh Dynamics API Studio can be used for functional testing with service isolation (also known as service testing). Service tests help test the functionality of a service without requiring all of the producer services to be instantiated for running the tests.
Reviewing the main challenges for service testing while isolating the service under test from the previous article:
We want to structure a fully consumer-driven process.
The contract implicitly is represented by the result returned by the mock service. The mocks can diverge from the code unless the teams are diligent about keeping the mocks and tests up to date.
The creation and maintenance of accurate mocks for producer services does not scale unless the process of mock creation itself can be automated.
Mesh Dynamics supports a simple process to enable service testing while isolating the service under test from its producer services.
Service testing process overview
Here is an overview of the process to create service tests using API Studio. The developer setup required is as shown below:
Developer issues requests from API Studio to the service running locally in an IDE or a container.
Egress requests from the service running locally are routed back to API Studio.
API Studio forwards the request to the producer services in a dev cluster, enabling API Studio to capture the requests and corresponding responses.
Capturing the requests and corresponding responses enables automated creation of precise mocks for the producer services. No manual effort required to create the mocks.
Since mocks are auto-created, minimal effort is needed to create and maintain mocks.
Further, since mocks are tied to their corresponding traces, the mocking is accurate.
Save the traces for the test requests to a collection, and convert the collection to a test suite. This also auto-creates mocks for the producer services associated with the test.
Run the test locally with API Studio configured to mock all producer services. Or run the test in CI with the configuration changed to send egress requests to the Mesh Dynamics mock server.
The process and the setup are described in more detail in our documentation.
Service test updates
An important aspect of service testing is test maintenance (similar to maintaining any test suite). As services evolve, the tests and mocks need to be updated. From a process perspective, we recommend that updating producer mocks of a service test is done by the owner of the consumer service.
Producer service updates are typically backward compatible, i.e., the new version of the producer service should continue to work with the current version of the consumer service. There is no need for the consumers to update their service tests when the producer services are updated. The owner of the producer should ensure backward compatibility of the service updates.
Consumer service update
When a service is updated, the owner of the service should update the service tests. The process is similar to the original service test creation:
Add any necessary new test requests to the test collection or modify existing test requests.
Run the test with the producer services live in the dev cluster and validate the test results. Since mocks are auto-created, mocks that diverge are automatically updated by the re-run with live producer services.
Update the test with the new set of requests and updated mocks.
Assign a test name that indicates the API version that the test is targeted to.
With this approach, we can achieve the following:
Service tests that are entirely consumer-driven.
Functional and contract validation of services in isolation.
Low effort maintenance of the service tests.
In the next part of the article, we will go over how to use API Studio for contract testing only.
Please contact us at firstname.lastname@example.org for our documentation on integrated contract and service testing with Mesh Dynamics.