Skip to content
TestLine Menu
TestLine
automation

The test pyramid in practice: how many unit, API and E2E tests?

Typical ratios of tests at three levels, an example for a web app, run times and situations where a different shape works better.

author
Artur Machowski, Senior QA Engineer
published
reading
6 min

Short answer

The test pyramid is a rule: many fast unit tests at the bottom, fewer integration and API tests in the middle, and the fewest E2E tests through the UI at the top. A commonly quoted ratio is 70/20/10, for example 500 unit tests, 150 API tests and 20-30 E2E tests in a mid-sized web app. More important than the percentages is one rule: check everything at the lowest level where it can be checked. Then the whole regression takes minutes, not hours.

Three levels

levelwhat it checkstime per testusually written by
unit testsa function or class in isolation, e.g. calculating VATmillisecondsdevelopers
integration and API testsworking with a database, a queue, another service; API endpointstens of milliseconds to secondsdevelopers and testers
E2E testsa whole user path through the UI, e.g. sign-up and paymenta few seconds to a minutetesters

Example: a booking app

levelnumber of testswhat we testtime for the whole suite
unitabout 500prices, discounts, slot availability, validationabout 1 minute
APIabout 150booking, cancellation, payment, permissions, negative scenarios3-5 minutes
E2E20-30sign-up, login, booking with payment, cancellation, admin panel10-15 minutes

The same discount rules checked in 40 E2E tests would take over ten minutes and break with every visual change. As unit tests they take a fraction of a second. The E2E test only checks that the discount reaches the cart at all.

How to shape the pyramid in your project

  1. List 5-10 critical paths and turn them into E2E tests. That’s the top.
  2. Move business rules down. Every data variant and edge case that can be checked without a browser goes into unit or API tests.
  3. Add negative API tests: missing fields, no token, someone else’s resource, a duplicate. I list them in 15 negative REST API test scenarios.
  4. Set the times: a smoke test on a pull request in a few minutes, full regression in ten to a few dozen minutes. How to split this in CI is covered in regression in CI.
  5. Leave room for exploratory testing. The pyramid is about automated tests. New features are still worth going through by hand before they go into automation.

When the shape is different

  • A frontend app with little server logic. More component integration tests, fewer unit tests. This shape is sometimes called the testing trophy.
  • Many services talking over APIs. Add contract tests, which make sure a change in one service doesn’t break the others.
  • A mobile app. E2E tests on devices are slower and more expensive, so the top should be even narrower, with more logic checked through the API.

The inverted pyramid and how to get out of it

An inverted pyramid means many E2E and manual tests and few unit and API tests. Symptoms: regression takes hours, tests are often flaky, and when a test fails nobody knows what broke. The way out isn’t throwing away E2E tests but gradually moving checks down: for every flaky or slow E2E test, ask whether the same thing can be checked with an API test.

How I can help

As part of test automation I build E2E tests of the critical paths in Playwright, and as part of API testing an API test suite with negative scenarios. If you already have many tests and regression takes too long, I’ll start by reviewing which tests to move down. Describe your project in the contact form.

Frequently asked questions

What are the ratios in the test pyramid?

A common rule of thumb is 70% unit tests, 20% integration and API tests and 10% E2E. It's a starting point, not a standard. More important than the percentages is the rule: check everything at the lowest level where it can be checked.

How many E2E tests should an app have?

Usually from a dozen to a few dozen, one per critical path and important variants. To start, 5-10 paths in CI are enough. Hundreds of E2E tests are slow, expensive to maintain and more often flaky.

What is an inverted test pyramid?

A situation where the team has many E2E and manual tests and few unit and API tests. Regression takes hours, tests are flaky and it's hard to tell what broke. It's a common state after years of automating only through the UI.

Does the test pyramid fit every project?

Not always. In a frontend app with little logic, more component integration tests work better (the so-called testing trophy). In a system with many services, contract tests matter. The lowest-level rule still holds.

Who writes the tests at each level?

Unit tests are usually written by developers. API tests by developers and testers together, with testers adding negative scenarios. E2E tests are most often written by a tester, with the team agreeing on the critical paths.

Got a release in a few weeks?

Describe your project in the form. I'll reply with questions about scope or an initial test plan.