4 Essential Strategies for Testing Microservices
What are microservices?
To understand microservices architecture, you first need to zoom in and understand a single microservice.
A microservice can be defined as an independently deployable service that is designed to accomplish a very specific task. Being independent means that it has very little dependency on other external systems like data stores. A well designed microservice should be quickly built, updated and deployed. It should be independent of any other system, like a database, and should scale easily through replication.
Microservices will typically operate behind a mediation layer, which is the API gateway in most cases. The API gateway acts as a single-entry point to your microservices implementation, which often spans a large number of services. The gateway may also collect responses from multiple microservices and assemble the final response.
Microservices implementations can get complicated and API gateways abstract away the complexity for the API consumers. This is important for API testing, as APIs will need to be tested at different levels ranging from the gateway to an individual microservice.
Another important aspect is the communication between microservices. Ideally, microservices should be 100% independent with no dependency on other systems, but this hardly ever possible as microservices will need to share some basic state information, like user profiles. Often to work around the need for communication between microservices and keep them sufficiently decoupled, the communication between microservices is kept asynchronous.
[caption id="attachment_24111" align="aligncenter" width="522"] Basic representation of a microservices setup.[/caption]
Testing the functionality of microservices
A microservice will encapsulate a domain and have its own data source. Additionally, a microservice will typically implement a small feature. As a result, it needs to be tested independently and specifically for the feature it implements. If a microservice has been implemented in a container, like Docker, then it becomes easy to quickly pull an image of the microservice onto a test environment and run functional tests on a single microservice at a time. If the number of services you are testing is large you can create holistic scenarios that run on multiple microservices in one go.
Testing many services becomes easy with an API testing tool. SoapUI Pro allows you to test all the possible scenarios, store input parameters on a data store, and loop over different test cases, or loop over different microservice endpoints with different data sets – your choice.
Testing the scalability of microservices
A microservice is designed to be agile and independent, and hence it needs to be scalable. A microservice architecture should be tested for scalability at different levels. Do your microservices replicate effectively with the increase in load? What about the scaling of multiple microservices when they work collectively to accomplish a task?
For example, a widget displaying weather may have the image rendered from one microservice and the temperature from another. When the widget gets rendered thousands of time in a second, the image microservice might display an old image due to faulty caching policies even when the weather has changed and a different image needs to be displayed. LoadUI Pro, a companion tool of SoapUI Pro can help you load test services at any level of granularity, you do not need to be a load testing expert. You can also reuse your SoapUI tests as load tests with just a click.
Testing the asynchronous communication of microservices
Most API testers have built tests for APIs that work in request response pairs – that is, you send a request and get a response that you validate. However, asynchronous communication works differently.
There is no concept of request and response in asynchronous communication. Instead, the client can subscribe to a queue or a topic and get updates as they become available. The benefit of asynchronous communication is that no resources are waiting on each other, thus there is less dependency. This helps in making the microservices decoupled and independent.
How do you test an asynchronous service? The concept is still the same: you set up a scenario and verify the response of the system to the scenario. For this, you will also need a tool that can subscribe to a queue or a topic and validate the incoming messages for correctness. Asynchronous testing can get complicated as it may involve protocols like JMS and MQTT. Although it is helpful to acquaint yourself with the basics of these protocols, it is not completely necessary.
Tools like SoapUI Pro make it easy for you to create an asynchronous test scenario on JMS, MQTT and web hooks with just a few clicks – in a similar fashion to how you create a REST or SOAP test. This eliminates all the hassle of learning different technologies to get your testing completed.
Testing the microservice independently
First, check if the microservice can function on its own in isolation. Isolation testing is important and, in a non-microservice architecture, it’s almost impossible to test a service independently as such services are tightly coupled with other systems. Microservices are designed to be independent and isolated, so it makes perfect sense to go ahead and test them independently for functionality, security, and performance.
Even though the microservice is independent, it may still have weak integrations with other services. Service virtualization can assist in removing these dependencies by creating virtual services. The advantage of creating a virtual service is that the virtual services are fully in your control and you can tweak them yourself to test different scenarios. You can also continue your testing when these dependencies are not available.
Modern virtualization solutions like ServiceV Pro enable you to create and customize virtual services in minutes. Service virtualization has several additional benefits like enabling testing while other microservices are still being built.
Finally, it’s important to remember that microservice architecture delivers immense value when coupled with DevOps. Continuous testing with DevOps enable testing of APIs and microservices throughout the development and delivery cycle – this helps in finding and fixing problems early. Automation of API tests is the first step you can take towards DevOps. There are different approaches to automation like Maven and Shell scripts, but the easiest one is integrating your tests with Jenkins through a command line interface.
Share this article.