12 Moving integration tests to contract tests

 

This chapter covers

  • When to use integration tests over contract tests
  • Identifying suitable integration tests to migrate into contract tests
  • Refactoring integration tests to contract tests
  • Refactoring end-to-end tests to contract tests

In chapter 2, section 2.2, we described how contract testing can replace other types of tests, in particular, integration tests. Contract testing sits above unit testing and below integration testing within the test pyramid, as shown in figure 12.1 (also referenced in chapter 2, figure 2.6). Think of contract testing like an asynchronous integration test. Even though it’s still important to have integration tests, contract tests contribute to a more stable and scalable testing solution.

Figure 12.1 Test automation pyramid with contract testing, showing how tests higher up the pyramid are less scalable
figure

The list in figure 12.1 on the left side shows common reasons for migrating tests to contract tests:

  • Slow
  • High maintenance
  • Expensive
  • Breaks easily

During presentations introducing contract testing with Pact, we often ask attendees a couple of questions.

  1. In your experience, how often do you find problems in an integration environment?
  2. How often is your integration environment unstable?

12.1 When to use integration tests over contract tests

12.2 Identifying suitable integration tests to migrate

12.3 Refactoring integration tests to contract tests

12.4 Refactoring end-to-end tests to contract tests

Summary