04.4.2 · Skill in Engineering quality

Integration tests

Tests that check components working together, including real databases or services.

Integration tests catch problems that unit tests miss, at the boundaries between parts. They are slower but often give more confidence per test. Many teams find them the best value for typical product code.

Your status · Integration tests

Saved in this browser. Sign in to keep it on every device.It colours this skill's tile on your map.

Why it matters for a product engineer

Integration tests check that parts work together: the code with the database, the API with its handler, the app with a partner’s service. Many bugs live at these boundaries, where unit tests cannot see them. Integration tests are slower than unit tests, but they often give more confidence per test. Kent C. Dodds sums it up: “Write tests. Not too many. Mostly integration.”

How to do it

  1. Test a feature through its real entry point, such as an HTTP route or a command, with a real database.
  2. Replace only what you do not control, such as third-party APIs, and keep the rest real.
  3. Record real responses from external services once, and replay them in tests.
  4. Give each test its own data, so tests can run in any order and in parallel.
  5. Assert on what a user or caller would see: the response, the saved record, the email sent.
  6. Keep the slowest tests out of the main loop if they block people, but keep them in CI.

Common mistakes

  • Mocking so much that the test only checks the mocks.
  • Tests that call a live third-party API, and fail when it is slow.
  • Shared test data, so one test breaks another.
  • Only unit tests, and every bug found at the boundaries in production.

Example

Segment sends events to over 140 destinations, each a partner API. Its tests used to call those APIs over the internet. These calls were the main cause of failing tests, some destinations took up to five minutes, and the whole suite up to an hour. The team built Traffic Recorder: the first run records each request and response to a file in the repository, and later runs replay the file.

Sequence

How Segment's Traffic Recorder runs a test

TestTraffic RecorderPartner API01 Request, first run02 Real request03 Response, saved to a file04 Response05 Request, later runs06 Replayed from the fileTestTrafficRecorderPartner API01 Request, first run02 Real request03 Response, saved to a file04 Response05 Request, later runs06 Replayed from the file

After the change, the tests for all 140+ destinations ran in milliseconds, and expired credentials or a slow partner no longer failed them.

ReportedTwilio Segment, 'Goodbye microservices'

Sources: The testing trophy and testing classifications (Kent C. Dodds) ↗ · The practical test pyramid (Ham Vocke) ↗ · Goodbye microservices (Alexandra Noonan, Twilio Segment) ↗

Resources

2

Tools

2
Tools for Engineering quality