Skip to content
Careersintermediate

What is test-driven development?

Updated Nov 30, 20263 min readBased on official documentation
What is test-driven development?

In short

Test-driven development (TDD) is a way of building software by writing a small test before writing the code it checks. You repeat three steps: write a failing test, write code until it passes, then clean up the code. The method was developed by Kent Beck in the late 1990s.

How it works

Test-driven development, usually called TDD, flips the usual order of work. Instead of writing code first and testing it later, you write a test first. A test is a small piece of code that checks whether another piece of code behaves the way you expect.

The process follows three steps that repeat again and again. They are often called Red, Green, Refactor. First, you write a test for the next small feature you want. The test fails, which is the red step, because the feature does not exist yet. Second, you write just enough code to make the test pass, the green step. Third, you tidy up both the new code and the old code so it stays clean, the refactor step.

Before the cycle starts, there is a planning step. You write a list of the test cases you want to cover. You then pick one test from the list, run it through the cycle, and move on to the next. Choosing the order of tests matters. Good tests lead you quickly to the most important parts of the design. While you work, new test ideas will come up, so you add them to the list.

Why write the test first

Writing the test first has two main benefits. The first is that every piece of functional code exists because a test asked for it. This means your project ends up with tests covering its behavior, sometimes called self-testing code.

The second benefit is about design. When you write a test first, you have to decide how the code will be used before you write it. You think about the interface, meaning the way other parts of the program call your code. This habit helps you keep the interface separate from the internal details, which is a key part of good design and something many programmers find hard at first.

When to use it

TDD works well when you are building new logic with clear inputs and outputs, such as calculations, parsers, or business rules. It also helps when you are fixing a bug. You write a test that reproduces the bug, watch it fail, and then fix the code until the test passes. The test then guards against the bug coming back.

TDD is less of a natural fit when you are exploring an unfamiliar technology and do not yet know what the code should look like. In that case, a short spike, which is a quick experiment written to learn something, can come before the tests.

A simple example

Suppose you want a function that adds two numbers. Your first test might look like this in pseudocode:

test "add returns the sum of two numbers"
  result = add(2, 3)
  expect result to equal 5

Run the test. It fails because add does not exist yet, which is the red step. Next, write the simplest add that makes the test pass. Return the sum of the two inputs. That is the green step. Finally, look at the code and the test together. Rename anything unclear and remove duplication. That is the refactor step. Then pick the next test from your list, such as adding negative numbers, and repeat.

Common mistakes

The most common mistake is skipping the refactor step. If you only write tests and code to make them pass, the code slowly becomes a messy collection of fragments. Cleaning up as you go keeps the project easy to change. Even so, a messy codebase with tests is easier to repair than one without tests.

Another mistake is writing too many tests at once. Work on one test at a time so each cycle stays small and easy to follow. Also avoid writing more code than the current test needs. The goal of the green step is to pass the test, not to build the whole feature in one go.

TDD is not the only way to test software. Some teams write code first and add tests afterward, which is simpler to start with but often leaves gaps. Others use behavior-driven development, which writes tests in plain language that describes what a user expects. Many teams combine these approaches, using TDD for core logic and other methods for areas where tests are harder to write early.

TDD grew out of Extreme Programming, a set of software practices developed in the late 1990s. You can read more about the original method in the source article: https://martinfowler.com/bliki/TestDrivenDevelopment.html

FAQ

Do I need a testing framework to practice TDD?

Not strictly, but most teams use one because it runs tests automatically and reports failures clearly. The method itself only depends on the order of writing tests and code.

Is TDD the same as writing unit tests?

Not exactly. TDD is a design process where tests come first and guide the code. Unit tests are one kind of test you might write during that process.

How long should one red-green-refactor cycle take?

Keep each cycle short, often a few minutes. Small steps make it easier to see what caused a failure and to stay focused.

Does TDD slow down development?

It can feel slower at first because you write more code overall. Many teams find it saves time later by catching mistakes early and making changes safer.

Sources

This page is based on the official documentation below. Read it for full detail.

Test-driven development — official documentation ↗
blozhikDeveloper news from across the web, explained. Every story links to its original sources.