← Back to libraryQuestion 220 of 273
🔧CI/CDBeginner

Where Tests Fit: the Test Pyramid in a Pipeline

📌 Definition:

The test pyramid describes the ideal proportion of test types — many fast unit tests at the base, fewer integration/API tests in the middle, and a small number of slow end-to-end tests at the top. In a pipeline, this shape dictates the order and parallelism of test stages so feedback is fast.

📖 Detailed Explanation:

The pyramid isn't just about counts; in CI it becomes an execution strategy. Fast, cheap unit tests run first (and often on pre-commit), so obvious breakages fail in seconds. Integration and API tests run next, validating how components and services interact. Slow, brittle-prone end-to-end tests run last and in smaller numbers, because they're expensive and their failures are harder to diagnose. Arranging stages this way — fail-fast on the cheapest signal — gives developers feedback in the right order: a broken unit test shouldn't wait 20 minutes behind an E2E suite. An inverted pyramid (mostly E2E) produces slow, flaky pipelines that teams learn to ignore, which is a classic anti-pattern interviewers probe for.

🔑 Key Points:
  • Base: many fast unit tests; middle: fewer integration/API; top: few E2E
  • Run cheapest tests first so obvious failures fail fast
  • E2E is slow and flakier — keep it small and run it last/in parallel
  • Inverted pyramid (mostly E2E) = slow, flaky, ignored pipelines
  • The pyramid is both a coverage strategy and a pipeline execution order
🌍 Real-World Example:

A pipeline runs 3,000 unit tests in 40 seconds first; if they pass, 400 API tests run in 3 minutes; only then do 30 E2E journeys run in 5 minutes. A developer who breaks a function gets a red build in under a minute instead of waiting 8 minutes for the whole thing.

📎 Testing Layers:
LayerCountSpeedPipeline PositionFailure Diagnosis
UnitMany (thousands)MillisecondsFirst / pre-commitPinpoints exact function
Integration / APIFewer (hundreds)SecondsMiddle stagePoints to a component boundary
End-to-EndFew (tens)Seconds to minutesLast stageBroad — needs traces/screenshots
🎯 Scenario-Based Interview Question:

Scenario: A team's CI takes 35 minutes and developers routinely merge without waiting for it. Investigation shows 90% of the tests are Selenium/Playwright E2E tests and there are almost no unit tests. How do you diagnose and fix this?

💡 Quick Tip:

When asked to 'speed up CI', reach for the pyramid first — the fastest win is usually moving coverage down from E2E to unit/API, not just parallelizing.