TestDrivenDevelopment a coding practice Rupesh Bhochhibhoya Software Engineer, Telogical Systems
Those who are unaware of TDD or haven’t done TDD
Those who know what TDD means but haven’t got its flow
What is TDD ? One of the best software development practice/habit/technique used in Agile methodology that relies on the repetitive short development cycle. Each short development cycle is formed following 3 basic rules
Requirements Requirements Design TDD model Design Testing Implementation Implementation Testing waterfall model Deployment & Maintenance Deployment & Maintenance
Rule #1 Write a test first that is just enough to fail before any production code that you are going to write. There should be only one reason for test to fail. So, each test is to specific one logical unit. The test case defines a desired functionality you want to see. This is also called Test-First approach.
Rule #2 Write a production code that is just enough to pass the test. This make sure production code is doing exactly what test wanted. No any abnormal behavior.
Rule #3 Refactor the production code to remove duplicationwithout changing the external behavior as desired by test units. We also refactor test code to make it more readable, remove duplication and add more logical unit test.
Refactor Production code Run test suite Fail How TDD ? Pass Add a test Run the complete test suite, ensure all pass. Add a new test code that is just enough to fail. Run the test and ensure it fails Then add production code just enough to make the test pass. Fourth step is to run the test again. If it fails, need to work again in production code and retest. Once test passes, the next step is to refactor any duplication out of your design as needed. SO: TDD = TFD + Refactoring… Pass Run the test Pass Fail Add/Modify Production code Fail Run the tests
Good Test Code in TDD… Describes the behavior of the code you are going to write. Run fast. Run in isolation. Should not be data driven. Should not communicates across the network. Should not interacts with file system
TDD Demo You write your own test You have a testing framework(Junit). Development environment must provide rapid response to small changes(CI) Eclipse IDE, Etc.
TDD reduces programmer’s bugs When you already have Tests that documents how your code works and also verifies every logical units, programmer’s bugs are significantly reduced resulting more time coding, less time debugging.
TDD builds developer’s Confidence you can confidently refactor your production code without worrying about breaking it, if you already have test code written, it acts as safety net.
TDD provides working specification Tests on TDD describes the behavior of the code you are going to write. So, tests provides better picture of specification then documentation written on a paper because test runs.
TDD enables integration more often allowing larger team to work on same code base
TDD guarantees testability
TDD reduces product delivery time
TDDproduces highly cohesive and loosely coupled Systems