EasyConcept

What is the difference between unit, integration and end-to-end tests?

Technology:
Software Testing
Experience:
Intern,
Junior
Topics:
Testing

Quick answer

Unit tests check one small piece of code in isolation, integration tests check that several pieces work together (for example code plus a real database), and end-to-end tests drive the whole application the way a user would.

Detailed explanation

Unit tests are fast and precise: when one fails, you know exactly which function broke. They replace slow or external dependencies with test doubles, so they cannot prove the pieces fit together.

Integration tests exercise real collaborations, such as a repository class against a test database or an API handler with its middleware. They catch wiring, configuration and query mistakes that unit tests miss, at the cost of more setup and slower runs.

End-to-end tests run the deployed application through its real interface, usually a browser with Playwright, Cypress or Selenium. They give the most confidence that a user journey works, but they are the slowest, most expensive to maintain and most prone to flakiness.

Example

javascript
// unit: pure function, no dependencies
test('applies a 10% discount', () => {
  expect(applyDiscount(100, 0.1)).toBe(90);
});

// end-to-end: a real browser against the running app
test('user can check out', async ({ page }) => {
  await page.goto('/cart');
  await page.getByRole('button', { name: 'Checkout' }).click();
  await expect(page.getByText('Order confirmed')).toBeVisible();
});

How to explain it in an interview

A unit test checks one function or class in isolation and runs in milliseconds. An integration test checks that real parts work together, like my API handler and a test database. An end-to-end test drives the whole app through the browser like a user. I rely mostly on unit and integration tests and keep a small set of end-to-end tests for the critical journeys.

Key points

  • Unit: small, isolated, fast, precise failures
  • Integration: real collaborators such as a database or HTTP layer
  • End-to-end: full user journeys through the UI, slowest and most brittle

Common mistakes

  • Calling any test that uses a framework like Jest a "unit test" even when it hits a real database.
  • Trying to cover every edge case with end-to-end tests.

Follow-up questions

  • Software TestingEasy

    What is the test pyramid?

    The test pyramid is a guideline to have many fast unit tests at the base, fewer integration tests in the middle and only a small number of slow end-to-end tests at the top, so the suite stays fast and reliable.

    Junior 路 Mid-level 路 Testing
  • Software TestingMedium

    What is mocking in testing and when should you use it?

    Mocking replaces a real dependency with a controllable fake so a test can run in isolation and check how the code interacts with it; use it for slow, non-deterministic or external dependencies, not for everything.

    Junior 路 Mid-level 路 Testing
  • Software TestingMedium

    How do you deal with flaky tests?

    Treat a flaky test as a bug: quarantine it so it stops blocking the pipeline, reproduce it by running it repeatedly, fix the root cause (usually timing, shared state or external dependencies), then bring it back.

    Mid-level 路 Senior 路 Testing
  • Software TestingMedium

    Selenium vs Cypress vs Playwright: which should you choose for test automation?

    Selenium is the long-established, language-agnostic standard built on WebDriver; Cypress runs inside the browser with an excellent developer experience for JavaScript apps; Playwright offers fast, auto-waiting cross-browser automation with multiple tabs, contexts and languages. For a new web project, Playwright is usually the strongest default.

    Mid-level 路 Senior 路 Testing