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.
The 60-second answer
Section titled “The 60-second answer”“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.”
The loop
Section titled “The loop”- 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.
- Green. Write the simplest implementation that passes. Deliberately ugly is fine; you are buying information, not impressiveness.
- 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.
RSpec.describe Roman do it "converts 1 to I" do expect(described_class.to_roman(1)).to eq("I") endendmodule Roman def self.to_roman(_n) = "I"endThe naive version is not cheating; it is the smallest thing that passes, and the next test forces generality.
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 versus inside-out
Section titled “Outside-in versus inside-out”| 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”:
- Write a request spec: POST checkout with a coupon code, expect a discounted total. Red.
- The controller needs a
Coupon#apply_to(order)— write a unit test for it. Red. - 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.
When TDD helps
Section titled “When TDD helps”- 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.
When it hurts
Section titled “When it hurts”- 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.
Habits that make it work
Section titled “Habits that make it work”- 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.
Follow-up questions
Section titled “Follow-up questions”- How do you TDD a function whose output is random or time-dependent?
- What do you do when you cannot see how to make the test pass?
- How does TDD interact with refactoring legacy code?
- Why does the red step matter if you already know the test will fail?
- When would you deliberately not write a test first?
Common traps
Section titled “Common traps”- 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.