Operations · Operations & process management

Test-driven development (TDD)

Best for Startup to Growth

1RED2GREEN3REFACTOR

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 make
    The feature, fix or behavior change, from a story or ticket with its acceptance criteria.
  • Test runner
    A unit test framework set up in the project that runs the relevant tests in seconds.
  • Existing testsOptional
    The current suite for the code you will touch, to show behavior that must not break.

Steps

  1. Write a test list of the behaviors the change needs, including cases where existing behavior must stay the same.
  2. Pick one item, write one automated test for it with setup, the call and assertions, and run it to see it fail.
  3. Change the code just enough to make that test and all earlier tests pass.
  4. Refactor the new and nearby code to remove duplication and improve the design, keeping every test passing.
  5. Add any new cases you thought of to the list, then repeat from step 2 until the list is empty.
  6. 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

By company stage

  1. Pre-IdeaNot applicableThere is no code being written.
  2. IdeaUsefulPrototype code changes fast, so tests first can slow proving the idea.
  3. Early StartupUsefulHelpful for core code, though speed of learning still dominates.
  4. StartupCriticalScaling a codebase with new engineers needs tests backing every change.
  5. GrowthCriticalLarge teams and many customers need automated tests to ship safely.
  6. ScaleUsefulEstablished test practices exist, so it remains a useful engineering habit.
  7. EnterpriseUsefulTeams usually have testing standards already, so it serves as a team level practice.

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.

Recommended 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