Tech Terms Explained Open in the app →

Test-Driven Development

Testing · Intermediate · 5 min read

What is it?

Test-driven development is a practice where you write a failing test first, then write just enough code to pass it, then clean up — repeating in small cycles.

Explain like I'm 5

TDD is like writing the answer key before solving the problem: you decide what 'correct' looks like first, then write code until it matches.

Why was it created?

Writing tests after the fact often gets skipped and lets bugs slip in. TDD was promoted to drive design and ensure tests actually exist.

Where is it used?

  • Building features with confidence
  • Designing clean interfaces
  • Preventing regressions
  • Refactoring safely

Why should developers care?

TDD shapes how many teams build software and is a common interview and code-review topic, so understanding it is valuable.

How does it work?

You follow a short cycle: Red (write a failing test for the next bit of behavior), Green (write the simplest code to pass it), Refactor (improve the code while tests stay green). Repeat for each small piece of behavior.

Real-world example

To add a discount feature, you first write a test expecting a 10% discount, watch it fail, implement the logic to pass, then tidy the code.

Common use cases

  • Driving design through tests
  • Ensuring coverage exists
  • Safe refactoring
  • Reducing regressions

Advantages

  • Tests exist by construction
  • Encourages simple, testable design
  • Fast feedback
  • Safer refactoring

Disadvantages

  • Slower upfront
  • Learning curve
  • Poorly written tests can mislead
  • Not ideal for exploratory spikes

When should you use it?

When building behavior you can specify clearly and want well-tested, maintainable code.

When should you avoid it?

For throwaway prototypes or exploratory work where the design is still unknown.

Alternatives

Writing tests after the codeBehavior-driven development (BDD)Manual testing

Related terms

Unit TestingIntegration TestingRefactoringCI/CD

Interview questions

Beginner

  • What is TDD?
  • What are the three steps of the cycle?

Intermediate

  • Why write the test before the code?
  • What does 'refactor' mean in TDD?

Senior

  • How do you avoid brittle tests in TDD?
  • When is TDD a poor fit?

Common misconceptions

  • "TDD means writing all tests first" — you write one small failing test at a time, not the whole suite upfront.
  • "TDD guarantees bug-free code" — it improves coverage and design but doesn't catch everything.

Fun facts

  • The cycle is often summarized as 'Red, Green, Refactor'.
  • TDD is as much a design technique as a testing one.

Timeline

  • 2000s — TDD popularized by the Extreme Programming movement

Learning resources

Quick summary

TDD is a write-test-first practice with a Red-Green-Refactor cycle that drives clean design and ensures code is tested as it's built.

Cheat sheet

  • Test first, then code
  • Red → Green → Refactor
  • One small behavior at a time
  • Drives design, not just testing

If you remember only one thing

TDD writes a failing test first, then code to pass it — building tested, well-designed code in small cycles.