Unit Test vs Integration Test — What's the Difference?

A unit test verifies a single function or component in isolation, typically with dependencies mocked out; an integration test verifies that multiple components work correctly together, often including real dependencies like a database.

Published October 3, 2026

A unit test checks one small piece of code in isolation, such as a function; an integration test checks that several parts (code, database, API) work together. Unit tests are fast and numerous, integration tests are slower and fewer.

The concept

Unit tests replace collaborators with fakes or mocks so a failure points to exactly one place. They run in milliseconds.

Integration tests use real components, such as a test database or HTTP server, to catch wiring, configuration and contract problems that unit tests cannot.

Test pyramid

/\ few end-to-end
/ \ some integration
/____\ many unit tests

Practical examples

Unit test

test('adds tax', () => {
  expect(withTax(100, 0.2)).toBe(120)
})

Integration test with a real DB

test('creates a user', async () => {
  const res = await request(app).post('/users').send({ name: 'Ada' })
  expect(res.status).toBe(201)
  expect(await db.users.count()).toBe(1)
})

Mocking a dependency

const mail = { send: jest.fn() }
await signup(user, mail)
expect(mail.send).toHaveBeenCalled()

Common mistakes

  • Mocking so much that tests prove nothing.
  • Only writing integration tests and having slow suites.
  • Sharing database state between tests.

Key takeaways

  • Unit = one piece, fast; integration = pieces together.
  • Aim for many unit, fewer integration tests.
  • Isolate test data.

FAQ

What about end-to-end tests?

They drive the whole app like a user, and are the slowest and most brittle.

How much coverage is enough?

Focus on critical paths; a percentage alone is a weak measure.

More General articles