Quality Assurance·

Unit vs Integration vs End-to-End Testing: Differences and Examples

Unit, integration and end-to-end tests compared: what each checks, worked examples, how many of each to write, and which should run on every pull request.

QA.tech

A brass core inside a frosted acrylic sphere inside a clear glass sphere on a gunmetal stand, representing unit, integration and end-to-end tests on one feature.

A unit test checks one piece of code on its own. An integration test checks that a few pieces work together. An end-to-end test checks that a user can get through a whole journey in the running app.

Each level catches bugs the others miss, so the useful question is how much of each you need and when each one runs.

The three levels at a glance

Unit testIntegration testEnd-to-end test
ScopeOne function, class or componentTwo or more parts, such as a service and its databaseA full user journey
DependenciesMocked or stubbedSome real, some mockedReal, or as close as you can get
Typical speedMillisecondsSecondsSeconds to minutes
Who usually writes itThe developer, alongside the codeThe developer or a QA engineerQA engineers, developers, or increasingly an agent
A failure tells youWhich function is wrongWhich connection is wrongThat a user can't finish the journey
Main riskPassing while the system is brokenSlow, needs infrastructureFlaky, slow, expensive to maintain

The definitions used by the ISTQB, the body behind the CTFL tester certification, line up with this. Its glossary defines integration testing as a test level "that focuses on interactions between components or systems". There's no ISTQB level called end-to-end. E2E testing cuts across its system and acceptance levels, which is part of why the terms get muddled.

One feature, three tests

Say you're adding a discount code field to checkout. Here's what each level would test.

Unit test

The function that applies a discount gets a cart total and a code, and returns the new total:

test('10% code takes 10% off the subtotal', () => {
  expect(applyDiscount({ subtotal: 200 }, 'SAVE10')).toEqual({ subtotal: 200, discount: 20, total: 180 });
});

test('expired code is rejected', () => {
  expect(() => applyDiscount({ subtotal: 200 }, 'SUMMER2024')).toThrow('expired');
});

These run in milliseconds and you can write dozens: rounding, currencies, stacked codes, minimum order values. They say nothing about whether the field on the page calls this function.

Integration test

The checkout API receives a code, looks it up in the database and returns the discounted cart. An integration test starts the API against a test database seeded with a known code and checks the response:

test('POST /cart/discount applies a valid code', async () => {
  await db.discountCodes.insert({ code: 'SAVE10', percent: 10, expiresAt: nextYear });
  const res = await api.post('/cart/discount', { cartId, code: 'SAVE10' });
  expect(res.status).toBe(200);
  expect(res.body.total).toBe(180);
});

This catches a wrong column name, a missing migration or a query that ignores the expiry date. It doesn't catch the frontend sending the code under the wrong field name.

End-to-end test

The E2E test opens the shop, adds a product, enters the code at checkout and checks that the total on the page and on the confirmation both show the discount. It's the only one of the three that fails if the form sends discount_code while the API expects code. It's also the slowest, and it fails if the payment sandbox is down that afternoon.

Integration testing vs end-to-end testing

This is the pair people mix up most. Two questions separate them.

Does the test go through the user interface? Integration tests usually call code or APIs directly. E2E tests usually drive a browser or a device.

Does the test cover a full journey or a single connection? An integration test checks one seam between parts. An E2E test checks a sequence a user would actually do, across every seam it touches.

A test that calls three services in a row through their APIs, without a UI, sits in between. Some teams call it an API end-to-end test, others a broad integration test, and in enterprise settings it's often called system integration testing. The label matters less than knowing what environment it needs and what a failure tells you.

How many of each should you write?

The classic answer is the testing pyramid, which Martin Fowler describes in TestPyramid: many unit tests at the base, fewer integration tests, and a small number of E2E tests at the top. Mike Wacker's 2015 Google Testing Blog post puts a number on it, a 70/20/10 split, as "a good first guess".

The reason is cost and noise. Google's own analysis found that about 14% of its large tests were flaky, against 0.5% of small ones. More E2E tests means more runs that fail for reasons unrelated to your change.

Plenty of teams don't follow the pyramid, and for good reasons. A frontend-heavy app with a thin API gets little from integration tests. A product built on third-party services can have perfect unit tests and still break every week at the seams. Kent C. Dodds' "testing trophy" shifts weight towards integration tests for that reason, and Alan Myrvold wrote on the same Google blog in 2020 about fixing a test hourglass, where teams had plenty of unit and E2E tests and too few in the middle.

On one call we reviewed for this piece, a fintech team with no QA engineers described its setup as a "testing diamond": Playwright for E2E, a wide band of backend integration tests and unit tests underneath. Their developers spent about a fifth of their time on testing. The shape suited their product, and it'll be different for yours.

Daniel Mauno Pettersson, our co-founder and CEO, argues for testing as close to production as possible and moving away from mocks wherever you can. His advice for the top layer is to keep a few scripted E2E tests for the most important journeys, the ones that don't change much, and accept that the long tail of journeys is too big to cover with scripts.

Which tests run on a pull request?

All three, in order of speed, with a different job each.

  1. Unit tests run on every push, in seconds. They gate the merge.
  2. Integration tests run on every pull request, against a test database or containers. They gate the merge.
  3. End-to-end tests run on the pull request against a preview deployment of the branch, scoped to what the change touches. A short critical-path suite runs again after merge on staging.

Step 3 is where most teams fall short. Running the whole E2E suite on every pull request is too slow, so many teams only run it nightly, and the bug a pull request introduced is found a day later by someone who didn't write it. Testing the journeys that a change affects, on that change's own preview, closes the gap. Our step-by-step guide to running E2E tests on every pull request with GitHub Actions covers the scripted setup.

Where QA.tech fits

QA.tech, an AI testing platform, only works at the end-to-end level. It doesn't write or run unit tests or integration tests in your codebase, and you still need both. It drives the app through a Chromium browser or calls your APIs, and it can't reach into your database to set up state.

What it adds is the per-change E2E layer. On each pull request with a preview deployment, an agent reads the description and linked tickets, works out which journeys the change could affect, and tests them in the running preview. You get a verdict on the pull request before the merge, and only the tests worth keeping are added to the regression suite.

FAQ

What is the main difference between unit testing and integration testing? A unit test checks one piece of code with its dependencies replaced by mocks. An integration test checks that real pieces work together, such as an API and its database, so it catches problems in the connections that unit tests skip.

What is an example of integration testing? Calling a checkout API that reads discount codes from a real test database, and checking that it returns the right total. The test covers the API, the query and the database schema together, but not the user interface.

What is the difference between end-to-end testing and integration testing? Integration tests check one connection between parts, usually without a UI. End-to-end tests check a whole user journey in the running application, usually through a browser, across every part the journey touches.

Is end-to-end testing the same as system testing? They overlap. System testing checks the complete system against its requirements, which can include performance and security. End-to-end testing focuses on user journeys through that system. Most E2E tests count as system tests, but not every system test is an E2E test.

What are the three types of software testing? People usually mean the three levels in this piece: unit, integration and end-to-end. Formal models such as the ISTQB's list more levels: component, component integration, system, system integration and acceptance testing.

Should developers write integration and end-to-end tests? Developers usually write unit and integration tests with the code. E2E tests are more often shared with QA, and in teams without QA engineers, developers write them or run them automatically on each pull request.

Do you still need unit tests if you have end-to-end tests? Yes. Unit tests are what let you change code quickly and tell you exactly which function broke. E2E tests are too slow and too broad to do that job.

Your team moves fast. Can your testing keep up?

Our QA agents test your product autonomously, so moving fast never means shipping broken. Start free or get a 30-minute demo.