Importance of TDD(Test Driven Development):
TDD (Test-Driven Development) starts with designing and developing tests for every small functionality of an application. In TDD approach, first, the test is developed which specifies and validates what the code will do.
In the normal Software Testing process, we first generate the code and then test. Tests might fail since tests are developed even before the development. In order to pass the test, the development team has to develop and refactors the code. Refactoring a code means changing some code without affecting its behavior.
Being a developer my objective is always deliver defect less code.Now question is how can i achieve this? There is no silver bullet tool in this world that promises you that your code is 100% defect less.You need to adopt and implement certain process that may not 100% guarantee you that your code is defect less but also makes sure you that your code is 100% tested, verified and bug free. As per my opinion TDD ( Test Driven Development) methodology is one of the best development practices to achieve this.
Step-1 Break your requirement into smaller unit.
Step-2 Then write small unit test case to fail for each of the small requirement.
Step-3 Then write code to correct fail test case and make it pass.
Step-4 Then refactor passed unit test case again to improve code quality until you or your boss satisfied.
Then repeat Step2...
What you have achieved at the end ?
- You actually fixed the bugs in very early stage before QA team introduce that and you maintain a very good code quality.
- You felt more confident on your code by getting fast feedback from your own unit test result and you are now out of fear of ‘breaking’.
- You've tested single responsibility of each of the unit and that will not fail in future by adding more units those have different responsibilities.
- You saved lots of time to fix the bugs in QA cycle and become more productive. Indirectly you cut down lots of maintenance overhead from your organization and show them confident about painless roll-out of new software in production.
Step 1: My requirement is to word "jyoti" is present in a HashMap with key '1'. The first test case will fail as "jyoti" is associated with key '2'.
Step 2: Then i made the correction to pass the test case. Now "jyoti" is asscociated with key '1' and test passed!
But this code need refactoring..
Step 3: Refactoring code.Now I am not using any Iterator, Map.Entry and Set
Step 4: Ran the code coverage to check all code tested or not. Seems 100%