Testing Strategy
Unit, integration, contract, E2E, performance, chaos; test pyramid vs trophy
A testing strategy defines the mix of test types used to provide confidence in software correctness while optimizing for speed and cost. Mike Cohn's Test Pyramid prescribes a large base of fast unit tests, fewer integration tests, and a small apex of slow E2E tests — inverting the pyramid creates a brittle, slow test suite known as an "ice cream cone." Modern strategies extend the pyramid with contract tests (Pact), chaos engineering (Chaos Monkey), and performance tests to address distributed system concerns. Google reports that test suites following the pyramid ship features 2–4x faster than suites dominated by E2E tests.
Key Points
- Unit tests: test a single class or function in isolation with mocked dependencies — fast (<10ms each), numerous (thousands), run on every commit.
- Integration tests: test interaction between two or more real components (e.g., service + database) — verify contracts hold in practice.
- Contract tests (Pact): verify that consumer expectations match provider capabilities without deploying both services — critical in microservices.
- E2E tests: simulate full user journeys through a deployed system — Playwright and Cypress are industry standard; keep suite <200 tests.
- Performance tests: load (expected traffic), stress (above capacity), soak (sustained load), and spike (sudden bursts) — run in pre-production environments.
- Chaos engineering: deliberately inject failures (kill pods, throttle network) to verify resilience — Netflix's Chaos Monkey is the canonical tool.
- Test doubles: mock (verifies calls), stub (returns canned responses), spy (wraps real object and records calls), fake (working lightweight implementation).
- Coverage gates: enforce minimum branch coverage (e.g., 80%) in CI — coverage is a floor, not a goal; 100% coverage with poor assertions is worse than 70% with good ones.
Test Pyramid: maximize fast unit tests, moderate integration tests, minimize slow E2E tests
Real-World Example
Spotify's "Testing Honeycomb" variant emphasizes integration tests over unit tests for microservices — they found that unit tests alone gave false confidence when service boundaries were the actual failure points. Adjust the pyramid shape to your architecture.