Test Driven Development

What is TDD :- (Test Driven Development)

Test Driven Development is linked to test-first concept of extreme programming language (Xp).

It is a technique to write test cases before you write any implementation code.

This technique was discovered by Kent Beck.

He defined two simple rules for TDD.

- First, you should write new business code only when an automated test has failed.
- Second, you should eliminate any duplication that you find.

a. In traditional approach the usual cycle followed is: -

    Design -> Coding -> Testing.

b. While in TDD the cycle followed is: -

     Test -> Code -> Refactor.

TDD Cycle: -

1. Write a test. 2. Run all the tests. 3. Write the implementation. 4. Run all the tests. 5. Refactor.

TDD Cycle













Why TDD?

There are numerous problems with traditional approach of testing: -

1.If testing is not comprehensive, defects occur in production and create issues.

2.Testing is done after the code is written, or after the programmer has moved on, and this takes a lot of time to fix an issue in the code.

3.Important tests may be missed by the tester who is not the programmer of the code.

4.If test cases are based on the documents which are outdated, it will cause problems.

5.If the tests are not automated then they will not be performed over and over again, regularly or exactly in the same manner.

6.In the traditional approach of testing when an issue is fixed, chances of breaking the code is very high.

TDD solves these problems: -

1.The programmer writes the test and implements the program. This ensures that all possible boundary conditions are tested.
2.The code is based on the tests which guarantee
 – testability
 – exhaustive test coverage
 – no code and test are out of sync

3.No code goes to production without an associated test.
4.Exhaustive tests make future changes and extension easier

Advantages of TDD: -
  1. Improves quality of code.
  2. Less time to fix.
  3. Maintainability of code is easier.
Limitations of TDD: -
  1. Testing insufficient for extensive UI based program.
  2. Misinterpretation of requirement will lead to wrong test and functionality.
  3. Maintaining and writing an extreme number of tests costs more time.
  4. Tests if written badly add to the maintenance overhead of a project.