Skip to content

TDD

Test-driven development is a design discipline disguised as a testing practice. The test comes first because writing it first forces you to decide what the code’s interface should be before you have committed to an implementation.

“TDD is a short loop: write a failing test for the next small behaviour, make it pass with the simplest code that works, then refactor with the test as a safety net. The value is not the tests — it is the feedback: the red step proves the test can fail, the green step bounds the scope, and the refactor step is where the design improves. It works best when the behaviour is well understood and the interface is not, and worst when you are exploring an unknown API, prototyping UI, or spiking. Outside-in starts from a failing acceptance test and drives inward; inside-out builds the domain first and composes upward.”

  1. Red. Write the smallest test that expresses the next requirement and watch it fail for the expected reason. If it passes immediately, it is not testing new behaviour.
  2. Green. Write the simplest implementation that passes. Deliberately ugly is fine; you are buying information, not impressiveness.
  3. Refactor. Improve names, remove duplication and generalise — with the test green the whole time. Refactor both code and test.

Then repeat, in steps of a few minutes. Long red phases mean the step was too big.

spec/lib/roman_spec.rb
RSpec.describe Roman do
it "converts 1 to I" do
expect(described_class.to_roman(1)).to eq("I")
end
end
lib/roman.rb — step 1 (green, deliberately naive)
module Roman
def self.to_roman(_n) = "I"
end

The naive version is not cheating; it is the smallest thing that passes, and the next test forces generality.

spec/lib/roman_spec.rb — more examples in the same describe
it("converts 2 to II") { expect(described_class.to_roman(2)).to eq("II") }
it("converts 4 to IV") { expect(described_class.to_roman(4)).to eq("IV") }
it("converts 1994 to MCMXCIV") { expect(described_class.to_roman(1994)).to eq("MCMXCIV") }

The 1994 example pushes you to a table-driven implementation. This is the “triangulate” habit: two or three examples before generalising, so the abstraction is forced by data rather than guessed.

Outside-in (London) Inside-out (Chicago / classicist)
Starts at Acceptance/integration test of the feature Domain unit
Uses doubles Heavily, to discover collaborators Sparingly, prefer real objects
Design emerges from Collaborator interfaces Data and behaviour of the domain
Strength End-to-end behaviour is specified early Refactor-friendly, fewer mocks
Weakness Can produce mock-heavy, brittle tests Integration gaps discovered late

Outside-in sequence for “apply a coupon at checkout”:

  1. Write a request spec: POST checkout with a coupon code, expect a discounted total. Red.
  2. The controller needs a Coupon#apply_to(order) — write a unit test for it. Red.
  3. Implement, green, refactor. The request spec now passes.

Inside-out sequence for the same feature: implement and unit-test Coupon, then Order#apply, then wire the controller, then a single request spec at the end.

Both are legitimate. Teams usually mix: outside-in for the flow, inside-out for algorithms.

  • Well-understood behaviour with an unclear interface. The test is a design tool.
  • Bug fixes. Reproduce the bug as a failing test, then fix it. This is non-negotiable and is the strongest form of TDD.
  • Algorithms and parsers. Example-driven development exposes edge cases early.
  • Code with expensive failure — payments, permissions, data migrations.
  • Legacy code you are about to change. Tests are how you make the change safe.
  • Spikes and prototypes. You do not yet know what you are building; tests lock in guesses.
  • Exploratory UI work. Visual iteration is faster in the browser; add tests once the shape settles.
  • Trivial code. Testing a one-line delegation adds noise.
  • When you do not know the domain. Write a throwaway spike, delete it, then TDD the real thing.
  • When the test is harder than the code. That is a design smell, not a reason to stop — but it is a reason to change the design.
  • One behaviour per test; the name should read as a sentence.
  • Keep the loop under a few minutes; if it is longer, split the behaviour.
  • Run the single test file, not the whole suite, while in the loop.
  • Refactor aggressively in the third step; that is where TDD pays for itself.
  • Do not delete tests that were hard to write — they are usually covering the risky part.
  • Commit at green, so a bisect lands on a working state.
  1. How do you TDD a function whose output is random or time-dependent?
  2. What do you do when you cannot see how to make the test pass?
  3. How does TDD interact with refactoring legacy code?
  4. Why does the red step matter if you already know the test will fail?
  5. When would you deliberately not write a test first?
  • Skipping red. You lose the proof that the test is meaningful.
  • Writing a test for a private method. You coupled the test to the implementation.
  • Test-first, design-never. TDD without the refactor step produces a pile of passing code.
  • Asserting on mocks instead of outcomes. The test passes even when behaviour is wrong.
  • Big-bang TDD. Writing twenty failing tests then implementing for an hour; the feedback loop is gone.
  • Believing TDD replaces design. It surfaces design problems; it does not solve them for you.