Integration Tests vs Unit Tests
You'll learn: How integration tests differ from unit tests in scope, dependencies, and the types of problems they catch.
The Core Distinction
Unit tests verify individual components in isolation—a single function, class, or small module. They use test doubles (mocks, stubs, fakes) to cut off external dependencies. Think of them as testing a single gear to ensure its teeth are properly shaped.
Integration tests verify that multiple components work together correctly across real boundaries. They use actual dependencies—real databases, file systems, or network calls. This is like testing whether multiple gears mesh smoothly when assembled.
What Each Test Type Reveals
When a unit test fails, it pinpoints exactly which component is broken. The narrow scope makes debugging straightforward: "This function doesn't handle negative numbers correctly."
When an integration test fails, it tells you components aren't cooperating properly. The problem might be: misconfigured connections, incompatible data formats, incorrect assumptions about timing, or violated contracts between modules. Integration tests answer: "Do these pieces actually fit together?"
Scope and Dependencies
Unit tests:
- Test one component at a time
- Replace dependencies with test doubles
- Run fast (milliseconds)
- Failures point to specific code
Integration tests:
- Test multiple components together
- Use real implementations across boundaries
- Run slower (seconds or more)
- Failures indicate interaction problems
An Analogy
Unit tests are like spell-checking individual words. Integration tests are like reading sentences to see if they make sense. Both "cat," "the," and "sat" might spell correctly, but "cat the sat" reveals an integration problem.
Key Takeaway: Unit tests verify components work in isolation; integration tests verify they work together. Unit test failures tell you what is broken; integration test failures tell you how things break when combined.