DevPath · Learn to code ESPTEN

Testing the complete application

The testing pyramid

Why a pyramid?

Not all tests cost the same nor give the same confidence. The testing pyramid (Mike Cohn) is a heuristic for distributing effort: wide at the base, narrow at the top.

        /\        E2E      ← few: slow, fragile, high confidence
       /  \
      /----\     Integration ← some: several pieces together
     /      \
    /--------\   Unit       ← many: fast, cheap, isolated

The three layers

The cost / speed / confidence axis

As you go up the pyramid, each test covers more system (more confidence that the product works) but in exchange it is slower, more expensive to write and more prone to fail for reasons unrelated to the bug (timing, network, data). That's why you want many cheap tests at the bottom and few expensive ones at the top: maximum confidence per minute of execution.

Antipattern: the ice cream cone (many E2E, few unit). The suite takes hours, fails intermittently and nobody trusts it.

Examples

A unit test: fast and isolated

function priceWithVat(net, vat) {
  return Math.round(net * (1 + vat) * 100) / 100;
}
// The "test" checks a single unit, without network or DB:
console.log(priceWithVat(100, 0.21) === 121 ? "OK" : "FAIL");
Put this into practice

DevPath is a hands-on course: you read the theory here; in the app you put it into practice with exercises that really run, offline.

Start free in the app →
Unit and integration: doubles and assertions →