QA AND TEST AUTOMATION · PART 3 · AUTOMATION FUNDAMENTALS · STRATEGY · 29 OF 112

The Test Pyramid vs Testing Trophy, Explained

How to balance a test suite: the test pyramid (many unit tests, fewer service tests, few UI tests), why it works (speed, cost, how precisely a failure points at the bug), the testing trophy and its focus on integration tests, the ice-cream cone anti-pattern, and how to pick the right layer for each ShopRung rule.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 67
  63. 68
  64. 69
  65. 70
  66. 71
  67. 72
  68. 73
  69. 74
  70. 75
  71. 76
  72. 77
  73. 78
  74. 79
  75. 80
  76. 81
  77. 82
  78. 83
  79. 84
  80. 85
  81. 86
  82. 87
  83. 88
  84. 89
  85. 90
  86. 91
  87. 92
  88. 93
  89. 94
  90. 95
  91. 96
  92. 97
  93. 98
  94. 99
  95. 100
  96. 101
  97. 102
  98. 103
  99. 104
  100. 105
  101. 106
  102. 107
  103. 108
  104. 109
  105. 110
  106. 111
  107. 112
1 / 12

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Many unit, some API, few UI
  • lowest layer that catches the bug
  • trophy: mostly integration
  • avoid the ice-cream cone

Notes

The pyramid

The test pyramid, made popular by Mike Cohn, has three layers. At the bottom, many unit tests that check small pieces of code. In the middle, fewer service or API tests that check parts working together. At the top, a few UI tests that drive a real browser. Higher up, tests are slower, cost more to write and keep, and flake more, but they are more realistic. Lower down, they are fast, cheap, and point straight at the broken line.

The test pyramid
        /  UI  \      few
       /---------\
      /  service  \    some
     /-------------\
    /     unit      \  many

higher up: slower, costlier,
  more realistic, more flaky
lower down: faster, cheaper,
  points at the exact bug

Why the shape

Why that shape? Speed and precision. A unit test takes milliseconds, and when it fails it points at one function. An API test takes tens of milliseconds to a second, and points at one endpoint. A UI test takes seconds, and when it fails the bug could be in the page, the API, the data or the network. A thousand unit tests run in seconds. A thousand browser tests can take hours, which no developer waits for.

Rough costs (per test)
  • unit ~ milliseconds fails → one function
  • API ~ tens of ms to a second fails → one endpoint
  • UI ~ seconds to tens fails → somewhere in UI, API, data or network
  • 1,000 unit tests: seconds
  • 1,000 UI tests: often hours

Choose the layer

The rule of thumb: test each rule at the lowest layer that can catch its bug. ShopRung's money rules, coupons, rounding and shipping, are pure calculations, so all their boundaries belong in unit tests. The quantity limit and the order states are enforced by the API, so API tests cover them, including the invalid moves. Privacy needs two users, still at the API. The browser is kept for what only a browser can show: one checkout journey from start to end.

ShopRung rules by layer
  • PRICE-3, 6, 9 money rules → unit: all boundaries
  • CART-2 quantity limit → API: 0, 1, 10, 11
  • ORD-4 order states → API: valid + invalid
  • ORD-7 privacy → API with two users
  • checkout journey → UI: one happy path

The trophy

The testing trophy, from Kent C. Dodds, is a different shape for modern web apps. At the base, static checks: types and linting catch whole classes of bugs for free. Then some unit tests. The biggest part is integration tests, which check pieces working together with few mocks, because that is where the confidence is. A small top of end to end tests. The idea: most of your confidence should come from tests that resemble how the software is used.

The testing trophy
        ___ e2e ___
       |integration|  ← most
       |           |
        \  unit   /
         \_______/
         static

static: types, linting
"write tests, not too many,
 mostly integration"

Pyramid or trophy?

Pyramid or trophy? They agree more than they disagree. Both say: keep end to end tests few, and get fast feedback first. The pyramid leans on unit tests, which fits back ends full of business logic, like ShopRung's pricing. The trophy leans on integration tests, which fits front ends and services that mostly connect things. The honest answer in an interview: pick the shape by where your bugs actually live, and measure it.

They agree more than not
  • both: few end-to-end tests
  • both: fast feedback first
  • pyramid: unit-heavy, fits logic-rich back ends
  • trophy: integration-heavy, fits UI apps and services with thin logic
  • pick by where YOUR bugs live

The ice-cream cone

The shape to avoid is the ice cream cone: lots of manual testing on top, then many UI tests, few API tests, and almost no unit tests. It is common in teams that started automation from the UI. The symptoms are familiar: a suite that takes hours, fails randomly, and that nobody trusts when it goes red. The fix is gradual: when a UI test checks a rule, move that check down to an API or unit test, and keep the UI test only for the journey.

The ice-cream cone
   \  manual tests  /
    \--------------/
     \  UI tests  /    many
      \----------/
       \  API   /     few
        \------/
         \unit/       almost none

symptoms: 3-hour suite, flaky,
nobody trusts red builds

Contract and component

Real suites have more than three layers. Component tests check one piece of the user interface in isolation, like a cart table, without the whole app. Contract tests check that two services agree on an API, so they can be tested apart; you will meet Pact in Part four. Visual and accessibility checks usually ride along with UI tests, which is Part five. The shapes are a guide for balance, not a strict count.

In-between layers
  • component tests: one UI component in isolation (Playwright component testing, Storybook)
  • contract tests: services agree on their API (Pact, Part 4)
  • visual and a11y checks ride on UI tests (Part 5)

Who owns what

Who writes which tests? Usually developers write unit tests, and SDETs review them for missing edge cases. API and integration tests are shared, often led by SDETs. UI journeys are mostly SDETs, with developers adding cases for their features. SDETs also own the framework, the pipeline and the reports. But the suite belongs to the whole team, and a modern SDET coaches developers to test well rather than testing everything alone.

Typical ownership
  • unit: developers, reviewed by SDETs for edge cases
  • API / integration: shared; SDETs often lead
  • UI journeys: SDETs, with developers adding cases
  • framework, CI, reports: SDETs
  • one suite, one owner: the team

Common mistakes

Shape mistakes that slow teams down. Testing every rule through the browser. Repeating the same check at three layers, so one bug fails five tests. Unit tests so full of mocks that they only test the mocks. The opposite mistake, no end to end tests at all, so nobody knows if the pieces work together. Counting tests instead of asking what bugs they catch. And ignoring how long the suite takes, until it is too late.

Avoid these
  • every rule tested in the UI
  • duplicating the same check at three layers
  • unit tests full of mocks that test nothing real
  • zero end-to-end tests
  • counting tests instead of asking what they catch
  • ignoring suite run time

Say it like this

In the interview, say: the pyramid means many fast unit tests, fewer API or integration tests, and few UI tests, because lower tests are faster, cheaper and point straight at the bug. I test each rule at the lowest layer that can catch it, and keep the browser for user journeys. Then show nuance: for UI heavy apps, the testing trophy, with more integration tests, often fits better, and I would choose based on where our bugs come from.

In the interview
  • "Many fast unit tests, fewer
  • API or integration tests, few
  • UI tests, because lower tests
  • are faster, cheaper and point
  • at the bug. I test each rule
  • at the lowest layer that can
  • catch it and keep the UI for journeys. For UI-heavy apps
  • the trophy, more integration, often fits better."

Interview questions

Q1What is the test pyramid?

A model for balancing a suite: many fast unit tests, fewer service/API tests, and a few UI end-to-end tests, because lower-level tests are faster, cheaper and more precise.

Q2What is the testing trophy?

A model (from Kent C. Dodds) with static checks at the base, some unit tests, most effort on integration tests and a few end-to-end tests, aiming for confidence from tests that resemble real use.

Q3What is the ice-cream cone anti-pattern?

A suite dominated by manual and UI tests with few API and unit tests: slow, flaky and expensive to maintain.

Q4How do you decide at which level to test a rule?

At the lowest level that can catch its bug: calculations in unit tests, endpoint rules in API tests, and only user journeys in the UI.

Q5Why are UI tests at the top of the pyramid?

They are the slowest, most expensive to maintain and most prone to flakiness, and a failure does not point precisely at the cause.

Q6Does the pyramid mean you should have no UI tests?

No. A small set of end-to-end tests checks that the pieces work together for key journeys; they are just kept few.

Q7How would you fix an ice-cream cone suite?

Gradually push checks down: replace UI tests of business rules with API or unit tests, keep UI tests for journeys, and track suite time and flakiness.

Q8What are contract tests in relation to the pyramid?

Tests between unit and end-to-end that check two services agree on an API, letting them be tested and deployed independently.