practiceEngineering & architecture

Test-Driven Development

A development practice in which programmers write a small failing test, implement enough code to pass it, and then improve the design safely.

Download Markdown (.md)

01What it means

Test-driven development (TDD) organizes programming into short feedback cycles. The developer first expresses one expected behavior as an automated test and confirms that it fails for the intended reason. They then write the minimum production code needed to pass, before refactoring tests and implementation while keeping the suite green. The familiar rhythm is often summarized as red, green, refactor.[1][2]

Red, green, refactor Express one behavior, make it pass, then improve the design safely
  1. 01Choose behaviorOne small observable expectation
  2. 02RedWrite test and confirm intended failure
  3. 03GreenImplement the simplest reasonable pass
  4. 04Check suiteDetect regressions
  5. 05RefactorImprove structure without changing behavior

The first failure must occur for the intended missing behavior, not because the test itself is broken.

02Why it matters

TDD turns examples and requirements into executable feedback close to the moment a design decision is made. Small steps can expose misunderstandings early, encourage code with controllable dependencies, and create a regression suite that supports later change. Its main value is not maximizing test count; it is using tests to guide behavior and design.

03When to use it

Use TDD when behavior can be specified through fast, deterministic examples and the team benefits from incremental design. It is particularly useful for business rules, libraries, APIs, bug fixes, and risky refactoring. Complement it with exploratory, integration, security, performance, and usability testing where unit-level examples are insufficient.

04How it works

  1. Select one small, observable behavior and write a focused automated test for it.
  2. Run the test and confirm it fails because the behavior is absent, not because the test is broken.
  3. Implement the simplest reasonable change that makes the new test pass.
  4. Run the relevant suite to check for regressions.
  5. Refactor duplication, names, boundaries, and test structure without changing behavior.
  6. Repeat with the next behavior, keeping cycles small and feedback fast.

05A practical example

Example

A pricing service must waive delivery fees for qualifying orders. A developer writes a failing test for an order exactly at the threshold, implements the rule, and then refactors threshold handling into a named policy. Further tests cover below-threshold orders and excluded regions, revealing an ambiguous requirement before release.

06Where it breaks

Poorly chosen tests can cement implementation details, slow change, and create false confidence. TDD does not prove that requirements are correct or that components work together in production. Legacy code, asynchronous systems, graphical interfaces, and expensive infrastructure may require seams, test doubles, broader tests, or a different sequence of discovery.

Common mistakes

  • Writing many tests before implementing the next small behavior.
  • Skipping the initial failure, so a test may never have checked the intended condition.
  • Mocking internal details so heavily that harmless refactoring breaks tests.
  • Treating a passing unit suite as complete evidence of product quality.

07Sources

  1. What is Test Driven Development (TDD)?Agile Alliance · Professional practice guideWhat is Test Driven Development (TDD)?
  2. Canon TDDKent Beck · Original-author technical articleCanon TDD
View saved topics

Start typing to search titles, definitions, categories, and frameworks.