The test pyramid: unit, integration, API, and end-to-end tests
coreintermediateA unit test checks one function in isolation. An integration test checks two real pieces together. An API test checks a running service through HTTP. An end-to-end test drives the whole system like a real user.
Think of it as
Picture a pyramid: many fast, narrow unit tests at the base, fewer integration tests above them, and a handful of slow, broad end-to-end tests at the top. Each layer up costs more time and tells you less about exactly what broke, but proves more about whether the real system actually works.
What we're doing: Show the same behavior tested at two different layers — a unit test with a mocked dependency, and an integration test against a real one — to make the speed/confidence tradeoff concrete.
- 10
- This test only exercises the validation branch — a real payment gateway is irrelevant to it, so mocking is correct here.
- 17
- Still a unit test: it checks that charge_customer calls the gateway correctly, without needing a real payment network.
2 passedWhy this works: Both tests stay fast and isolated because the payment gateway — the expensive, external dependency — is mocked. An integration test would swap Mock() for a real (sandboxed) gateway client to prove the actual wiring works, at the cost of needing network access and running slower.
Writing only end-to-end tests because they feel most "real"
Wrong
Better
What you see: A single failing end-to-end test gives almost no clue which step broke, and the whole suite runs slowly because every test drives the full stack.
Why: End-to-end tests prove the pieces are wired together, but they are the wrong layer for pinpointing a specific bug — a broad failure with many possible causes is expensive to debug. Most coverage should live in fast, narrow unit tests; end-to-end tests confirm the pieces fit, in smaller numbers.
- End-to-end — the whole system, like a real user — slowest, broadest
- API — a running service via HTTP — seconds
- Integration — two+ real components together — seconds, real I/O
- Unit — one function, dependencies mocked — milliseconds, most coverage lives here
The four everyday test types
Together
Remember: Unit tests are fast and narrow; integration/API/end-to-end tests grow slower and broader — build a pyramid.
See also: regression contract and performance tests · mock and magicmock

