Test Pyramid
The pyramid is a heuristic about proportion: many small fast tests, fewer integration tests, and a thin layer of end-to-end tests. It is not a law, and the argument about its shape is really an argument about where confidence comes from per unit of maintenance cost.
The 60-second answer
Section titled “The 60-second answer”“The pyramid says most of your tests should be unit tests, because they run in milliseconds and pinpoint failures, with fewer integration tests and a handful of end-to-end tests for critical user flows. The logic is cost: as you move up, each test is slower, flakier and more expensive to maintain, but it tests more of the real system. The trophy variant argues that for UI-heavy apps the integration layer — components rendered together with realistic data — gives the best confidence-per-cost, because pure unit tests of components often test nothing users care about. Either way, the shape should follow the risk profile of the system, and coverage measures lines executed, not behaviour verified.”
The three layers
Section titled “The three layers”| Unit | Integration | End-to-end | |
|---|---|---|---|
| Scope | One function/class/module | Several real components, often a DB | Whole system through its UI or API |
| Speed | Milliseconds | Tens to hundreds of ms | Seconds |
| Dependencies | All doubled | Real DB, real routing; external services doubled | Everything real |
| Failure signal | Precise | Good | “Something in this flow broke” |
| Flakiness | Very low | Low to medium | Highest |
| Confidence | Low about wiring | High about contracts | Highest about user outcome |
| Typical share | Majority | Substantial | A few critical paths |
RSpec.describe Invoice do it "applies tax after the discount" do invoice = build(:invoice, subtotal: 100, discount: 10, tax_rate: 0.2) expect(invoice.total).to eq(108) endendRSpec.describe "Checkout", type: :request do it "creates an order and redirects to it" do cart = create(:cart, :with_items)
expect { post checkout_path, params: { cart_id: cart.id } }.to change(Order, :count).by(1)
expect(response).to redirect_to(order_path(Order.last)) endendRSpec.describe "Checkout", type: :system do it "lets a signed-in user pay with a saved card" do user = create(:user, :with_saved_card) sign_in_as(user)
visit cart_path click_button "Checkout"
expect(page).to have_content("Order confirmed") endendThe three tests overlap deliberately. The unit test tells you the tax rule is wrong. The request test tells you the controller stopped creating orders. The system test tells you the button is gone. You want the first signal to arrive in the cheapest layer that can produce it.
The trophy, and why it exists
Section titled “The trophy, and why it exists”Kent C. Dodds’ testing trophy puts the largest share in integration tests, with a static-analysis base (types, lint) and a small unit and e2e layer on top. The argument is specific to front-end code: a unit test of a React component that mocks the DOM, the router and the API mostly asserts that your mocks were called. Rendering the component with a realistic provider tree and asserting on visible output catches more real bugs.
Generalised: the best layer is the one where the thing you care about is real. For a pricing algorithm, that is a unit test. For a component’s accessible output, that is a component test. For “can a user complete checkout”, it is an end-to-end test.
The cost model
Section titled “The cost model”Total cost of a test ≈ write time + run time × frequency + maintenance + debugging flakiness.
A unit test running in CI 200 times a day at 5 ms is free. An e2e test at 40 s with a 2% flake rate costs engineer-hours every week. That asymmetry — not purity — is why the base of the pyramid is wide. But a hundred unit tests that never exercise the wiring will happily pass while the application is broken, which is the failure mode the trophy addresses.
Optimisations that keep the top of the pyramid affordable:
- Run e2e tests in parallel shards and only on pull requests to protected branches.
- Replace sleep with deterministic waits; most flakiness is timing, not the app.
- Seed data explicitly per test; shared fixtures create order-dependent failures.
- Quarantine flaky tests instead of retrying them silently; a retry masks a real race.
Coverage myths
Section titled “Coverage myths”Coverage reports which lines, branches and functions were executed. That is all.
- High coverage is necessary but not sufficient. A test with no assertions executes lines and proves nothing.
- Low coverage is actionable. It points at code nobody exercised, which is where surprises live.
- 100% is a bad target. It rewards testing trivial code and punishes defensive branches that cannot be reached.
- Branch coverage beats line coverage for conditionals; a line with
a && bis one line and three paths. - Coverage of changed lines (a diff-coverage gate in CI) is a more useful signal than a global number.
The healthy use: set a floor that ratchets, review uncovered branches in critical modules, and never let a coverage number substitute for a conversation about what behaviour matters.
Follow-up questions
Section titled “Follow-up questions”- Give an example of a bug only an end-to-end test would catch.
- How would you test a background job that sends email?
- What would you do about a suite that takes 40 minutes?
- When is a unit test with heavy mocking actively harmful?
- How do you keep test data from leaking between tests?
Common traps
Section titled “Common traps”- Treating the pyramid as a rule. It is a default, adjusted by risk and architecture.
- Unit-testing glue code. Controllers and components with no logic are better covered by integration tests.
- End-to-end testing everything. You get a slow, flaky suite and a team that ignores red.
- “We have 100% coverage.” Say what the tests assert, not what they execute.
- Shared mutable fixtures. Tests pass in isolation and fail in suite order.
- Retrying flaky tests as the fix. The retry hides a race that will reach production.