IntermediateTopic 50 of 104Software & Computing
Test-driven development
Write failing tests first, then code to pass them.
In depth
TDD cycle: write a failing test (red), write minimal code to pass (green), refactor. Drives design toward testable, modular code and provides a regression safety net. Requires discipline and suits well-understood logic better than exploratory UI work.
Key points
- Red-green-refactor
- Small steps
- Behaviour-driven variants
ExampleWrite a test for a leap-year function before implementing it.
What is Test-driven development?
Write failing tests first, then code to pass them.
Why is it important to understand test-driven development?
Writing tests first clarifies what the code must do, and it produces a suite that protects the design as it develops.
What is the red-green-refactor cycle?
Write a failing test, write the minimum code to pass it, then tidy the code while the tests stay green.
Is TDD always worth it?
It helps most where requirements are clear and correctness matters. For exploratory work, writing tests afterwards can be more practical.
How is Test-driven development used in practice?
Typical applications include writing tests for a new function before implementing its logic, specifying the behaviour of a control algorithm with examples and building a safety-critical module one failing test at a time.
What should I learn before Test-driven development?
Study "Unit testing" first (topic 49 of 104 in Software & Computing). It covers the groundwork this topic builds on.
What are the key points of Test-driven development?
Red-green-refactor. Small steps. Behaviour-driven variants.
Can you give an example of Test-driven development?
Write a test for a leap-year function before implementing it.
What comes after Test-driven development?
The next topic is "Integration and system testing", which builds directly on this one.
How difficult is Test-driven development?
It is classed as Intermediate in Software & Computing (topic 50 of 104). It is an intermediate topic. Work through the beginner topics in Software & Computing first.