Testing Pyramid: Layers, Examples and How to Use It

Testing Pyramid: Layers, Examples and How to Use It

—

by

in
Table of Contents

The testing pyramid is a model for balancing automated tests: many fast unit tests at the base, fewer integration and API tests in the middle, and a small number of end-to-end tests at the top. Keploy helps with the middle layer by turning real API traffic into integration tests with mocks.

The testing pyramid usually comes up in a retro after the same complaints: the suite takes 40 minutes, fails twice a week for no clear reason, and nobody trusts a red build anymore. Teams tend to blame the framework or the CI runner. Most of the time, though, the problem is the shape of the suite, not the tools running it.

When most of the checking happens through the browser, every test is slow to run, costly to maintain, and hard to debug. Move the same checks lower down, and they get faster, cheaper and far more precise. That one idea, putting each check at the lowest level that can catch the problem, has held up through more than 15 years of debate.

What Is the Testing Pyramid?

Testing pyramid definition: the testing pyramid, also called the test pyramid or test automation pyramid, is a guideline for how many automated tests to write at each level of detail. The more isolated and cheaper a test is to run, the more of them you should have.

Mike Cohn introduced the idea in his 2009 book Succeeding with Agile. His original version had three layers: unit tests at the bottom, service tests in the middle and UI tests at the top. Martin Fowler later helped popularize it and pointed out that, in web applications, the service layer usually means testing through the API rather than the interface.

The pyramid shape reflects a trade-off. Tests near the bottom are fast, cheap and precise, but each one checks only a small piece of the system. Tests near the top check the whole system the way a user would, but they’re slow, brittle and costly to maintain. So the pyramid’s answer is simple: write lots of tests at the bottom and only a few at the top.

Testing pyramid layers many fast unit tests at the base, fewer integration and API tests in the middle, and the fewest end-to-end and UI tests at the top

The Layers of the Testing Pyramid

The three testing pyramid levels are usually named unit, integration and end-to-end today, even though Cohn called the middle layer "service tests." If you’re new to these terms, our guide to software testing basics covers them, and the post on levels of software testing explains how they map to the wider testing process.

Unit Tests

  • What it checks: a single function, method or class in isolation, with its dependencies replaced by test doubles.
  • Speed: milliseconds per test, so thousands can run on every commit.
  • Who writes it: the developer writing the code, usually at the same time.
  • Typical tools: JUnit, pytest, Jest, and Go’s built-in testing package.
  • What it can’t catch: problems in how pieces fit together, such as a wrong database query, a mismatched API field or a misconfigured service.

Well-written unit tests are the base of the pyramid because they’re the cheapest way to check logic, and a failure points straight at the broken function.

Integration and API Tests

  • What it checks: how components work together, such as a service and its database, two services talking over an API, or a request moving through several layers.
  • Speed: seconds per test, depending on how many real dependencies are involved.
  • Who writes it: developers, often with help from QA engineers.
  • Typical tools: REST Assured, Supertest, Testcontainers and Postman.
  • What it can’t catch: UI problems and full user journeys that span many services and screens.

Integration tests are where Cohn’s service layer lives today. In API-heavy systems, this is often the layer that catches the bugs users would actually notice.

End-to-End and UI Tests

  • What it checks: a complete user journey through the real interface, such as signing up, adding to cart and paying.
  • Speed: many seconds to minutes per test, because each one starts a browser and the full application stack.
  • Who writes it: QA engineers or developers, usually for the most critical journeys only.
  • Typical tools: Playwright, Cypress and Selenium.
  • What it can’t catch: much of anything precisely. A failure tells you something is wrong somewhere in the stack, not where.

End-to-end tests give the most confidence that the system works as a whole, which is exactly why they belong at the top in small numbers.

\
Here’s how the three layers compare:

Aspect

Unit tests

Integration and API tests

End-to-end and UI tests

Scope

One function or class

Components working together

A full user journey

Speed

Milliseconds

Seconds

Seconds to minutes

How many

The most

A moderate number

The fewest

Cost to maintain

Low

Medium

High

Flakiness risk

Low

Medium

High

A failure tells you

Exactly which function broke

Which interaction broke

That something broke

When it runs

Every commit

Every pull request

Before release or nightly

Why the Shape Matters

The pyramid isn’t about the picture. It’s about what each layer costs you every day the suite runs.

  • Feedback speed: A suite made mostly of unit tests runs in minutes, so developers wait for it. A suite made mostly of browser tests runs long enough that people merge before it finishes.
  • Cost to maintain: A UI test breaks when a button moves, even if nothing is actually wrong. Lower tests change only when the behavior they check changes.
  • Flakiness: Every extra moving part, such as a browser, a network call or a shared database, is another way for a test to fail at random. Fewer moving parts means fewer false alarms.
  • Diagnosis: When a unit test fails, you know which function broke. When an end-to-end test fails, someone has to dig through logs across the stack before the real fix can even start.
  • Catching regressions early: Because lower tests run on every change, a regression is caught minutes after it’s introduced, not days later in a nightly run.

The Ice Cream Cone Anti-Pattern

The ice cream cone is the testing pyramid turned upside down. It has a thin layer of unit tests at the bottom, a few integration tests above that, a thick layer of end-to-end and UI tests, and a large scoop of manual testing on top. Without the manual layer, it’s sometimes called the testing pizza.

Ice cream cone anti-pattern vs testing pyramid mostly manual and UI tests compared with mostly unit tests

Teams rarely choose this shape. It builds up over time, usually for one of these reasons:

  • Test automation started in a QA team that only had access to the UI.
  • An older codebase has no easy seams for unit tests, so the browser became the only way in.
  • Manual test cases were automated one by one, so each became a UI test.
  • Record-and-playback tools made UI tests quick to create, even though they’re slow to maintain.

Flipping the cone doesn’t mean deleting your UI tests in one go. Stop adding new end-to-end tests for business logic, and write those checks as unit or API tests instead. When an end-to-end test fails, ask whether a lower test could have caught the same bug, and add that test. Over a few months, retire the UI tests that now duplicate lower coverage.

Testing Pyramid Variations

The pyramid has plenty of critics, and several alternative shapes have come out of that debate. Most of them keep the core idea of testing at different levels but change where the bulk of the tests should sit.

Testing pyramid vs testing trophy vs testing honeycomb, showing where most tests sit in each model

Testing Trophy

  • Shape: a trophy with a static analysis base, a small unit layer, a wide integration layer and a small end-to-end top.
  • Where most tests sit: integration.
  • Best for: frontend and full-stack JavaScript apps, where testing components together gives more confidence than testing them alone.
  • Trade-off: integration tests are slower than unit tests, so the suite needs care to stay fast.

Kent C. Dodds developed the testing trophy, inspired by Guillermo Rauch’s advice to write tests, "not too many," and "mostly integration." That’s the main difference in the testing pyramid vs testing trophy debate: the trophy shifts the bulk of the effort from unit tests to integration tests and adds static analysis as its own layer.

Testing Honeycomb

  • Shape: a hexagon with a small layer of implementation-detail tests at the bottom, a wide layer of integration tests in the middle and a small layer of integrated tests at the top.
  • Where most tests sit: integration, testing each service through its API with its own dependencies.
  • Best for: microservices, where most of the risk is in how services talk to each other.
  • Trade-off: it depends on good dependency control, such as mocks or test containers, to keep integration tests fast and reliable.

Spotify’s engineering team introduced the honeycomb in 2018. Their argument was that a microservice is small enough that it effectively becomes the new "unit," so most tests should check it through its API.

Testing Diamond

  • Shape: a diamond with a narrow unit base, a wide integration middle and a narrow end-to-end top.
  • Where most tests sit: integration.
  • Best for: teams whose unit tests keep breaking during refactors without catching real bugs.
  • Trade-off: fewer unit tests means less precise failures when logic breaks.

Testing Crab

  • Shape: a crab with a heavy body of end-to-end and visual tests, with integration and unit tests in the background.
  • Where most tests sit: end-to-end and visual UI tests.
  • Best for: small, UI-heavy projects where how the app looks matters as much as how it works.
  • Trade-off: it’s the most expensive shape to run and maintain, so it rarely scales to large systems.\

Here’s how the shapes compare:

Shape

Where most tests sit

Best for

Main trade-off

Testing pyramid

Unit

Most backend and general-purpose codebases

Can underinvest in integration testing

Testing trophy

Integration, plus static analysis

Frontend and full-stack JavaScript apps

Slower suites if integration tests aren’t kept lean

Testing honeycomb

Integration through each service’s API

Microservices

Needs solid dependency mocking

Testing diamond

Integration

Codebases where unit tests erode during refactors

Less precise failures

Testing crab

End-to-end and visual

Small, UI-heavy projects

Highest cost to run and maintain

Martin Fowler has noted that much of this debate comes down to definitions. One team’s "unit test" checks a single class with everything mocked, while another team’s checks several classes working together. Before you pick a shape, agree on what each layer means for your codebase.

The Testing Pyramid for Microservices and APIs

A test pyramid for microservices usually ends up with a much wider middle than the classic version. Each service is small, so there’s less logic inside it to unit test. Most of the risk sits at the boundaries: the API contract, the database, the message queue and the other services it calls.

That’s why the middle layer carries most of the weight in service-based systems:

  • API tests check each endpoint’s requests and responses directly, without a browser in the way.
  • Contract tests check that a service and the services that call it still agree on request and response formats.
  • Dependency mocking replaces databases and downstream services with stand-ins, so these tests run fast and give the same result every time.

The catch is that this layer is expensive to build by hand. Every endpoint needs requests, expected responses and realistic mocks for each dependency.

Keploy makes the middle layer practical by recording real API traffic and turning it into integration tests, with the database and downstream calls captured as mocks. Because the tests come from real usage, they cover the paths your users actually take. Recorded traffic won’t cover brand-new endpoints, though, so those still need hand-written tests.

Unit tests still matter here. Keep them for the parts of a service with real logic, such as pricing rules, validation or data transformations.

How to Apply the Testing Pyramid in CI/CD

The pyramid maps neatly onto a CI/CD pipeline. The faster a layer runs, the earlier and more often it should run:

  • Every commit: static checks and unit tests. They finish in minutes, so developers get feedback before they’ve switched tasks.
  • Every pull request: integration, API and contract tests, with dependencies mocked so results don’t depend on shared environments.
  • Before release or nightly: end-to-end tests for the most critical user journeys only.

Testing pyramid in a CICD pipeline unit tests on every commit, integration and API tests on every pull request, end-to-end tests before release

Make each stage a quality gate. A failing unit or API test should block the merge, and a failing end-to-end test should block the release.

This is also why the agile testing pyramid suits short sprints so well: most problems surface within a single pull request instead of piling up for a test phase at the end. For the broader picture of how this fits into test automation, including tooling and rollout, see Keploy’s guide.

How to Check If Your Suite Matches the Pyramid

You don’t need a consultant to see your suite’s real shape. Your repository and CI history already have the numbers:

  1. Count tests per layer. Use folder names, file naming conventions or test tags to sort every test into unit, integration and end-to-end. If you can’t tell which layer a test belongs to, that’s useful information too.
  2. Pull run time per layer. From your CI history, add up how long each layer takes on an average run.
  3. Measure flakiness per layer. Count tests that failed and then passed on a retry with no code change in the last 30 days.
  4. List end-to-end tests that check logic. Any UI test that really checks a calculation, a validation rule or an API response is a candidate to move down a layer.
  5. Look for gaps in the middle. List your API endpoints and check which have no integration or API tests at all.\

Fill in a simple table with your own numbers:

Layer

Number of tests

Total run time

Flaky in the last 30 days

What to move or add

Unit

Integration and API

End-to-end and UI

If the bottom row has the biggest numbers in the first three columns, you’re looking at an ice cream cone, and step 4 is where to start.

Common Testing Pyramid Mistakes

  • Treating ratios as rules. The pyramid shows proportions, not exact percentages. A team that forces a fixed split ends up writing tests to hit a number.
  • Counting tests instead of coverage. Two hundred unit tests on getters and setters don’t protect you as much as twenty on the code that actually makes decisions.
  • Mocking everything in unit tests. If every dependency is mocked, the test only proves the mocks agree with each other.
  • Skipping the middle layer. Teams with strong unit tests and a few UI tests often have nothing checking how services talk to each other, which is where many production bugs start.
  • Letting the end-to-end suite grow unreviewed. Every new UI test adds run time and maintenance forever. Review them like code, and delete the ones lower tests already cover.
  • Ignoring flaky tests. A test that fails at random teaches people to rerun the pipeline instead of reading it. Fix it or remove it.

Conclusion

My take: the testing pyramid gets criticized mostly by people arguing about its exact proportions, which was never the point. The lasting lesson is to catch each problem at the lowest level that can see it, and that holds whether your suite looks like a pyramid, a trophy or a honeycomb.

If you want one next step, run the five checks above on your own suite this week. Start with the slowest end-to-end tests, and ask which of them could become API tests instead. That single change usually does more for speed and stability than switching frameworks.

FAQs

What are the levels of the testing pyramid?

There are three: unit tests at the base, integration and API tests in the middle, and end-to-end or UI tests at the top. Mike Cohn’s original version called the middle layer "service tests." Some versions add static analysis below the unit layer.

What is the difference between the testing pyramid and the testing trophy?

The pyramid puts most tests at the unit level. The testing trophy, developed by Kent C. Dodds, puts most of them at the integration level and adds static analysis as its own base layer, because integration tests give more confidence per test in many frontend and full-stack apps.

Is the testing pyramid still relevant?

Yes, as a principle. Its core advice, to test each behavior at the lowest level that can catch the problem, still holds. What’s changed is the exact shape: microservices and frontend apps often need a wider integration layer than the classic pyramid shows.

Where do API tests fit in the testing pyramid?

In the middle layer, alongside integration tests. They check services directly through their endpoints, which makes them faster and more stable than UI tests while still checking real behavior between components.

What is the ice cream cone anti-pattern?

It’s the testing pyramid turned upside down: few unit tests, many slow end-to-end and UI tests, and a large amount of manual testing on top. It usually builds up over time and leads to slow, flaky suites that are expensive to maintain.

Author

  • Sancharini Panda

    Sancharini is a digital marketer with experience in the technology and software development space. She collaborates with engineering teams and uses industry research to create practical insights on software testing, automation & modern development workflows.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *