Wednesday, January 29, 2014

Test Driven Development (TDD) - Explained

Test Driven Development (TDD)

Define

Test Driven Development (TDD) is about writing the test first before adding new functionality to the system.

Explain

The cycle at the heart of TDD is:

  • Write a test (test fails - RED)
  • Write some code to get the test to pass (GREEN)
  • REFACTOR the code to be as simple an implementation of the tested features as possible
  • Repeat

Agile developers work in this circle of life when adding new code. Write the test first. Make it pass. Then refactor.

Tests provide feedback regarding:

  • Whether the system works
  • If the system is well-structured

By writing the Test first, we get double the benefit. 

The Exercise of Writing the Tests:

  • Makes us clarify the acceptance criteria for the next piece of work - we have to ask ourselves how we can tell when we are done (design)
  • Encourages us to write loosely coupled components, so they can easily be tested in isolation and, at higher levels, combined together (design)
  • Adds an executable description of what the code does (design)

Running the Tests:

  • Detects errors while the context is fresh in our mind (implementation)
  • Let us know when we've done enough, discouraging over-loading the code and adding unnecessary features (design)

This feedback cycle can be summed up by the Golden Rule of TDD: "Never write new functionality without a failing test"


Contributors:

"Growing Object-Oriented Software, Guided by Tests" by  Steve Freeman and Nat Pryce