principleMajorpending
Testing Diamond: Unit, Integration, and E2E Test Balance
Viewed 0 times
testing strategyintegration teststesting trophytesting diamondE2Eunit tests
Problem
The testing pyramid says write mostly unit tests, but modern apps with microservices and APIs get more value from integration tests. Teams argue about the right balance.
Solution
The testing trophy/diamond model for modern apps:
Guidelines:
- No mocking needed = good unit test candidate
- Math, parsing, validation rules, state machines
- HTTP handlers with real DB (use test containers)
- Service layer with real dependencies
- React components with real state management
- Sign up -> login -> core action -> verify
- Payment flow, onboarding, key workflows
- Max 5-15 E2E tests for most apps
- TypeScript, ESLint, mypy catch entire categories of bugs
- Cheaper than any test to maintain
/\ E2E tests (few, critical paths)
/ \ - Login flow, checkout, signup
/ \ - 5-10 tests covering happy paths
/------\
/ \ Integration tests (most value)
/ \ - API endpoints with real DB
\ / - Component + service interactions
\--------/ - Module boundaries
\ /
\ / Unit tests (pure logic)
\ / - Algorithms, calculations
\/ - State machines, validatorsGuidelines:
- Unit tests for pure functions with complex logic
- No mocking needed = good unit test candidate
- Math, parsing, validation rules, state machines
- Integration tests for behavior at boundaries
- HTTP handlers with real DB (use test containers)
- Service layer with real dependencies
- React components with real state management
- E2E tests for critical user journeys only
- Sign up -> login -> core action -> verify
- Payment flow, onboarding, key workflows
- Max 5-15 E2E tests for most apps
- Static analysis as the base
- TypeScript, ESLint, mypy catch entire categories of bugs
- Cheaper than any test to maintain
Why
Over-investing in unit tests that mock everything gives false confidence. Integration tests catch the bugs users actually hit: misconfigured routes, wrong SQL, broken serialization.
Gotchas
- Integration tests are slower but catch more real bugs per test
- Mocking too much turns tests into 'testing the mocks' - prefer fakes or test containers
Context
Deciding test strategy for a project
Revisions (0)
No revisions yet.