Test Driven Development: A Detailed Guide

Software industry is always on a look out for new ways to produce quality software. Over the time, there have been many models evolving to serve this purpose. Test Driven Development is one such concept getting more and more attraction in recent times. More so because its many advantages and its methodology which puts quality aspect at the center of all activities. We will explore about Test Driven Development (aka TDD) in this article with its important concepts.

How TDD works?
While traditional software development models follows testing after implementation strategy, TDD puts testing at a core of development activities. In TDD, once the design is done, a test is written to express the design. The test should cover all possible inputs, outputs, errors and warnings regarding the feature to be developed. That test should fail initially. Developer then proceeds to write the code to address all the tests. The code is then subsequently refined/re-factored if needed.

As described above, development is done in small iterations. It is related to test-first programming concepts of extreme programming. Also, TDD recommends automated testing. Test automation is beneficial to verify that addition of new features does not break the existing features. Below is a graphical representation of development cycle in Test Driven Development.

Test Driven Development

Test Driven Development image courtesy: WikiPedia

As you can see, TDD follows Design–>Test–>Implement–>Test strategy rather than traditional Design–>Implement–>Test techniques. To use TDD, you should follow-what many calls-a ‘red, green, refactor’ cycle. Let’s see what it is at high level. When you write a test, it should fail(Red) at first. Once code is developed for the test, it should pass(Green). Developers can further refactor the written code to accommodate new functionalities.

Benefits:
Below are some benefits of TDD:

  • Makes developer focus on quality of the product.
  • May result in reduced number of bugs.
  • Writing a test first ensures a deeper and earlier understanding of the product requirements.
  • Elimination of defects earlier in the cycle. This helps in avoiding lengthy and tedious debugging activities.
  • Written code is often modular making it flexible and easily extensible.
  • Maintenance time is less since code is solidly tested earlier.

A study conducted in 2005 suggests that programmers who wrote more tests found to be more productive.

Disadvantages:
Below are certain disadvantages of TDD:

  • Learning curve is steep for implementation of this technique.
  • It may not be of much help in case of legacy systems.
  • May cause team to believe that full fledged functional testing is not required.
  • Management may be reluctant towards this approach as time taken for testing is more.

There is a lot of debate going on about relative merits of Test Driven Development in software development. As it turns out, there are not many organizations right now which are following this method. Are you involved in TDD in your project? If yes, let us know your views in comments.

Leave a Reply

Your email address will not be published.