7 Testing microservices

 

This chapter covers

  • Understanding the motivation behind testing
  • Test strategies (e.g., unit testing, integration testing, and end-to-end testing)
  • Test containers’ usage in test automation
  • Calculating and viewing the test coverage for test suites
  • Understanding test pyramids

Microservice architecture encourages you to write minimal services that can be easily tested as a unit. For example, while testing the Order service, the only context you need to focus on is Order. To test the Order service, you have two high-level alternatives: manual testing and automated testing.

With manual testing, you must run actual and dependent services to test the entire flow. This can be a time-consuming operation compared to testing backed by a machine. This inefficient testing methodology will also slow down your software delivery. However, with automated testing, you can get fast feedback from your implementation because you don’t need to wait until you finish and test the entire system.

7.1 Testing pyramid

7.2 Testing with a unit test

7.2.1 System under test

7.2.2 Test workflow

7.2.3 Working with mocks

7.2.4 Implementing a mock

7.2.5 Automatic mock generation

7.3 Integration testing

7.3.1 Test suite preparation

7.3.2 Working with Testcontainers

7.4 End-to-end tests

7.4.1 Specifications