Integration and End-to-End Testing

Test your API against real databases and dependencies, and verify full user flows end to end.

Intermediate · about 8 hours · difficulty 3/5

Learn first: Unit Testing Fundamentals, Resource-Oriented REST APIs

Unit tests prove your logic; integration tests prove the pieces work together: your code with a real database, a real HTTP layer, a real queue. Functional or end-to-end (e2e) tests exercise complete user flows from the outside, the way a client would.

Integration testing an API

  • Start your app in-process and send real HTTP requests (Supertest, FastAPI TestClient, Spring MockMvc, WebApplicationFactory).
  • Use a real database engine, not a mock. Testcontainers spins up PostgreSQL, Redis or Kafka in Docker per test run.
  • Reset state between tests with transactions that roll back, truncation or a fresh schema.
  • Test the unhappy paths: validation errors, auth failures, conflicts.

End-to-end tests

Run the deployed system (or a production-like staging) and drive it through user journeys: sign up, create an order, receive an email. For APIs this can be a collection of HTTP scenarios; for full-stack apps, browser tools like Playwright. Keep e2e tests few and focused on critical paths because they are slow and more brittle.

Contract tests

In multi-service systems, consumer-driven contract tests (for example Pact) check that a provider still satisfies what its consumers expect, without deploying everything together.

The shape of a good suite

Many unit tests, a solid layer of integration tests, and a thin layer of e2e tests. Many backend teams find integration tests give the best confidence per minute.

You are ready to move on when your API has integration tests against a real Postgres in Docker that run in CI in under a few minutes.

Key concepts

  • HTTP-level API tests
  • Testcontainers
  • Test data and database reset
  • End-to-end user journeys
  • Playwright
  • Contract testing with Pact
  • Load testing (k6)

Mini project

Write integration tests for your API's signup, login and create-resource endpoints against a Testcontainers PostgreSQL, including tests for 401, 403 and 422 responses.

Check yourself

Why can mocking the database in API tests give false confidence?

Free resources