Skip to content

What Is Test-Driven Development (TDD)? Definition and Examples

Quick answer

Test-driven development explained: the red, green, refactor loop, a three-pass leap-year example in Python, Indian use cases and common mistakes.

What Is Test-Driven Development (TDD)? Definition and Examples

Test-driven development, or TDD, is a way of building software in which you write a failing test first, then write just enough code to make it pass, then tidy the code while the test stays green. The tests describe what the code should do before the code exists, so they guide the design.

Key takeaways

  • TDD means writing tests before the code they check.
  • The loop is red (a failing test), green (just enough code to pass) and refactor (clean up).
  • Each pass is small, so a failure points to the last change you made.
  • TDD is different from simply writing tests after the code is done.

On this page:

ApproachWhen tests are writtenWhat drives the design
Test-driven developmentBefore each piece of codeThe tests
Test-after developmentAfter the code worksThe developer's plan
Manual testing onlyRun by hand, not savedWhatever is checked that day
Behaviour-driven developmentBefore code, in plain-language scenariosDescribed behaviour

What does test-driven development mean?

It means letting tests lead. Martin Fowler (page dated 11 December 2023) describes TDD as a technique for building software that guides software development by writing tests. IBM calls it an approach in which software tests are written before their corresponding functions.

In plain English: write the check for what the code should do before writing the code, then tidy up while the check still passes.

How does test-driven development work?

It works in a short loop with three steps. Fowler lists them as: write a test for the next bit of functionality you want to add; write the functional code until the test passes; refactor both new and old code to make it well structured. IBM names the same loop red, green and refactor. Red is to write a failing test for the intended behaviour, green is to write enough extra to pass the test, and refactor is to refine the code to meet simplicity standards as much as possible while still passing the test.

Here is an illustrative leap-year function in three passes. Red. Save this as test_leap.py with no leap.py yet:

from leap import is_leap_year

def test_leap_years():
    assert is_leap_year(2024) is True
    assert is_leap_year(2023) is False
    assert is_leap_year(1900) is False
    assert is_leap_year(2000) is True

Run pytest. It fails because there is nothing to import. That failure is the red step.

Green. Create leap.py with the simplest code that passes:

def is_leap_year(year):
    return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)

Run pytest again and the test passes. Refactor. Rename variables or extract a helper if it helps, then re-run the test. If it is still green, the behaviour is unchanged.

Where is test-driven development used?

It is used for business rules that are easy to state and easy to get wrong: discounts, tax, limits and validations. An illustrative shopping cart: the cart total is Rs 1,000 and a 10 percent discount applies. You write a test that expects Rs 900 before the discount code exists, and the test fails until you add it.

An Indian example: a UPI-style payment limit check. If the rule is that a single payment cannot exceed a set amount, you first write tests for an amount below the limit, one exactly at the limit and one above it. Then you write the check. The rule is written down before the code, so nobody has to guess it later.

What are the common mistakes and confusions?

  • Writing too much test at once. Keep each step small so a failure has one obvious cause.
  • Skipping the red step. A test that has never failed may not be testing anything.
  • Skipping refactor. Green code that is messy becomes costly to change.
  • Confusing it with writing tests later. The order is the point: the test comes first.

IBM's timeline notes that Kent Beck's book Test Driven Development: By Example, published in 2003, popularised the practice.

TDD sits within testing generally. See the QA software testing roadmap, the automation testing syllabus and API development and testing tools. For testing AI features, see how to test an AI tool you built. Testing ideas for data work appear in A/B testing explained for analysts.

Next step

Try the leap-year example, then pick a rule from your own project, such as a fee or a discount, and write three tests before any code. For where this fits in a career, read the backend developer roadmap and the system design roadmap for beginners.

Frequently asked questions

What is test-driven development?

Test-driven development is a technique in which tests are written before the code they check. You write a failing test, write code until it passes, then refactor while keeping the test green.

What is red, green, refactor?

Red is writing a failing test for the intended behaviour. Green is writing enough code to pass it. Refactor is cleaning up the code to be simple and well structured while the tests still pass.

Who popularised TDD?

According to IBM's timeline, Kent Beck popularised the practice in 2003 by publishing Test Driven Development: By Example.

Is TDD the same as unit testing?

No. Unit tests are a kind of test. TDD is a working order: you write the test first, then the code. You can write unit tests without doing TDD.

Why write the test before the code?

Writing the test first makes you state what the code should do before you build it, and gives you a quick check after every small change. It also guides the design of the code.

Share LinkedIn X WhatsApp
×

Our Courses

Practice-Based Learning Tracks, Supercharged By A.I.