Monday, November 06, 2006

What is Test Driven Development (TDD) ?

TDD is one of the core methodologies of “Extreme Programming”. Kent Beck is founder of Extreme Programming. Extreme Programming (aka XP) has been successful as it emphasis more on Customer Satisfaction. XP encourages to keep the design very simple and straight forward. XP significantly changes the way we code, it drifts from the traditional style of coding. XP is best suited when the requirements change dynamically. We might have a system whos functionality changes every few months. Some times requirements change when the customer is not very clear about what he wants. Lastly the developer’s productivity increases using XP.
Test Driven Development is known as “Test First”. TDD lays more stress on through unit testing. It’s a usual practice to write and execute test cases after the coding is complete. Goal of TDD is to write a clean code. But in TDD, first we write test cases, write the code and execute test cases. So that’s the reason it’s called “Test First”. It requires a different mind set from developers.
In tradition software practice we follow the below steps.
Create the Technical Design Document.
Write the Code.
Create Unit Test Cases.
Test the code based on Unit Test Cases.
Create the build and pass on to the testing team.
Pit falls of Existing System.
1. Most of the Bugs (which are of course nightmares of developer community) are discovered during the test cycles, rather than at unit test phase.o Identifying the root cause of bugs becomes very difficult.
2. Code becomes tough to manage.o We can never be sure that every line of our code is tested.
Implementing TDD.
TDD takes a different approach compared to traditional software development. Here a test case for a specific functionality is written rather than code. The functional code is written only to pass that test cases, one good thing is that duplication of code is avoided.

Rules of TDD are
1. Test comes first.
2. Write a functional code only if a test case fails.
3. Make sure that your code is testing 100%.
4. It always better to use Code coverage tools.

Initially, adopting TDD can be pain for developer community. Natural tendency of developers is to write code first and test next. More over they feel it’s too much of writing test cases rather coding. Developers are not used to do write and execute large no. of test cases. To implement TDD there are two requirements which are must.
1. A faster complier.
2. An automated tool for executing the test cases.
3. Always use production data for your unit tests.
4. In case if you don’t have production data on time, spend some time to create test data similar to production data.

Automated tool is a must for TDD. Without which executing a unit test case will be very difficult and becomes more time consuming. I have listed available tools for different languages in below section.

Benefits of TDD
1. Biggest Benefit is verification. We can have exhausite set of automated unit test cases that can protect our system against defects.
2. It is best suited to the systems where requirements are very dynamic.
3. We can get simple and clear design.
4. Code becomes loosely coupled and much easier to manager.
5. Test code provides a complete documentation.
6. It ensures that the system is free from defects.
7. Avoids duplication of code.
8. QA time is saved as many defects as many of bugs are worked out up front.
9. Sytem what we develop exacts does it needs to do and no more.
10. High quality unit tests can significantly augment the quality of code and can generate a tremendous amount of confidence in the integrity of tested code.

Originally Posted by:Sudheer Palyam

No comments: