The Test Pyramid

Not Everything Should Be a UI Test

UI tests are the slowest and most fragile kind of test. The test pyramid says: many fast unit tests, fewer API tests, and a small number of UI tests for the most important user journeys.

/\
           /UI\          ~50 tests   : login, search, checkout journeys (minutes)
          /----\
         / API  \        ~400 tests  : every endpoint, validation, business rules (seconds)
        /--------\
       /   Unit   \      ~3000 tests : prices, coupons, tax, mappers (milliseconds)
      /------------\

Note: The opposite shape, an ice-cream cone (mostly UI tests, few unit tests), is common in teams that started with manual testing. It leads to 3-hour suites and many flaky failures.

Why It Matters

Example

Compare suite time for the same 1,000 checks spread in two ways. Average times per test are typical values:

import java.util.*;

public class Main {
    static String minutes(double seconds) { return String.format("%.1f min", seconds / 60); }

    public static void main(String[] args) {
        double unit = 0.01, api = 0.4, ui = 25;      // seconds per test
        Map<String, int[]> shapes = new LinkedHashMap<>();
        shapes.put("Pyramid  ", new int[]{800, 170, 30});
        shapes.put("Ice cream", new int[]{100, 100, 800});

        shapes.forEach((name, n) -> {
            double total = n[0] * unit + n[1] * api + n[2] * ui;
            System.out.println(name + " unit=" + n[0] + " api=" + n[1] + " ui=" + n[2]
                    + " -> " + minutes(total) + " on one thread");
        });
    }
}

Output

Pyramid   unit=800 api=170 ui=30 -> 13.8 min on one thread
Ice cream unit=100 api=100 ui=800 -> 334.0 min on one thread