Side by side
Unit TestvsIntegration Test
What is the difference between a unit test and an integration test?
Updated 2 min read7 differences
In short
A unit test checks one small piece of code, like a function, in isolation within milliseconds; an integration test checks that several parts work together.
Unit Test
A unit test is a small, automated check that verifies one function, method, or class behaves correctly in isolation from the rest of the program.
Read the page on Unit TestIntegration Test
An integration test is an automated test that checks whether several parts of a system, such as code, a database, and an API, work correctly together.
Read the page on Integration TestUnit Test and Integration Test compared
| Aspect | Unit Test | Integration Test |
|---|---|---|
| Scope | One function, method or class | Several components working together |
| Dependencies | Replaced with mocks, stubs or fakes | Real, or close to real, such as a test database |
| Speed | Milliseconds; thousands run in seconds | Slower, because of I/O, network and setup |
| A failure points to | The exact function that broke | A problem somewhere in the interaction |
| Catches | Logic errors and edge cases inside the unit | Wiring, data format, query and configuration errors |
| Setup | Minimal: call the code with inputs | Databases, services or containers must be running |
| Share of a suite | Most tests, at the base of the test pyramid | Fewer tests, in the middle of the pyramid |
The difference, explained
A unit test verifies the smallest testable piece of code, usually a function or a class, with its dependencies replaced by fakes or mocks. An integration test verifies that several components work together, such as an API route with its real database, or two services talking over the network.
The difference is scope, and scope drives the trade-offs. Unit tests are fast, precise and stable, so a failure points straight at the broken function, but they cannot catch problems in the connections between parts. Integration tests catch wrong queries, mismatched data formats and configuration mistakes, but they are slower, need more setup and can be harder to debug.
A healthy test suite uses both, often pictured as a test pyramid: many unit tests at the base, fewer integration tests in the middle and a small number of end-to-end tests at the top. Unit tests give quick feedback while you code, and integration tests confirm the pieces actually fit together.
A common misconception is that high unit test coverage means the system works. Every unit can pass its tests while the whole still fails, for example when one function returns dates in a format the next component does not expect.
Which one should you use?
Choose Unit Test when…
- You are testing pure logic, calculations or edge cases.
- You want feedback within seconds while you write code.
- You need to pinpoint exactly which function broke.
Choose Integration Test when…
- You need to confirm your code talks correctly to a database or API.
- Bugs tend to appear where modules connect.
- You are verifying configuration, queries or serialization.
Testing a pricing function and a users route
// Unit test: one function, no outside dependencies
import { applyDiscount } from "./pricing";
test("applies a 10% discount", () => {
expect(applyDiscount(100, 0.1)).toBe(90);
});// Integration test: HTTP route + a real test database
import { db } from "./db";
test("POST /users stores the user", async () => {
const res = await fetch("http://localhost:3000/users", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Ada" }),
});
const { id } = await res.json();
expect((await db.users.findById(id)).name).toBe("Ada");
});Readers ask
How many unit tests vs integration tests should I write?
A common guideline is the test pyramid: mostly unit tests, fewer integration tests and only a handful of end-to-end tests. The right mix depends on where your bugs usually come from.
Is an integration test the same as an end-to-end test?
No. An integration test checks a few components together, while an end-to-end test drives the whole application the way a user would, often through a real browser.
Should integration tests use mocks?
Mostly not for the parts being integrated, since checking the real interaction is the point. It is still common to mock external services you don't control, such as a payment provider.