TDD (Test-Driven Development) is a software development practice in which you write an automated test before the code it checks. The cycle is called red, green, refactor: write a test that fails, write the simplest code that makes it pass, then clean up the code while keeping the tests passing.
Why it matters
Writing tests first forces you to define the expected behavior before implementing it, which tends to produce smaller functions and clearer interfaces. The growing test suite becomes a safety net that lets you refactor with confidence. TDD takes discipline and can feel slow at first, and it fits some work (logic, libraries, APIs) better than others (exploratory prototypes, visual layout). Evidence on its benefits is mixed, and many teams use it selectively.
Example
# 1. Red: test written first, fails because slugify doesn't exist
def test_slugify():
assert slugify("Hello World") == "hello-world"
# 2. Green: simplest code that passes
def slugify(s):
return s.lower().replace(" ", "-")
# 3. Refactor, then add a test for punctuation
Common mistakes
- Writing many tests at once instead of one small step at a time.
- Testing implementation details so tests break on every refactor.
- Skipping the refactor step.
Related
Get notified when this term updates
Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.
This page is general information written by our editorial team and updated when documentation changes. Servers and providers differ, so test changes safely first. See our editorial policy and disclaimer.