Software Testing

Intermediate path · lesson 2 of 19

IntermediateAutomation7 min readUpdated November 12, 2026

The test pyramid in practice

Knowing the pyramid is easy. Applying it takes test doubles you can trust, contract tests between services, a shape that fits your architecture and a pipeline that gives fast feedback. Here's how.

On this page

Key takeaways

  • Lower layers depend on test doubles (stubs, mocks, fakes). They make tests fast, but they can drift from reality, so something must check them against the real thing.
  • Contract tests check that services agree on their API without running them together. They replace many slow end-to-end tests.
  • The right shape depends on your architecture: a pyramid, a honeycomb for microservices or a trophy for front-ends. Stage tests in the pipeline and measure suite health to keep it in shape.

Where this picks up

This article assumes you know the three layers of the pyramid and why you want many fast tests at the bottom. Here we look at what makes it work in a real codebase, and what to do when it doesn't.

Test doubles: how lower layers stay fast

A unit test of the checkout code shouldn't call the real payment provider. Instead, it uses a test double: a stand-in for the real dependency. Gerard Meszaros named the common kinds in xUnit Test Patterns (2007):

DoubleWhat it doesExample
DummyFills a parameter; never usedAn empty customer object passed to satisfy a function signature
StubReturns fixed answersA pricing service that always returns 100.00
FakeA simple working versionAn in-memory database instead of a real one
SpyRecords how it was calledCounts how many emails were "sent"
MockExpects specific calls, and fails the test if they don't happenFails unless charge(149.90) is called exactly once

The risk: doubles that lie

A double is only as accurate as the assumptions behind it. If the real payment API renames a field, every stub of it keeps returning the old field. The unit tests stay green while production breaks.

Two habits keep doubles honest:

  • Verify doubles against reality. Every important double needs at least one integration or contract test that checks the real thing behaves the same way.
  • Don't mock what you don't need to. Tests full of mocks often just repeat the implementation: they check how the code works, not what it does, and break on every refactor.

Contract tests: integration without the full system

In a system of services, end-to-end tests that start everything together are slow and fragile. Contract tests check the agreement between two services instead.

With consumer-driven contracts, the approach popularised by the tool Pact:

  1. The consumer writes its expectations. The checkout service's tests say: "When I ask pricing for product 42, I expect a price field with a number."
  2. Those expectations become a contract, a file shared with the provider team.
  3. The provider verifies the contract against its real code, in its own pipeline, without starting the checkout service.
  4. If the provider breaks the contract, for example by renaming price to amount, its build fails before it's deployed.

Each service tests its side alone and fast. Together, the contracts cover the integration that would otherwise need slow end-to-end tests. For running contracts across many teams and services, see Contract testing for microservices.

The shape depends on your architecture

The pyramid was described for typical applications of its time. Two widely discussed alternatives adapt it to other architectures:

  • The testing honeycomb. In a 2018 post, Spotify's engineering team argued that for microservices, most tests should check each service through its API, with real dependencies like its database. They kept few tests of internal implementation details, and aimed for as few tests as possible that depend on other services being correct.
  • The testing trophy. Kent C. Dodds proposed this shape for front-end applications, also in 2018. It adds static analysis (type checking, linting) as the base and makes integration tests the largest layer, testing components together the way users use them.
ShapeLargest layerFits best
PyramidUnitApplications with lots of business logic in code
HoneycombService-level integrationMicroservices where most risk is in the connections
TrophyIntegration (plus static analysis)Front-end apps built from UI components

Stage the tests in your pipeline

Not every test needs to run on every change. Order them by speed so developers get feedback fast:

StageWhat runsTarget
Every commit or pull requestStatic analysis, unit, fast integration and contract testsA few minutes
After merging to mainWider integration tests, a small end-to-end smoke suiteUnder half an hour
Scheduled (nightly or before release)Full end-to-end, cross-browser, performance, security scansHours are acceptable

A failure at an early stage stops the later ones. That saves time, and points developers to the cheapest failure to investigate first.

Measure the suite's health

You can't keep the shape without watching it. Useful numbers:

  • Feedback time for a pull request. If it creeps up, developers stop waiting for it.
  • Flaky rate: how often tests fail and then pass with no code change. See Flaky tests.
  • Time to diagnose a failure. Long investigations often mean too many checks at the top.
  • Escaped bugs by layer: for each bug found in production, which layer should have caught it? Patterns show where the suite is thin.

From ice-cream cone to pyramid

Real-world exampleFrom a 3-hour suite to 12 minutes

A team's release checks were 400 browser tests, taking about 3 hours. Most failures turned out to be timing problems, not bugs, so people re-ran the suite "to see if it passes this time".

They tagged every test by what it actually checked. Most were checking business rules, like price calculations, through the UI.

They moved those checks down to unit and API tests, added contract tests for the two services checkout depends on, and kept about 40 browser tests for critical user journeys. The suite now runs in about 12 minutes on every pull request.

Lesson: the fix wasn't deleting tests. It was moving each check to the lowest layer that can catch the same problem.

To do the same:

  1. Inventory the top layer. For each end-to-end test, write down the rule or risk it really checks.
  2. Find what can move down. Calculations, validation and permissions usually belong in unit or API tests.
  3. Cover connections with contracts where services meet.
  4. Remove duplicates once the lower-level tests exist and have caught real problems.
  5. Keep a short list of critical journeys end to end: sign-up, checkout, whatever must never break.
  6. Track the numbers above, so the cone doesn't grow back.

Do

Replace the real payment provider with a stub in unit tests, and add a contract or integration test that checks the stub's assumptions still match the real API.

Don't

Mock every collaborator in every test. The tests will pass, break on every refactor and still miss the integration bugs that matter.

Check yourself

Your checkout service calls a pricing service owned by another team. An end-to-end test breaks every time pricing changes its API. What would you add, and what would you keep?

Show answer

Add consumer-driven contract tests: checkout records what it expects from pricing, and pricing verifies that contract in its own pipeline, so a breaking change fails their build before deployment. In checkout's own tests, use a stub of pricing. Keep one end-to-end smoke test of the full purchase journey.