Test-Driven Development
What if you wrote the test *before* the code? It sounds backwards, but writing the test first โ red, green, refactor โ is a powerful way to build correct code with a clear target.
Here's an idea that sounds backwards but is surprisingly powerful: write the test first, before the code it tests. This is test-driven development (TDD), and its rhythm - red, green, refactor - turns testing from an afterthought into a way of designing and building code with a clear target. You don't have to use TDD for everything, but understanding it will make you a sharper developer.
Red, green, refactor
TDD is a tight loop: write a failing test (red), write just enough code to pass it (green), then clean up (refactor) - and repeat.
| Step | What you do |
|---|---|
| 1. Red | Write a test for behaviour that doesn't exist yet - it fails |
| 2. Green | Write the simplest code that makes the test pass |
| 3. Refactor | Clean up the code, tests still passing |
| Repeat | One small behaviour at a time |
You build the code one small, tested behaviour at a time, always with a passing test suite. Because the test comes first, you never write code without knowing exactly what it should do - the test IS the specification. And you can't forget to test, because the test led the way.
Why writing the test first helps
Writing the test first sounds odd, but it has real advantages - it forces clarity and guarantees testability.
Test-first thinking
- TDD writes the test first, then the code to pass it - red, green, refactor.
- Red: a failing test for behaviour that doesn't exist yet.
- Green: the simplest code that passes; then refactor to clean up.
- The test is the spec - you clarify 'done' before writing code, and it's always testable.
๐ฏ Key takeaways
- Test-driven development (TDD) writes the test before the code - the cycle is red, green, refactor.
- Red: a failing test for behaviour that doesn't exist. Green: the simplest code to pass it. Refactor: clean up.
- Writing the test first forces you to define exactly what 'done' means before implementing.
- Even without strict TDD, asking 'how would I test this?' before coding makes you build better, testable code.
Frequently asked
Writing a test before the code exists sounds impossible - how does that even work?
It feels impossible at first, but it works precisely because a test describes behaviour, not implementation - and you can describe what you want code to do before you've written how it does it. Here's the concrete flow: suppose you need a function to calculate a discount. In TDD, you first write a test asserting what that function should do - say, 'assert calculate_discount(100, 0.2) == 80' - even though calculate_discount doesn't exist yet. You run it, and it fails (red), often because the function isn't defined - that's expected and fine; the failing test is your target. Then you write the simplest code that makes that test pass (green) - define calculate_discount to return the price minus price times the rate. Run the test again; now it passes. Then you clean up if needed (refactor), keeping the test green, and move to the next behaviour with another test. So you're not writing a test for code that exists; you're writing a test that specifies code you're about to write, using the test to define exactly what you want before you build it. The test failing at first isn't a problem - it's the whole point, confirming you have a real target and that your test can actually detect the absence of the behaviour. What makes this possible is that you're testing behaviour (given this input, expect this output) from the outside, which you can specify without knowing the internal implementation. It also naturally keeps you building one small, well-defined piece at a time, since each cycle adds one behaviour. The mental shift is from 'write code, then check it' to 'decide what the code should do (as a test), then write code to fulfil that' - and once you've done a couple of red-green-refactor cycles, the 'test first' idea clicks and stops feeling backwards. It's a disciplined way of turning 'what do I want this to do?' into an executable target before you implement, which is why many developers find it produces clearer, better-tested code.
Do I have to use TDD for everything, or is it optional?
TDD is a valuable technique to know and use where it fits, but it's absolutely not a rule you must apply to everything - plenty of excellent developers use it selectively or rarely, and you should treat it as one tool among several rather than a mandate. It shines in certain situations: when you're building something with clear, well-defined behaviour (like a calculation, a data transformation, or a function with specific rules), when you're fixing a bug (write a test that reproduces the bug first, then fix it so the test passes - this both confirms the fix and prevents the bug returning), and when you want to think carefully about a piece of code's interface and behaviour before diving into implementation. In those cases, writing the test first genuinely helps by forcing clarity and guaranteeing coverage. But TDD is a poorer fit for other situations: exploratory or experimental code where you're figuring out what you even want (it's hard to write a test first when you don't yet know the behaviour), rapidly changing prototypes, or code where the 'behaviour' is inherently visual or hard to assert (like fine-tuning how a page looks). For those, writing code first and adding tests after (or testing lightly) is perfectly reasonable. The healthiest stance is pragmatic: understand TDD, use it when it helps (well-defined logic, bug fixes, when you want design clarity), and don't force it where it doesn't fit. Even if you rarely do strict TDD, absorbing its core insight - thinking about how you'd test something, and what 'done' means, before or while you write it - makes you a better developer, because it pushes you toward code that's testable and clearly specified. Many developers land on a middle ground: mostly writing tests alongside or shortly after their code rather than strictly first, while using true test-first TDD for the cases where it clearly pays off, like reproducing bugs or nailing down tricky logic. So: learn it, appreciate its benefits, apply it where it fits, and don't feel obligated to use it universally. What matters is that your important code ends up well tested and clearly thought out; TDD is one effective route to that, not the only one and not a requirement.
Related lessons
What to Test (and What Not To)
You can't test everything, and you shouldn't try. Good testing is about covering what mattโฆ
Testing & QualityQuality Beyond Tests
Tests prove code works, but quality is more: readable code, sensible performance, and the โฆ
Build Your First Real ProjectBuild the Core (One Piece at a Time)
Now you build โ but not all at once. Get the smallest piece working, then the next, keepinโฆ