Unit vs integration vs E2E tests?
Practice this questionWANT TO UNDERSTAND THE CONCEPT FIRST?Learn Unit vs integration vs E2E tests?
Definition:
Unit tests check a small piece of logic, integration tests verify collaborating components, and E2E tests exercise a complete user or API flow. They catch different classes of failure and have different setup and execution costs. A balanced strategy puts detailed business-rule checks near the unit level and preserves real integration coverage where mocks would hide bugs.
Key points:
-
Unit: one function or small module in isolation.
-
Integration: real components together, such as a service and database.
-
E2E: a full user flow; keep a focused set for critical behavior.
-
Use real database tests for constraints and rollback behavior; a mocked repository cannot prove these work.
Example — a focused unit test:
function canReserve(stock, quantity) {
return Number.isInteger(quantity) && quantity > 0 && stock >= quantity;
}
test('rejects a negative reservation quantity', () => {
expect(canReserve(5, -2)).toBe(false);
});- This checks validation only. A separate real-database integration test is needed to prove concurrent reservations cannot oversell.
Interview point:
- Explain what a test deliberately includes and excludes, and why that boundary is useful.
Adapted from 09_TESTING_GIT_DOCKER_CLOUD.md · “Unit vs integration vs E2E tests?”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.