8 Benefits of Using Test Driven Development

  • Focus on behaviour of a system
  • Shorter feedback loop when coding
  • Identify defects earlier in lifecycle
  • Provide freedom to refactor
  • Codebase should be easier to maintain & understand
  • Tests act as documentation
  • Quicker to make a change with less risk
  • Prevents over-engineering
TDD Cycle

Focus on behaviour of a system

TDD forces you to think about what your code (system) needs to do. By directly working from a list of requirements it keeps you focused and on track to meet those needs. The TDD cycle prevents you from running away on a tangent, as is so often easy to do.

Shorter feedback loop when coding

Tests are quick to run, and run often. Almost instant feedback when something is wrong. Imagine coding a new feature or making a change on an unknown project, but one done in TDD. You would have feedback from the test, without needing to run up the Application that your change had worked and not caused any regression issues, or alternatively, that your change does not work as intended!

Identify defects earlier in lifecycle

One of the cheapest places to fix a defect is in code, prior to deployment or release. TDD helps identify and weed out defects by ensuring test coverage (and test cases) are high, and removing duplication during the refactor phase leaves less places for bugs to hide.

Provide freedom to refactor

Refactor with confidence - If the tests still pass, then no problem! How many projects have you worked on where that can be said with a degree of certainty. Refactoring is key to improve code, reduce code smells, identify possible bugs and generally keeping code as clean as possible (Boy Scout Rule).

Codebase should be easier to maintain & understand

TDD projects tend to be well written, cleaner and therefore easier to maintain. If not refactor! (see above).

Tests act as documentation

TDD can be used to provide developer level documentation with well written and well named unit tests. Code is the best documentation, as you never have to remember to update it.

Quicker to make a change with less risk

Once the initial learning curve is overcome (as with any new skill), TDD should actually speed up development, as making a change or adding a new feature becomes much easier on a well tested clean code base

Prevents over-engineering

Its hard to write complex code when using TDD. Generally if a piece of code is hard to test, it probably can be improved. Because TDD focuses on meeting requirements and small incremental changes, its difficult to stray and add in over complicated or 'might be required' features.

Quality is never an accident; it is always the result of intelligent effort

John Ruskin http://softwaretestingfundamentals.com/software-testing-quotes/