Quality Assurance

Microservices Testing – A practical approach

In an era of digital transformation, it’s imperative that the enterprises have to constantly understand and quickly accommodate the business needs of end-users to stay relevant and compete in the market. Most of the organizations had found challenges with the centralized multitier monolithic architecture as all the 3 layers are tightly coupled and the entire code is maintained in the same code base. Even the smallest of the changes require the entire application to be built and deployed. For the changes to be implemented in short span with minimal disruption, the application architecture should be flexible, scalable, data-agnostic without compromising on Performance and Security. The solution to the challenge is Microservices architecture as it offers increased modularity, making applications easier to develop, test, deploy and easy to maintain.

The picture below clarifies the structure of a monolithic architecture versus Microservices architecture.

Introduction to Microservices

Microservices are the way of breaking our application or services down into standalone independent applications that can be run on different hardware or server instances. They all talk to each other over Rest APIs and work together to provide the functionality of the application or product. This way we no longer have one single application to build and deploy instead we have several individual applications that all do small things each, but they work together and work with each other at run time to form the actual complete application.

Advantages of Microservices

  1. Deployment Flexibility
  2. Technical Flexibility
  3. Scalability
  4. Organized around Business capabilities
  5. Improved Productivity and Speed
  6. Autonomous and Cross-Functional team utilization

 

The microservice architecture does come with few disadvantages which needs to be weighed down before defining the architecture of an application. Some of the challenges could be deployment architecture complexity, service discovery, difficult Cross-domain Changes and Integration testing.

How are Microservices different to Web Services

Two of the terms that you might hear in the world of software are Microservices and web services. Both are ways of defining software products by breaking larger software products down into manageable chunks that can talk to each other. Let’s take a closer look.

Web Services

A web service is a network-accessible interface to an application functionality that helps expose the functionality of an application to another application. It is built using standard internet technologies. It’s a platform that provides the functionality to build and interact with distributed applications by sending XML messages.

Difference between Microservices and Web Services

Microservices Web Services
Microservices are  a software development architecture that structures an application as a collection of loosely coupled modules A Web Service is an application accessed over a network using a combination of protocols like HTTP, XML, SMTP or Jabber
It is an architecture style organized around business capabilities and can be included in web service It’s a service offered by an application to another application which can be accessed via World Wide Web
It can be implemented in different technologies and deployed independently of each other It’s a platform that provides the functionality to build and interact with distributed applications by sending XML messages

Major challenges of testing Microservices 

  • Availability: Since different teams may be managing their own Microservices, securing the availability of a Microservices (or, worse yet, trying to find a time when all Microservices are available at once), is tough.
  • Fragmented and holistic testing: Microservices are built to work alone, and together with other loosely coupled services. That means developers need to test every component in isolation, as well as testing everything together.
  • Knowledge gaps: Particularly with integration testing (which we will address later in this article), whoever conducts the tests will need to have a strong understanding of each service in order to write test cases effectively.

How to Test Microservices

Testing Microservices is hard. Most specifically, end-to-end testing is hard – and gets exponentially harder when new service gets introduced. Following is a pragmatic approach to bring method to the madness.

Common Microservices Testing Methods

Unit Testing

Microservices may be smaller by definition, but with unit testing, one can go even more granular. A unit test focuses on the smallest part of a testable software to ascertain whether that component works as it is intended.

Unit Testing further divided into 2 categories

1. Sociable Unit Testing:

This unit testing method tests the behavior of modules by observing changes in their state.

2. Solitary Unit Testing:

This method focuses on the interactions and collaborations between an object and its dependencies, which are replaced by test doubles.

It is worth noting that unit testing alone doesn’t provide guarantees about the behavior of the system. We need other types of testing for Microservices.

How to perform Unit Tests?

  • Test the Smallest Unit code
  • Have good code coverage. Should be at least 95%
  • Have a strong assertion
  • Use Mutation Tests

Component Testing

Once we have carried out unit testing of all functions within a Microservices, then we need to test the Microservices itself in isolation.

Typically, an application would be composed of several Microservices, so in order to test in isolation, we need to mock the other Microservices.

Component tests will also test the interaction of Microservices with its dependencies such as a database, all as one unit.

Integration Testing

After we have verified the functionality of each Microservices, then we need to test the inter-service communications. An integration test verifies the communication paths and interactions between components to detect interface defects

Service calls must be made with integration to external services, which should include error and success cases, hence, integration testing validates that the system is working together seamlessly and that the dependencies between the services are present as expected.

Contract Testing

Contract tests verify interactions at the boundary of an external service asserting that it meets the contract expected by a consuming service.

This type of testing should treat each service as a black box and all the services must be called independently and their responses must be verified.

A “contract” is how a service call (where a specific result or output is expected for certain inputs) is referred to by the consumer contract testing. Every consumer must receive the same results from a service over time, even if the service changes. There should be the flexibility to add more functionality as required to the Responses later on. However, these additions must not break the service functionality.

How to perform Contract Tests?

  • Contract meets the expectations by a consuming test
  • Consumer-Driven Contract tests

End-to-End Testing

The role of end-to-end tests is to make sure everything ties together and there are no high-level disagreements between the Microservices.

End-to-end tests verify that a system meets external requirements and achieves its goals, testing the entire system, from end to end.

The tests also verify that the entire process and user flows work correctly, including all service and DB integration. Thorough testing of operations that affect multiple services ensures that the system works together as a whole and satisfies all requirements.

How to perform End-to-end Tests?

  • Test Crucial workflows, not every detail
  • Use APIs instead of the GUI for most tests
  • Focus on personas and user journeys

Tools/Technologies – Testing Microservices

List of tools that have benefited during Microservices testing

  1. Unit Tests: Junit5, Assertions on AssertJ
  2. Component Tests: Docker Containers
  3. Integration Tests: Cucumber, Docker containers
  4. Contract Test: Pact Test
  5. End to End Tests: Test Rail, Cucumber

Conclusion

Microservices or Microservices architecture (MSA) has been considered as an asset, especially, in the current phase of Digital Transformation. As applications get complex, Microservices architecture breaks down massive applications into smaller fragments for better manageability and performance. Testing and verification of these systems are significantly more nuanced and complex than testing a traditional monolithic application but a well thought out strategy and expertise can help in achieving all the benefits it promises.

About The Author