Test pyramid
The test pyramid is a model for the shape of an automated test suite: many fast, isolated unit tests at the base, fewer tests through a service or integration layer in the middle, and very few tests that drive the user interface at the top. Mike Cohn described it in his 2009 book Succeeding with Agile, and it is also called the testing pyramid or test automation pyramid.
The longer version
Martin Fowler's bliki entry on the test pyramid, dated 1 May 2012, credits the model to Mike Cohn, who described it in Succeeding with Agile in 2009 under the name test automation pyramid. Fowler adds that Cohn first drew it in a conversation with Lisa Crispin in 2003 or 2004 and showed it at a Scrum gathering in 2004, and that Jason Huggins came up with the same idea independently around 2006. Cohn's own layers are unit tests at the base, service tests in the middle and user interface tests at the top. In his blog post The Forgotten Layer of the Test Automation Pyramid he calls the middle layer the one teams forget, and describes testing everything through the user interface as brittle, expensive and time consuming. Testing pyramid and test automation pyramid name the same model.
The pyramid is a statement about proportions, and it gives no ratio. Ham Vocke's The Practical Test Pyramid, published on Fowler's site on 26 February 2018, reduces it to two rules: write tests with different granularity, and the more high-level you get, the fewer tests you should have. The layer names vary between teams. Vocke calls service test a term that is hard to grasp, and the version most diagrams show today reads unit, integration and end-to-end. Contract tests, which check that two services agree on the shape of their API, usually sit beside the integration tests. What the shape encodes is cost. A unit test runs in milliseconds and fails at a named function. A test that drives a browser through a whole journey takes seconds or minutes, depends on every service in the chain, and when it fails it says only that the journey broke somewhere. So each check is written at the lowest level that can prove it, and the top layer is kept for the few journeys where the interface itself is what needs proving.
The usual failure is the shape upside down. Fowler's entry warns that a suite built mostly from tests driven through the user interface quickly runs into trouble and becomes an ice-cream cone. In the usual drawing of that cone, the pyramid is upside down: a wide layer of browser tests, a scoop of manual checks on top, and very few unit tests underneath. Such a suite is slow enough that people stop waiting for it, and brittle enough that a layout change breaks dozens of tests at once. Fowler also calls UI-driven tests prone to non-determinism, which is how a red build becomes something the team reruns until it turns green. The remedy is to move each check down: the business rule behind a checkout total belongs in a unit test, the call to the payment provider in an integration or contract test, and the browser test proves only that a person can reach the confirmation page.
Manual and exploratory testing are not a layer of the pyramid, because the pyramid describes automated tests. Neither Cohn's post nor Fowler's entry places them, although many diagrams now add a cloud of manual testing above the point. The TMAP body of knowledge publishes an adapted pyramid that shows manual testing along one side, done while the automated tests are written, and manual exploratory testing by end users and business stakeholders at the top. Vocke writes that some manual testing is still a good idea however good the automation is, and that a bug found by exploring should become an automated test so that it cannot return. The agile testing quadrants answer the other half of the question: they sort testing by purpose, and exploratory testing, usability testing and user acceptance testing sit in the quadrant that critiques the finished product. On a website, that is where a person notices that a menu overlaps the heading on a narrow phone or that an error message makes no sense. The finding is filed with its evidence, fixed, and then pinned by a test at the lowest level that would have caught it.
Common questions
What is a test pyramid?
A test pyramid is a model for balancing an automated test suite. The wide base is many small, fast unit tests, the middle is fewer tests of services and integrations, and the narrow top is a handful of tests that drive the user interface. The higher a test sits, the slower and more fragile it is, so the suite holds fewer of them.
Who created the test pyramid?
Mike Cohn described the model in his 2009 book Succeeding with Agile, where he called it the test automation pyramid. Martin Fowler's 2012 bliki entry credits Cohn, notes that he first sketched it with Lisa Crispin in 2003 or 2004, and adds that Jason Huggins reached the same idea independently around 2006.
What are the layers of the test pyramid?
Cohn's layers are unit tests, service tests and user interface tests, from the bottom up. Most diagrams today label them unit, integration and end-to-end. Names matter less than the rule behind them, which Ham Vocke puts as writing tests with different granularity and having fewer of them the more high-level they get.
What ratio of unit, integration and end-to-end tests should a suite have?
Neither Cohn's post nor Fowler's articles on the pyramid gives a ratio, so any fixed percentage split is a team's own choice. The model is relative: more unit tests than integration tests, and more integration tests than end-to-end tests. A useful check is to look at a failing end-to-end test and ask whether a lower-level test could have caught the same fault.
Where does manual testing fit in the test pyramid?
Manual testing sits beside the pyramid, because the pyramid describes automated tests. Exploratory sessions, usability checks and acceptance by the people who asked for the work look for problems no assertion was written for. TMAP's adapted pyramid draws manual exploratory testing at the top, and Ham Vocke recommends turning each bug found that way into an automated test.
What is the ice-cream cone anti-pattern?
The ice-cream cone is the test pyramid upside down: most automated tests drive the user interface, manual checks sit on top, and few unit tests sit underneath. Martin Fowler uses the term for suites that lean on UI-driven tests, which he describes as brittle, expensive to write and time consuming to run.
Is the test pyramid still relevant?
The two rules behind it still hold for most products: test at several levels, and keep the slow, broad tests few. Critics argue that the popular three-layer drawing leaves things out. Valentina Jemuović, for example, writes that it says nothing about acceptance, component or contract tests and gives no guidance for microservices. Many teams keep the shape and rename or add layers to fit their architecture.
Bug reports that arrive ready to triage.
Collect replay, console, network and browser context with the Free plan.