Operations · Operations & process management
Test-driven development (TDD)
Best for Startup to Growth
Use it when
You are adding or changing behavior in code and want every change backed by an automated test written first, with the design shaped as you go.
Skip it when
- Business stakeholders need to read and agree the expected behavior in plain language. Use Behavior-driven development (BDD), which builds on this cycle.
What to apply first and what to use instead are listed below.
Inputs
- Change to makeThe feature, fix or behavior change, from a story or ticket with its acceptance criteria.
- Test runnerA unit test framework set up in the project that runs the relevant tests in seconds.
- Existing testsOptionalThe current suite for the code you will touch, to show behavior that must not break.
Steps
- Write a test list of the behaviors the change needs, including cases where existing behavior must stay the same.
- Pick one item, write one automated test for it with setup, the call and assertions, and run it to see it fail.
- Change the code just enough to make that test and all earlier tests pass.
- Refactor the new and nearby code to remove duplication and improve the design, keeping every test passing.
- Add any new cases you thought of to the list, then repeat from step 2 until the list is empty.
- Run the full suite and push the change to the shared mainline.
Output
The changed code with one passing automated test for each behavior on the test list, pushed with the full suite green.
- Test list items
- Test for each item
- Item status (failing, passing, refactored)
- Full suite result
- Commit reference
Done when
- Every item on the test list has a passing test, or is crossed off with a written reason.
- Each new test was run and seen failing before the code that passes it was written.
- Every test has at least one assertion about an outcome.
- The full suite passes after the last refactor.
Measure
For two to three months, track defects found after release in code changed this way, and the rework needed on later changes to that code. Compare with similar code changed before the team adopted the cycle. It worked when escaped defects and rework fall while the time to finish a change holds steady or improves.
Hypothetical example
Orrin, a developer at a freight booking firm, needed to add a fuel surcharge to quotes. Her test list had four items. These were no surcharge under 100 km, 5 percent from 100 km, rounding to the cent, and existing discounts still applying first. She wrote the under-100 km test, watched it fail, and made it pass by returning zero. The 100 km test forced a real calculation. After the rounding test passed, she refactored the pricing code to remove a duplicated discount check. Midway she added a fifth case for cross-border trips. All five tests passed and the change shipped with the full suite green.
Common mistakes
- Skipping the refactor step, which leaves tested but tangled code. Clean up the design each time the tests pass.
- Turning the whole test list into tests before making any pass. Write one test, make it pass, then take the next item.
- Making a test pass by deleting assertions or pasting the actual output in as the expected value. Change the code until the original test passes.
Sources
- Canon TDD · Kent Beck, 2023
- Test Driven Development · Martin Fowler, 2023
By company stage
Apply first
Nothing. You can start here.
Use instead
- Behavior-driven development (BDD)
Business stakeholders need to read and agree the expected behavior in plain language. Use Behavior-driven development (BDD), which builds on this cycle.
Where it sits
Open the operations & process management roadmapRecommended resources
No sponsored resources on this framework yet. Sponsored listings would be labelled here, never in search.
Progress is not saved until you have an account. Create a free account