Notes on test-driven development. Strong opinions are flagged as opinions.

TDD is a way of writing code, not a way of testing it. That distinction clears up most arguments. The loop has three steps, and the order is the point.

  1. Red. Write one small test for behaviour that does not exist yet. Run it. It must fail. If it passes, either the behaviour already exists or the test is wrong.
  2. Green. Write the least code that makes it pass. Not the elegant code, not the general code. The least.
  3. Refactor. With the tests green, clean up. Rename, remove duplication, restructure. Run the tests after each step. They tell you if you broke something.

Each lap is a few minutes. Kent Beck popularised the practice through the Extreme Programming community in the late 1990s, and his book on it is still the standard reference.

A tiny lap, in words

Say we need a function that computes GST on an invoice line. First test: a line of 1,000 rupees at 18 percent should give 180. It fails because the function does not exist. Red. We write code that returns the right answer, perhaps even by multiplying and dividing sensibly. Green. Next test: a zero-rated item gives 0. Red, because the first version ignores the rate. We adjust. Next: rounding of 99.99 at 5 percent to two decimals. Another red, another small change. Only after several laps do we look at the whole function and tidy it.

Notice what the tests did. They forced a decision about the function’s signature before it existed. They defined what rounding means. They were written from the caller’s point of view. That is why people call TDD a design technique: the test is the first user of your code, and awkward code is awkward to call.

What it is genuinely good at

Rule-heavy logic: tax, pricing, eligibility, parsing, anything with lots of cases and clear answers. Bug fixes, where the discipline is to write a failing test that reproduces the bug first, then fix it. That one habit alone, in my opinion, is worth adopting even if you never do TDD otherwise, because it proves the bug existed and stops it returning.

It also pushes code towards being small and loosely coupled, since tightly entangled code is painful to test first. Testability stops being an afterthought.

Where it is oversold

Here the opinions begin. TDD does not guarantee good tests, good design or correct requirements. If you misunderstand the requirement, you will faithfully write passing tests for the wrong thing. It does not replace testing by someone who did not write the code, because the author’s blind spots end up in both the tests and the implementation.

It is also awkward for exploratory work. When you do not yet know what you are building, such as a first spike with an unfamiliar library or a visual layout, writing tests first can be wasteful. Spike, learn, throw it away, then build properly with tests. Many serious practitioners do exactly this and still call it TDD.

And it is not a religion. Studies of TDD’s effect on quality and productivity have given mixed results, in part because the practice is hard to isolate from team skill. Anyone who tells you the evidence is overwhelming is overselling. I would defend it on the narrower ground that many developers find it keeps them focused and their code testable.

Mistakes I see in the wild

  • Writing all the tests first for a whole feature. The loop is one test at a time; a pile of failing tests gives no feedback.
  • Skipping the red step. A test never seen failing might never fail.
  • Skipping the refactor step, and ending with passing but ugly code.
  • Testing implementation details with heavy mocking, so any refactor breaks tests even though behaviour is unchanged. Tests should describe what, not how.
  • Chasing 100 percent coverage as the goal. Coverage is a by-product here, not the aim.

How it relates to BDD and ATDD

TDD works at the level of small units, written by developers. Behaviour-driven development lifts the same idea to the conversation between business, developers and testers, using examples in plain language. Acceptance-test-driven development writes the acceptance tests first at the feature level. They stack: an outer failing acceptance test says the feature is not done, while inner TDD laps build the pieces to make it pass. If you are a tester, you may never write a unit test lap, but you can read them. They are executable documentation of what the developer believed the code should do, and where those beliefs differ from yours there is usually a bug waiting.

Leave a Reply

Trending