QA Interview Hub · Knowledge base · Quizzes · Українська

QA Fundamentals › Testing basics

Unit, integration and e2e tests: what is the difference?

A unit test checks one function in isolation, an integration test checks the seams between components, and e2e runs a full user scenario. The wider the scope, the slower and costlier the test and the less precisely a failure shows its cause, so unit tests are the most numerous and e2e is kept for critical paths. The pyramid is a guide, not a law.

QA Fundamentals · Testing basics

All unit tests are green, and the user cannot apply a promo code. Where should this bug have been caught?

The project has a thousand unit tests, a hundred integration tests and five e2e tests. After the release the promo code is not applied. The developer says the discount function is tested, QA says there is no e2e test for the promo code, and the tech lead asks at which level this should have been caught and why exactly there. This lesson compares the three kinds of automated checks, shows what each of them sees and explains why one big suite does not replace the others.

How it works and why. The three kinds differ in scope, that is, in how much of the system takes part in the check. A unit test (component testing in ISTQB terms) checks one function or class in isolation: dependencies are replaced with stubs, so the test is fast and points exactly at the place of failure. An integration test checks how several components interact through their interfaces: a service and a database, a front end and an API, our code and someone else's service. An e2e test runs a full user scenario on a deployed system, usually through the interface. In the ISTQB CTFL 4.0 syllabus "e2e" is not a separate level: such scenarios usually belong to system testing (end-to-end scenarios with external systems also to system integration testing), and in practice the word e2e is used for any end-to-end test.

The wider the scope, the longer a test runs, the more factors can break it and the less precisely a failure points at the cause. Hence the test pyramid: most checks are placed where they are cheap and precise, and only a few end-to-end scenarios for critical paths are left at the top.

Unit, integration and e2e in short

KindWhat it checksSpeed and precision
UnitOne function or class, dependencies stubbedMilliseconds, a failure points at a line of code
IntegrationA seam between components: API and DB, service and neighbourSeconds, a failure points at the seam
E2EA full user scenario on a deployed systemSeconds to minutes, a failure says "something in the chain"

Example: a promo code that is not applied. One scenario, three possible causes, and each is found by its own kind of test:

CauseWho finds itWhy exactly this one
The discount function returns 990 instead of 900 for an amount of 1000 and a 10 % codeUnitThe error is in the logic itself, no other parts are needed
The cart sends the code in a promo field, while the discount service expects promoCode and answers "no discount"IntegrationEach side is right by its own assumption, the error is only at the seam
An overlay in the UI covers the "Apply" button in the browserE2EThe failure shows only in the real interface on a deployed system

No kind replaces the rest: unit tests pass even when the button is unreachable, and an e2e test can fail with no hint of the actual cause. A sensible mix is this: the discount logic is covered exhaustively by unit tests, the exchange format between services by a few integration tests, and one scenario "from the cart to payment with a promo code" is left in e2e.

Limits and alternatives. The pyramid is a guide, not a law: for applications where almost all logic sits in integration with a database or third-party APIs, teams use another ratio (often drawn as a "trophy" with a larger share of integration tests). The borders between the kinds depend on what a team counts as a component and as a system, so the names are worth agreeing. A unit test with a stubbed database and a test with a real database in a container belong to different kinds, even though teams often give both the same name, "unit" or "integration". The quality of e2e tests depends on stable locators and controlled data, otherwise such tests become a source of flakiness.

Common mistakes.

  • Covering everything with e2e only (an "inverted pyramid"): the suite is slow, flaky and a failure is hard to localise.
  • Believing that high unit coverage guarantees the system works: unit tests do not see seams and scenarios.
  • Mocking every neighbour and calling it an integration test: then the seam is not checked.
  • Duplicating the same check at all levels: it costs more without new findings.
  • Calling any UI test e2e: it is end-to-end only when it covers the whole user path.

Check yourself

The order service stores the amount in cents, while the payment service expects the main currency unit. Unit tests of both services are green. Which kind of test finds the mismatch most cheaply?

Answer

An integration test: it checks the data exchange between the two services with the real (or contract) format. Unit tests check each service against its own assumptions, and an e2e test would find it later and with no pointer to the cause.

A team has 300 e2e tests and almost no unit tests. What follows from this and what should be done?

Answer

The pyramid is inverted: the suite is slow, flaky and failures are hard to diagnose. Logic and edge cases move down to the unit level, the exchange format between components to the integration level, and only critical scenarios stay in e2e. Do not delete e2e tests without a replacement: first check that the lower levels cover the same things.

In an interview. Junior: A unit test checks one function in isolation, an integration test checks how components interact, and an e2e test runs a full user scenario through the system. Unit tests are the most numerous and e2e the fewest, because they are slow and expensive. Middle/Senior: I choose the kind of test by where the error can live: logic goes to unit, seams and exchange formats to integration, the critical user path to e2e. In ISTQB terms unit is component testing, and e2e is only a way of testing the system level. I treat the pyramid as a guide: the ratio depends on the product, but an inverted pyramid where everything rests on e2e gives a slow and flaky suite.

More articles in this chapter