Test pyramid
Пирамида тестирования
A way to organize automated tests: use many small tests for individual pieces, fewer tests for how those pieces work together, and a small set of tests for complete workflows.
What the pyramid is for
When a feature needs testing, you don't have to run the whole application for every case. The test pyramid describes a suite with a broad base of focused tests, a smaller layer of tests that exercise components together, and a handful of end-to-end checks at the top. The goal is fast feedback without overlooking failures that only show up when the system is assembled.
The three layers
- Unit tests exercise a small piece of behavior, such as a function that decides whether a booking can be canceled. They usually run without a database or network, so it's cheap to check many inputs and pinpoint a failure.
- Integration tests check a boundary between components. A test that runs your data access code against a real test database can catch a mismatch between the query and the schema. A unit test that replaces the database with a test double can't catch that mismatch.
- End-to-end tests exercise a complete workflow through the system's public interface. A browser is one entry point, not a requirement: an API-only product can start an end-to-end test with a request to its public API and verify the workflow's final outcome.
Broader tests tend to need more setup, take longer, and leave more possible explanations when they fail. The extra scope is worthwhile when the failure would be invisible in a narrower test.
Example: canceling a booking
Suppose customers can cancel for free up to 24 hours before a booking starts. Use unit tests for the time rule, including exactly 24 hours and just either side of it. Use an integration test with a real test database to check that saving the cancellation releases the reserved slot. Then run an end-to-end test in which a customer cancels through the website, sees the confirmation, and finds that the slot is available to book again. There's little value in repeating every time boundary in the browser, but the full flow can catch a wiring mistake, such as a cancel button calling the wrong endpoint.
How to apply it in practice and in interviews
Interviewers often describe a bug and ask where you'd test for it. Name the failure first, then choose the narrowest test that can reliably catch it. If the bug is that the cutoff time is calculated incorrectly, test the rule directly. If the button sends the wrong request, a passing test of the rule tells you nothing about that problem.
The pyramid isn't a required ratio or a measure of code coverage. A service that mostly coordinates a database and a message queue may get more value from integration tests than from lots of isolated tests with mocked dependencies. If the CI suite is slow or flaky, inspect what each expensive test actually protects before moving it down a layer: keep the broad checks that catch distinct failures, and move repetitive cases only when a narrower test can give you the same confidence.