Unit, Integration or End-to-End Tests?

Arguments about the right mix of tests go on forever. The useful question is simpler: which test will catch the bug you are most worried about, at the lowest cost?

Unit, Integration or End-to-End Tests? — Troiana insight cover

In short

Unit tests check one small piece of logic, and they're fast and cheap. Integration tests check that parts work together, such as code and a database. End-to-end tests drive the real app like a user, and are slower, but offer the most confidence. Start with a few end-to-end tests on your critical journeys, use integration tests for most of your logic, and unit tests for tricky calculations.

The three kinds

Unit tests check one function or small piece of code on its own.

test("adds VAT", () => {
  expect(addVat(100, 0.2)).toBe(120);
});

Integration tests check that several parts work together, such as your API, your logic and a real test database.

test("a customer can't read another customer's invoice", async () => {
  const res = await apiAs(customerA).get("/invoices/" + invoiceOfB.id);
  expect(res.status).toBe(404);
});

End-to-end (E2E) tests control a real browser, and use your app as a person would.

test("customer can check out", async ({ page }) => {
  await page.goto("/products/blue-shoes");
  await page.getByRole("button", { name: "Add to basket" }).click();
  await page.getByRole("link", { name: "Checkout" }).click();
  await expect(page.getByText("Order summary")).toBeVisible();
});

The comparison

UnitIntegrationEnd-to-end
SpeedMillisecondsOften under a second to secondsSeconds to minutes
ConfidenceLow to mediumMedium to highHigh
MaintenanceLow, unless overly tied to the codeMediumHigher
FlakinessRareOccasionalMore common
When it failsPoints to the exact spotPoints to an areaSays something is broken
Best forTricky logicAPIs, data rules, permissionsCritical user journeys

Pyramid or trophy?

The classic testing pyramid, described by Martin Fowler among others, recommends many unit tests, fewer integration tests and very few end-to-end tests, because the higher you go, the slower and more fragile things get.

Kent C. Dodds' testing trophy puts more weight on integration tests: “Write tests. Not too many. Mostly integration.” His reasoning is that these give the most confidence for the effort, particularly in web apps where much of the risk lies in how parts connect.

Each has a point. A financial calculation engine deserves lots of unit tests. A typical CRUD web app has little complex logic, so most of its bugs are found at the boundaries. Choose based on where your risk is, not on a diagram. See what you should actually test.

Pyramid
End-to-end
Integration
Unit
Trophy
End-to-end
Integration
Unit
Simplified shapes, where width suggests how many tests of each kind. Each has a point, and a calculation engine would lean toward the first.

Where to start in a real project

If you've got few or no tests, here's an order that works:

1. Write three to five E2E tests of your critical journeys. Signing up, logging in, the main action in your product, paying. Tools such as Playwright make this fairly straightforward. They tell you immediately if something essential breaks.

2. Add integration tests for data and permissions. Particularly access control: can user A reach user B's data? Those are the bugs that end companies, and you can't see them just by looking at the interface. See how to design roles and permissions before the UI.

3. Add unit tests for tricky logic. Prices, taxes, dates, discounts, permissions and parsing. Whenever a function has lots of edge cases, unit tests are the cheapest way to cover them.

4. Add a regression test for each bug you fix. This way you'll never fix it twice, and your test suite grows in the places that matter.

5. Put them in CI, so they run on every change. See web app testing before launch.

How to decide for each piece of code

Ask what could go wrong, and then choose the cheapest test that would catch it.

The worryTest to use
VAT calculated wrongly at the edge of a dateUnit
Customer sees someone else's dataIntegration
Database query fails with real constraintsIntegration
The checkout button stopped workingEnd-to-end
Payment webhook handled twiceIntegration
The layout broke on mobileE2E or visual regression
The form can't be used with a keyboardE2E, plus accessibility checks

Common mistakes

Mocking everything. If your test replaces the database, the API and the payment layer with fakes, it may pass while the real thing fails. Mock only what's outside your control, such as third-party services.

Testing implementation details. Tests that break each time you refactor, even when nothing's wrong, are a burden. Test behaviour instead.

Chasing a coverage number. 100% coverage doesn't show that a test checks anything useful. It's a useful way of finding untested areas, but it's a poor goal.

Ignoring flaky tests. Once people stop trusting the tests, they stop reading the results. Fix flaky tests or delete them. Don't simply retry forever.

Too many E2E tests. A two-hour test suite won't get run. Keep E2E tests to what really matters.

Testing only the happy path. Include failed payments, expired sessions and empty states.

AI-written tests

Agents can write tests very quickly, which is useful, but watch out for:

  • Tests that confirm what the code does, bugs included, rather than what it should do
  • Weak assertions, such as checking merely that a response exists
  • Excess mocking, so that nothing real is being tested
  • Deleted or weakened tests, to make failures go away. Check for this in diffs.
  • Large volumes of near-identical tests, which need maintenance

A good practice is to describe the required behaviour first, have the agent write a failing test, and then have it write the fix. Break the code on purpose, to confirm that the test notices. See how to test a vibe-coded app.

How much is enough?

Enough that you can deploy on a Friday afternoon without feeling worried. If you've got a critical journey that you always check by hand before a release, that's the next test to write. For a very early prototype, a single E2E test can be sufficient. For software handling money or medical data, you'll need far more.

If you'd like help building a test suite that catches real problems without slowing your team down, book a call.

Common questions

What is the difference between unit and integration tests?

A unit test checks one small piece of code by itself. An integration test checks that several parts work together, such as your API and a database. Unit tests are faster, while integration tests catch more real-world problems in typical web apps.

Should I write unit or end-to-end tests first?

In an existing app with few tests, start with a few end-to-end tests of your critical journeys, as they protect what matters most. Then add integration tests for data and permissions, and unit tests for tricky logic.

How many end-to-end tests do I need?

Usually only a handful to a few dozen, covering your critical journeys, such as sign-up, login and payment. They're slow and sometimes fragile, so test detailed cases at lower levels. Keep the suite short enough that people actually run it.

What is the testing pyramid?

It's a model suggesting many fast unit tests, fewer integration tests and very few end-to-end tests. Others prefer the testing trophy, with more integration tests. Choose the balance according to where the risk lies in your particular software.

Have something worth building right?