Test-Driven Development (TDD) is a software development practice where tests are written before the code that makes them pass, popularized by Kent Beck in the late 1990s as part of Extreme Programming. TDD follows the red-green-refactor cycle: write a failing test (red), write minimal code to make it pass (green), then improve the code structure (refactor). This approach transforms testing from an afterthought into a design tool that drives cleaner, more maintainable code. The practice emphasizes small, incremental steps and continuous validation, providing instant feedback and catching issues at the earliest possible moment β a quality proven especially vital in AI-assisted development workflows where tests act as specifications that keep generated code on track.
What This Cheat Sheet Covers
This topic spans 16 focused tables and 95 indexed concepts. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
Table 1: Core TDD Cycle
| Phase | Example | Description |
|---|---|---|
test('sum adds numbers', () => { expect(sum(2, 3)).toBe(5);}); | β’ Write a test for functionality that doesn't exist yet β’ test must fail to confirm it's testing the right thing. | |
function sum(a, b) { return a + b;} | β’ Write the simplest code that makes the test pass β’ no gold-plating, no extra features β just enough to turn red to green. | |
// Clean up duplicates// Extract methods// Improve names | β’ Improve code structure while keeping tests green β’ eliminate duplication, clarify intent, apply design patterns β all with test safety net. |