Skip to content

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

B2Bnetwork team · · 6 min

Testing and quality

In brief

The test pyramid says you should have the most small, fast tests, meaning unit tests, fewer integration tests, and the fewest end-to-end tests through the user interface. There is no single correct ratio. What counts is the principle: check each rule at the lowest level where it can be checked, and keep E2E tests for the dozen or so paths the business depends on. The shape of the pyramid follows from the system's architecture and from where defects originate.

Where the pyramid comes from and what it actually says

The model was described by Mike Cohn in his 2009 book “Succeeding with Agile”, in three layers: unit tests, service tests and user interface tests. The ISTQB Foundation Level 4.0 syllabus discusses the pyramid together with the testing quadrants and describes it cautiously: the layers represent different granularity of tests, tests at the bottom are small, isolated and fast, and the higher you go, the more complex and slower the tests get. The number and names of layers can vary.

The pyramid does not say E2E tests are worse. It says each level has a different price:

  • execution time: a unit test takes milliseconds, a browser test seconds or minutes;
  • diagnostic precision: a red unit test points to a function, a red E2E test points to “somewhere in the process”;
  • stability: the more components, network hops and data along the way, the more reasons for a random failure;
  • maintenance cost: changing a screen layout does not touch unit tests, but it can break dozens of E2E tests.

What to test at each level

The simplest rule: put a test at the lowest level where it still checks what it is meant to check.

LevelWhat it checksBanking example
UnitRules, calculations, validations in isolationInterest calculation for different dates and rates, IBAN validation
IntegrationCooperation with a database, queue or another serviceSaving a transfer in the database, publishing an event to a queue, a response from the limits service
ContractInterface conformance between consumer and providerThe mobile app and the accounts service agree on response fields
E2EThe full user path through all layersLogin, transfer, confirmation, transaction history

If the rule “an instant transfer above the limit is rejected” has twelve variants, twelve unit tests check them in a fraction of a second. You need only one E2E test: that the rejection reaches the user and the screen shows the right message. Repeating all twelve variants through a browser adds no confidence, but adds a quarter of an hour to every run.

How many tests at which level

Ratios quoted in presentations, such as “70, 20, 10”, are rules of thumb, not a standard. It is better to count from the perspective of risk and architecture:

  1. List the critical paths. In an online store these are usually search, cart, payment, return. In a policy system: quoting, issuing a policy, amendment, renewal. Each gets one or a few E2E tests. A dozen or so of these is often a sufficient top layer.
  2. Plan an integration or contract test for every integration. A database, a queue, an external system: each is a separate point of failure.
  3. Move business rules and calculations into unit tests. This is where most test cases come from, with boundary values and rejection paths.

An example for a mid-sized application with several services: a few hundred unit tests, a few dozen integration and contract tests, a dozen or so E2E tests. This is an order-of-magnitude example, not a norm. More important than the count is that the full suite returns a result in a time the team is willing to wait before merging a change.

When the pyramid gets distorted

A few shapes show up in projects regularly:

  • the inverted pyramid (ice cream cone): hundreds of tests through the interface, a few unit tests. Typical when automation is started by a team of testers without access to the code. Symptom: regression takes hours, and half of the failures are flaky tests;
  • the hourglass: lots of unit and E2E tests, nothing in the middle. Integration defects surface only in the full path and are hard to locate;
  • the trophy: a wide integration layer at the expense of the unit layer. It makes sense in applications with little logic where most of the work is assembling data from APIs, for example in a thin web client.

In a microservices architecture the middle of the pyramid usually grows, because most defects arise at the boundaries between services. Part of the integration tests are then replaced by API contract tests, which check the interface without standing up the whole environment.

For systems with a large share of legacy code, for example applications on IBM i or a monolith without tests, the pyramid is built from the top: first a few E2E tests as a safety net, then unit tests for every module the team changes. It is similar in a bank's mobile app: tests on devices are expensive, so the E2E layer is limited to critical paths, while rules and screen logic are checked with unit tests and API tests.

How to rebuild an existing test suite

Rebuilding a suite goes best in small steps:

  1. Measure the duration and stability of every E2E test over the last few weeks.
  2. For tests that check a rule rather than a path, write an equivalent at a lower level and remove the E2E version.
  3. Keep only critical paths and post-deployment smoke tests in the E2E layer.
  4. Set a time budget, for example a full run before merge in a dozen or so minutes, and enforce it in CI.

This work requires two skills: test case design and automation. The first is described by the ISTQB Foundation Level syllabus, the second by the Test Automation Engineer (CTAL-TAE) syllabus. You can take both exams at our accredited exam center.

Questions and answers

Do developers write unit tests and testers write E2E tests?

That is usually how it looks, but the pyramid works better when the whole team is responsible for all levels. A developer writes unit and integration tests for their change, a tester designs cases and points out boundary values and risks, and E2E tests are written together. The split “developers the bottom, testers the top” favors an inverted pyramid, because testers without access to the code can test only through the interface.

Does high code coverage mean a good pyramid?

No. Coverage tells you which lines were executed, not whether a test checked anything. You can have high unit coverage with no assertions and no integration checks. Coverage is useful for finding untested areas, especially in code with many rules. How to read this and other numbers is described in the article on software quality metrics.

Where do performance and security tests fit in the pyramid?

Outside it. The pyramid mainly organizes functional tests by level of granularity. Performance, security and accessibility tests are other test types with their own schedule and environment. The ISTQB syllabus organizes them with the testing quadrants. In practice some of them, for example a dependency scan or a short load test, also land in the CI pipeline.

How many E2E tests is too many?

Too many is when the full run does not fit in the time the team waits before a release, or when failures have to be rerun regularly to tell a defect from chance. A second signal is E2E tests that check the same rule across many data variants. Such variants belong lower down, and a single path stays at the top.

What it looks like in a project

Planning testing for your project? Talk to a test manager.

Let's talk

Read more: Testing and quality