Skip to main content

Test-Driven Development

Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/test-driven-development

In short

Test-driven development is a coding practice in which you write a failing test first, then write just enough code to pass it, and then clean up the design.

What is test-driven development?

Test-driven development, or TDD, is a way of writing software where the test comes before the code. You describe the next small behavior you want as an automated test, watch it fail, and only then write the production code that makes it pass. The practice was popularized by Kent Beck in the early 2000s as part of Extreme Programming.

TDD follows a short loop called red, green, refactor. In the red step, you write a test that fails because the feature doesn't exist yet; in the green step, you write the simplest code that makes it pass. In the refactor step, you improve the code's structure while the tests confirm that its behavior hasn't changed, and each loop takes minutes rather than hours.

It's like marking the finish line before starting a race: you know exactly when you are done. TDD is common for business logic, libraries, and bug fixes, where you first write a failing test that reproduces the bug. It also tends to produce smaller, loosely coupled functions, because code that is hard to test is painful to write this way.

TDD is not the same as simply having tests. Writing tests after the code is still valuable, but TDD specifically uses tests to drive design decisions. It is also related to, but different from, behavior-driven development (BDD), which phrases tests as plain-language scenarios that non-developers can read.

Key takeaways

  • Write a failing test before writing the code that makes it pass.
  • The cycle is red (fail), green (pass), refactor (clean up).
  • Each cycle is small and fast, often just a few minutes.
  • TDD shapes the design and leaves a suite of regression tests behind.

Example

One red, green, refactor cyclepython
# 1. Red: write the test first. It fails because slugify doesn't exist yet.
def test_slugify_replaces_spaces_with_hyphens():
    assert slugify("Hello World") == "hello-world"

# 2. Green: write the simplest code that makes the test pass.
def slugify(text):
    return text.lower().replace(" ", "-")

# 3. Refactor: improve names or structure while the test stays green.

Readers ask

What does TDD stand for?

TDD stands for test-driven development, a practice where you write an automated test before the code it checks and then write code to make it pass.

Does TDD slow development down?

It can feel slower at first because you write tests up front. Many teams find it saves time overall by catching bugs early, making refactoring safer, and reducing time spent debugging.

What is the difference between TDD and BDD?

TDD focuses on developers writing small tests that drive the design of the code. BDD, or behavior-driven development, builds on TDD by describing behavior as plain-language given, when, then scenarios that business stakeholders can also read.

Often compared

See also

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings