16.1 TDD Methodology and the Test Pyramid
1. The Test Pyramid
The Test Pyramid visualizes the ideal composition of software tests. The lower you go, the cheaper and faster; the higher you go, the closer to reality but slower and more costly.
/ E2E Tests \ ← Few, slow, validates real user scenarios
/ Integration Tests \ ← Moderate, validates inter-component wiring
/ Unit Tests \ ← Many, fast, validates single class/method
2. The TDD (Test-Driven Development) Cycle
TDD follows a Red → Green → Refactor cycle.
- Red: Write a failing test for functionality that doesn't exist yet.
- Green: Write the minimum code to make the test pass.
- Refactor: Improve the code (remove duplication, improve readability) while keeping all tests green.
tip
The core benefit of TDD is that it forces you to write code that's easy to test. If your code is hard to test, its design is usually flawed. Tests are design feedback.