Test Driven Development – Brief Explanation
This blog explains about Test Driven Development – Brief Explanation . It is explained clearly well below :
Test Driven Development
TDD or Test Driven Development is a process for testing the functionality of the code. It is a repeated process where the testscript is written first for the particular requirement then the business logics were driven using the test scripts.
Main Moto of Test Driven Development:
TDD is used mainly to enable the developer to think in QA perspective and write the test cases or test scripts before writing the functional code.
Is TDD is different from Unit testing?
Yes, In normal unit testing the functional code were developed and then unit testing ( decision coverage, branch coverage, loop testing ) were performed while in TDD first the test scripts or test cases are written using the selected requirement then the functional code is developed using the test scripts.
Understanding TDD:
In TDD the initial test scriptmust fail, then the developers should write logic for the particular requirement and make the test case to pass.
Note:
TDD is not a framework/ tool/ Technology, it is a thought process which makesdevelopers to write test cases for the particular functionality.
Step 1: Add a test script in a class, corresponding to the particular requirement.
Step 2: Run the test script, eventually it will fail.
Step 3: Write a functional code in separate class
Step4: Run the test script, if they fail, update the functional code
Step 5: Repeat these steps until the test script should pass.
Example code for adding two numbers to understand TDD:
Test script and corresponding functional code is developed to portray how TDD works:
Step 1: (Test Script)
@Test
Public void Add ( )
{
Addition obj= new Addition ( );
int result = obj.add (10, 20);
Assert.assertEquals (20, result);
}
Step 1 Explanation:
Test script must developed first and mandatory it should fail. In this case this test script will fail because we haven’t created an Addition class, without the class name there is no way to create an object. Even though we know this program won’t compile, user must compile or run this test case, eventually this test case will fail.
Step 2: (Functional Code)
Class Addition {
Public static void main (String [] args) {
}
Public int add (int num1, num2) {
return 0;
}
}
Step 2 Explanation:
It’s time to write the functional code in new project. Create a new project, new package, and add a new class called Addition. Method declared&exact functional code is not developed. Again run the test script and check the output for the test script.
(Eventually test case will fail.)
Step 3:
Class Addition {
Public static void main (String [] args) {
}
Public int add (int num1, num2) {
return num1+num2;
}
}
Step 3 Explanation:
This steps should be repeated until the functional code meets the test script’s requirement criteria. Now run the test script, the test case should pass as we developed the exact functional code.
Note: For this particular scenario, test process concluded in 3 steps, it doesn’t mean all the test should using TDD should end in three steps. Steps are directly linked with the complexity of the requirement.
TDD is also called as Red Green Refractor.
Red : Initially test script fails
Green : After adding suitable code the test script pass
Refractor : In refractor stage, Test scripts are still in green, Refractor is thinking about how to implement code better or more efficiently.
Some of the advantages in using Test Driven Development:
- This process forces to write test cases.
- Intentions are clear, user understands the requirement well.
- Documentation evolved for future reference.
– AUTHOR
KOGUL
Reference taken from:
https://www.infoq.com/articles/testers-TDD-teams
https://www.youtube.com/watch?v=1JMa2O2ia-I
https://www.youtube.com/watch?v=5gMBGVNR8wE
______________________________________________________________________________________