Test-Driven Development
- In Turkish
- Test Odaklı Geliştirme
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
# 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
- Unit TestTesting & Quality, p. 35A unit test is a small, automated check that verifies one function, method, or class behaves correctly in isolation from the rest of the program.
- RefactoringSoftware Architecture, p. 33Refactoring is the process of restructuring existing code to make it cleaner and easier to maintain without changing what the code does from the outside.
- Regression TestingTesting & Quality, p. 22Regression testing is the practice of re-running existing tests after a code change to make sure that features which used to work have not broken.
- Test CoverageTesting & Quality, p. 30Test coverage is a metric that measures how much of a program's source code is executed when its automated tests run, usually shown as a percentage.
- MockingTesting & Quality, p. 16Mocking is a testing technique that replaces a real dependency, such as a database or API, with a fake you control so that code can be tested in isolation.
Spotted a mistake or something missing on this page?Suggest an edit