Test-driven development (TDD) is a workflow in which tests guide implementation. Its central loop is Red-Green-Refactor: write a failing test, add just enough code to pass it, then improve the structure while preserving passing behavior. section 5.6
A practical TDD cycle proceeds as follows:
- Red: define expected behavior and edge cases in a test, then confirm that it fails. section 5.6
- Green: implement the smallest solution that makes the test pass. section 5.6
- Refactor: improve the implementation after the tests are green, using the tests to detect regressions. section 5.6
- Treat TDD as a tool rather than a mandatory quality gate; writing tests before code is not universally suitable when APIs, data, or external services remain unclear. section 5.0 section 5.7
Sorting an array
For an array-sorting function, begin with tests for unsorted, already sorted, one-element, and empty arrays. Implement sorting until those tests pass, then refine the code while keeping them green. section 5.6
Visualizes the Red-Green-Refactor cycle as a repeating implementation workflow. section 5.6






















