What are unit, integration and end-to-end tests in a backend service?
Model answer
The three levels differ in how much real code and infrastructure they run.
A unit test checks one class or function in isolation. Collaborators such as the database or an HTTP client are replaced with test doubles, so the test runs in milliseconds and needs no network.
An integration test runs several parts together with real infrastructure: your repository against a real PostgreSQL, or a controller through the real HTTP stack. It catches wiring, SQL and serialization bugs that unit tests cannot see.
An end-to-end test drives the whole deployed system the way a client would, for example by calling the public API and checking the result across several services. It gives the most confidence but is slow and the most likely to be flaky.
The usual shape is many unit tests, a fair number of integration tests and few end-to-end tests, because cost and flakiness grow as you go up.
Pick the cheapest level that can still catch the bug you worry about.
Key points
- Unit tests isolate one unit with test doubles
- Integration tests use real infrastructure such as a database
- End-to-end tests run the whole deployed system
- Cost and flakiness rise with each level
Likely follow-ups
- Is a controller test with a mocked service a unit or an integration test?
Common mistakes
- Calling any test that uses a framework an integration test
- Testing every rule through the end-to-end layer