Testing of Microservices – A Beginner’s Guide
What Are Microservices?
Microservice, aka Microservice architecture, is an architectural style, is an approach to developing a single application as a suite of services. Each service is defined by its characteristics some of which are:
- Running in its process.
- Communication with a lightweight mechanism often with an HTTP resource of API.
- Independently deployable by fully automated machinery.
- Using different programming languages /technologies/ DB.
- Is using different data storage technologies.
HTTP Basics for Software Testers
The microservice architectural style requires developing a single application that will work together as a suite of small services where each will run in its individual process and communicating with lightweight mechanisms such as an HTTP resource API. These services involve simple minimum centralized management, use different data storage technologies, and can be written in different programming languages. These services, built around business capabilities and can also be deployed independently by machinery that supports fully automated deployment.
- Organized around business capability,
- Automated deployment,
- Intelligence in the endpoints rather than in service bus,
- The decentralized control of languages and data.
How Are Microservices Different to SOA
Service-oriented architecture(SOA): an architectural pattern in computer software design in which application components provide services to other components via a communications protocol, typically over a network.
Microservices: a software architecture style in which complex applications are composed of small, independent processes communicating with each other using language-agnostic APIs
If Uber application were built with an SOA, their services might be:
If Uber application were built with microservices, their APIs might be more like:
SOA vs. Microservices
The typical service-oriented architecture (SOA) model usually dependents on enterprise service buses (ESBs), with microservices for faster messaging mechanisms. While SOA focuses on imperative programming language, the microservices architecture uses a programming style that focuses on a responsive-actor as its base. While service-oriented architecture (SOA) models usually have an outsized RDBMS where microservices frequently use databases such as micro-SQL or NoSQL that can be connected to conventional databases. It means, the real difference lies in the architecture methods that are used for creating an integrated set of services.
The application that is used for the microservices architecture has been created based on creating software/applications for a large number of enterprise organizations which is followed by developers. The experience of developers comes in very handy to understand what are the expectations of today’s end-users, and helps create a consistent, yet dynamic experience across a wide range of devices. Cloud-based applications that are accessible, adaptable, modular, and scalable are in high demand. This has led many developers to change their approach.
Challenges in Microservices Testing are:-
We are facing some key challenges around microservices architecture testing. The selection of the right tools is one of the elements that help us deal with the issues related to those challenges. First, let’s identify the most important elements involved in the process of microservices testing:
- Teams coordination – With many independent teams managing their own microservices where it becomes very challenging to coordinate the overall process of software development as well as testing.
- Complexity – There are many microservices that communicate with other microservices. We need to make sure that every one of them is working properly and is resistant to slow responses or failures from other microservices
- Performance – Since there are many independent microservices, it is important to test the whole architecture under traffic close to production.
How to Test Microservices
A microservices architecture consists of focused, small services that together create a complete application/task. Every instance of a microservice presents with a single responsibility within your application. The real advantage is that these services are independent of one another, which makes them independently deployable and testable.
Let’s look over some approaches for how to go about automated testing.
- Unit Testing
The scope of unit testing (UT) is internal to the service. In terms of the volume of testing, they are the largest in number. Unit testing should ideally be automated, depending on the programming language and the framework within the service.
- Contract Testing
Contract testing should treat as each service as a black box and all the services must be called independently and their responses must be verified. Any dependencies of the service will be stubs that allow the service to function but do not interact with other services. This helps us to avoid any complicated behavior that may be caused by external calls and turn the focus on performing the tests on a single service.
A “contract” means how a service call (where a specific result is expected for certain inputs) is referred to by the consumer-contract testing. Every consumer must receive the same results from service in time, even if the service changes. There should be flexible when we add more functionality as required to the Responses. However, these additions will not break the service functionality. If the service is designed in this manner, it will stay robust over longer durations and the consumers will not be required to modify their code to take into account the changes made later on.
- Integration Testing
Verification of the services that have been individually tested must be performed. The critical part of microservice architecture testing relies on the proper functioning of inter-service communications. Service calls must be made with integration to external services, success cases and including error. Integration testing (IT)thus validates that the system is working together seamlessly and the dependencies between the services are present as expected.
- End-To-End Testing
End-to-end (E2E)testing verifies that the entire process flows work correctly, including all service and DB integration. Thorough testing of operations that affect multiple services ensures that the whole system works together and satisfies all requirements. Frameworks like JBehave help to automate Functional testing by taking user stories and verifying that the system behaves as expected.
- UI/Functional Testing
User interface (UI) testing is the testing of the highest order as it tests the system as an end-user will use it. Testing of this level must feel like a user trying to interact with the system or application. All the interfaces, databases, internal and third-party services work together seamlessly to produce the expected results.
- Microservices Testing Tools
– Goreplay is an open-source network monitoring tool that records your live traffic. Goreplay tool can be used for capturing and replaying live HTTP traffic into your microservices test environment.
– Mountebank is an open-source tool that provides multi-platform, cross-platform test doubles over the wire. we can simply replace real dependencies with Mountebank, and test like you’d do with traditional stubs and mocks.
– VCR helps to record your test suites HTTP interactions that can be played later during future tests for fast, accurate and reliable tests.
– Wilma is a service virtualization tool with the combines capability of Service Stub and HTTP and HTTPS Transparent Proxy. It is easily expandable via plug-ins and can be configured on-the-fly.
– Hikaku– This is a library that helps you in making sure that the implementation of REST-API meets its specifications or requirement.
– Mitmproxy– It is a free and open-source interactive HTTPS proxy that can be used for testing, debugging, privacy measurements, and penetration testing.
– Wiremock is a simulator for HTTP-based APIs. Meanwhile, the general-purpose mocking tools as it works by creating an actual HTTP server for which your code under test can connect to a real web service.
- Example of Testing Microservices
Let’s take a simple example, suppose we have a microservice A that depends on two other services B & C. You need to establish a remote environment where the state/service of A, B, and C is well defined and can be repeatedly set-up. For example, storage of B and C should be pre-initialized. After that, you just run a set of tests testing APIs of microservice A using REST/WebService set of test tools, e.g. Chakram or SOAPUI or xUnit alternative for your programming language (PL).
Mock any peer services where API depends on using the resistor tool. Other alternatives include WireMock,rest-driver, and Mochito.
The obvious challenge is the mocking 3rd party APIs when doing integration testing of microservices. You can use any of the mocking tools mentioned above, just treat the mocks as part of our test fixture and make sure you are up to date with new API releases.
My goal was not to make an argument for one form of testing over others but to showcase the lessons our team has learned while implementing microservices testing strategies. There may be things that may not resonate with you because every scenario will get differ.
In order to perform contract testing is one, you need to incorporate in your comprehensive release process. Also, the performance contract test is a combination of other tests such as automated Integration testing, unit testing, and load testing combined with the manual. This will provide the overall quality assurance in the process.
Today most of the big such as Amazon, Netflix, Uber has moved towards microservice, this makes microservice testing more imperative and powerful. If you have any questions or if you are struggling to execute a successful microservices testing pipeline, we can surely help you out..!
The right selection of tools is not the most important element of microservices testing, however, these tools can help you face many key challenges related to it. Hoverfly allows us to create full component tests that verify if your microservice is able to handle errors or delays from downstream services. Pact helps you to organize the team by verifying and sharing contracts between independently developed microservices. Finally, the Gatling tool can help you implement load tests for selected scenarios in order to confirm the end-to-end performance of your system. To know more, click here
SevenMentor Pvt Ltd.
Call the Trainer and Book your free demo Class for now!!!
© Copyright 2019 | Sevenmentor Pvt Ltd.