← Concepts

阅读中文版本

Test Pyramid

The test pyramid favors many fast unit tests, fewer integration tests, and a small number of end-to-end tests to balance feedback speed and confidence.

testing

Definition

The test pyramid is a heuristic for test portfolios: a broad base of fast, isolated unit tests, a smaller layer of integration tests that exercise real boundaries, and a thin top layer of end-to-end tests that cover critical user journeys. The shape reflects cost and feedback time: more tests at the bottom where they are cheap, fewer at the top where they are slow and brittle.

Context and scope

The pyramid is used to plan test coverage and review suites. It is a starting point, not a law; the right mix depends on the system, the risk, and the cost of failures.

Common misconceptions

  • Unit tests alone are enough — they do not prove that services, databases, or browsers work together
  • End-to-end tests are the only real tests — a suite of only E2E tests is slow and gives poor failure localization
  • The pyramid mandates a fixed percentage — the proportions are a guide, not a quota

Anti-patterns

  • An inverted pyramid: many brittle E2E tests and few unit tests
  • Mocking every boundary so integration behavior is never tested
  • Measuring success by test count instead of risk coverage

Related concepts

Related examples

Authoritative source

https://martinfowler.com/articles/practical-test-pyramid.html

Fast feedback at the base

Unit tests run in milliseconds and localize failures to a function. They are ideal for pure logic, validation, and edge cases. Integration tests exercise real adapters: a database, a queue, an HTTP boundary. End-to-end tests cover a few critical user journeys through the deployed system. Each layer answers a different question.

        E2E        few, critical journeys
     integration   real boundaries, contracts
   unit            many, fast, deterministic

The pyramid is about feedback economics. A bug caught by a unit test is cheaper to diagnose than the same bug caught by a browser test that timed out after 30 seconds.

Integration is where surprises live

Many teams over-mock and then discover that serialization, SQL, or authentication behaves differently in production. Integration tests should hit real boundaries where practical, using containers or ephemeral services. Contract tests can cover service interfaces without a full environment. E2E tests should focus on the flows that generate the most business risk, not every permutation.

Review the portfolio, not the count

A suite with thousands of unit tests and no integration coverage is not strong. A suite with 100 E2E tests and no unit tests is slow and opaque. Examine which risks are covered, how long failures take to diagnose, and whether the suite is flaky. Adjust the mix as the architecture changes.

When to use it

Use the pyramid when planning a test strategy or diagnosing a slow, flaky suite. The advantage is a shared language for cost and coverage; the drawback is that it can be applied mechanically. Compared to a single-layer strategy, a layered portfolio provides faster feedback and better failure localization.