Blog‎ > ‎

5. TDD

Test Driven Development (TDD)

TDD


Test Driven Development (TDD)

Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle:

  • First the developer writes an (initially failing) automated test case that defines a desired improvement or new function,
  • Then produces the minimum amount of code to pass that test,
  • Finally refactors the new code to acceptable standards.

Test-driven development is related to the test-first programming concepts of extreme programming, begun in 1999, but more recently has created more general interest in its own right.

The process flow for TDD is as follows

Test Driven Development (TDD)

Example of Test Driven Development

Program to calculate total of 2 numbers

Step 1 : Write a Test Function and Actual Function with no vode

Function CalcTotal {

<<NO CODE>>

}

Function TestCalc {

<<Start writing the TestCalc Function First>>

}

Step 2: Write only the Test Case Code

Function CalcTotal (Number1,Number2) {

<<NO CODE>>

}

Function TestCalc () {

Number 1 = 20;

Number 2 = 30;

Result = 50;

ResultFromFunction = CalcTotal(Number1, Number2);

If (Result EQUALS ResultFromFuntion) then return TRUE else return FALSE;

}

END RESULT = FALSE; TEST CASE FAILED

Step 3 : Write Code to pass the test case

Function CalcTotal (Number1,Number2) {

return (Number1+Number2); //WRITE CODE TO PASS TEST CASE

}

Function TestCalc () {

Number 1 = 20;

Number 2 = 30;

Result = 50;

ResultFromFunction = CalcTotal(Number1, Number2);

If (Result EQUALS ResultFromFuntion) then return TRUE else return FALSE;

}

END RESULT = TRUE; TEST CASE SUCCESS

Step 4 : Clean Up the code (REFACTOR)

Function CalcTotal (Number1,Number2) {

//This function calculates total of 2 numbers

result = Number1+Number2; //Add 2 numbers

return (result);

}

Function TestCalc (Number1,Number2,Result) {

ResultFromFunction = CalcTotal(Number1, Number2);

If (Result EQUALS ResultFromFuntion) then return TRUE else return FALSE;

}