The test pyramid
The shape is a consequence of cost, measured on this project: five in-process guards in 1.9 seconds, two container-based ones in 54.
The pyramid says: many fast tests at the bottom, fewer and slower ones as you go up. It is drawn in every testing article and the drawing is not the useful part — the reason for the shape is, and the reason is a number you can measure on your own project in about a minute.
Measure your own, first
Here is this site's test suite, timed guard by guard:
check: 0.3s
learner:test: 0.8s
search:test: 0.2s
blog:test: 0.4s
sql:test: 0.2s
java:test: 19.5s
lesson:run:test: 34.5sFive of those run pure JavaScript against in-process data: 1.9 seconds together. Two of them start Docker containers and compile and run Java: 54 seconds.
That is a 28× difference, and nothing about it is unusual. It is the whole argument for the shape:
- The bottom is where you can afford many tests, because a hundred of them cost a second.
- The top is where each test is expensive, so you want few — and you want each one to earn its place by covering something the layer below cannot.
The pyramid is not an aesthetic preference about test counts. It is a consequence of what things cost.
The three levels, and what only each can catch
| Level | Speed | Catches what nothing below it can |
|---|---|---|
| Unit | milliseconds | a rule computed wrongly |
| Integration | seconds | your code meeting something real — a database, a broker, an HTTP client |
| End to end | tens of seconds to minutes | the pieces wired together, and configuration |
The middle row is the one that gets skipped and the one that pays. A unit test of a repository method proves nothing about whether the SQL is valid — @DataJpaTest against a real Postgres in a container does, and that is a class of bug unit tests structurally cannot reach.
The top row's real subject is configuration. Most end-to-end failures are not logic; they are a missing environment variable, a security rule in the wrong order, a serialiser that behaves differently in the packaged jar. Those exist nowhere below.
The shapes teams actually build
Three of them, and each is a recognisable failure:
The ice-cream cone. Almost everything end to end, little underneath. Usually arrives when a QA team owns tests and engineers do not write them. It catches real bugs and does it slowly, flakily, and hours after the commit — so it tells you something is broken rather than this line is wrong.
The hourglass. Many unit tests, many end-to-end tests, nothing in between. Extremely common, and it happens because the middle is the awkward one: too slow to feel like a unit test, too fiddly to set up. Then a query is wrong and only the browser test notices, three minutes later.
The cupcake. Everything, everywhere, several times. Every rule tested as a unit, again through the service, again through the API. It passes every review and takes forty minutes, and the duplication means one change breaks tests at three levels.
The number that actually matters
Not the ratio. How long until a developer knows they broke something.
Under ten seconds, people run the tests constantly and find out while the change is still in their head. Past a few minutes they stop running them and wait for CI. Past ten minutes they push and go to lunch, and the feedback arrives attached to somebody else's commit as well as theirs.
So the useful tuning is not "more unit tests" — it is:
- Split the suite. Fast tests on every save; the slow ones on push or in CI. Maven profiles or JUnit tags are enough.
- Run in parallel. JUnit 5 does it with a property; Testcontainers reuse turns a container start into a container connect.
- Make the slow ones fewer, not faster. A container start has a floor. The lever is how many times you pay it.
That last point is why this site's own suite has exactly two slow guards. They cost 54 seconds and they are the only place a Java fixture or a lesson's predict output can be proven against a real JVM — so they are worth their cost, and there is no third one.
Where each test belongs
A rule of thumb that resolves most cases: write the test at the lowest level that could actually fail for this reason.
- A discount threshold — a unit test. Nothing about a database makes it more true.
- A repository query — an integration test with a real database. It is SQL; only a database can say.
- Authentication rules — an integration test at the filter chain, because order is the behaviour.
- "Can a customer complete checkout" — one end-to-end test. One, not twelve.
Under the hood: the cost math that forces the shape
The gap this lesson measured is not incidental — it comes from what each layer's cost is actually made of. A unit test's cost is almost entirely in-process bytecode execution: nanoseconds to low microseconds once the class is loaded, and for a hot loop, once the JIT has compiled it. Process startup is paid once per test run, amortised across every test in it, which is why running a thousand more unit tests barely moves the total.
An integration test against a real dependency pays two separate costs. A fixed cost per container — image pull if not cached, docker create, docker start, and the wait strategy blocking until the dependency's own readiness check passes, commonly hundreds of milliseconds to a few seconds even from a warm image cache. And a marginal cost per test that is now bounded below by real I/O: a round trip to the database process, even over loopback, involves a context switch and actual work on the other end, which is orders of magnitude slower than an in-process method call whatever the network hop costs on top of it. An end-to-end test adds a third fixed cost — the whole application starting, every bean @SpringBootTest decided to load — and its marginal cost per test is bounded by the slowest hop in the request path, frequently a real socket rather than a mocked one.
The consequence for the shape follows directly: because a fixed cost is paid once per class or once per JVM rather than once per test, the way to make a slow layer affordable is to write fewer tests at that layer and make each one prove something the layer below structurally cannot — which is "catches what nothing below it can" from this lesson's table, not a taste in diagrams. The pyramid is what falls out of measuring that unit tests cost single-digit milliseconds and integration tests cost hundreds of them to low seconds, and then minimising total cost for the information gained.
Try it yourself
Do the arithmetic this lesson's own numbers imply
This lesson measured this site's suite: five in-process guards at 1.9 seconds combined, two container-based guards at 54 seconds combined. Suppose a change under review would convert ten checks currently living in the fast guards into ten new integration-level checks, each starting its own fresh Testcontainers container rather than sharing one per JVM. If a Postgres container takes roughly one to two seconds to start from a warm image cache, roughly how much slower does the suite get — and is that consistent with what this lesson says about where suites actually go slow?
Why the fixed cost, not the test count, is what to look at
Ten fresh containers at roughly one to two seconds of fixed startup each adds somewhere around ten to twenty seconds to the run, on top of whatever the tests themselves take — purely from paying a fixed cost ten times instead of once. That is exactly consistent with, and really just a restatement of, the "one container per JVM" habit the next lesson recommends: the naive layout (a container per test class) is expensive specifically because the fixed cost is paid repeatedly, not because integration tests are inherently many times slower to execute than unit tests. The fix is never "make each test faster" here — it is "pay the fixed cost once" — which is why that habit, not a faster assertion library, is the biggest lever on a suite shaped like this one.
Misconceptions
- "A slow suite means the tests themselves are slow." Often it means a fixed cost — a container starting, an application context starting — is being paid far more times than necessary. The fix, per the Under the hood section above, is amortising that fixed cost, not speeding up individual assertions.
- "Integration tests are integration tests because they're slower." Speed is a consequence of testing something with real, unavoidable I/O — a symptom of what the layer is for, not its defining property. A test that is slow without touching anything real is just a test written at the wrong level.
Sources
- Martin Fowler, "TestPyramid" (bliki).
- Mike Cohn, Succeeding with Agile — the original test automation pyramid.
- Google Testing Blog, "Just Say No to More End-to-End Tests".