TDD – Test Driven Development is a Development-Testing methodology that helps developers to achieve Speed, Robustness and Quality with its specifically structured mechanisms. It is a software development approach where a dev write unittests before the application code.
Joe Birch, a Senior Android Engineer at Buffer:
“Following TDD has not only allowed us to write simpler, more understandable code but has allowed us to think more about what we are implementing before actually implementing it. This extra space to think, allows us to understand and test for the different logical paths before getting caught up in implementation details. This, in turn, helps to reduce development time by creating something less error-prone from the start.”
If we unittest but don’t refactor, then we will find (some) bugs earlier. If we unit test and refactor then we will prevent bugs—fewer mistakes will be made, so less to detect. So let’s measure those two outcomes as our primary measures.
Pattern for Arranging and formatting code in UnitTest methods:
Follow – 3 A’s
- Arrange all necessary preconditions and inputs.
- Act on the object or method under test.
- Assert that the expected results have occurred.
Pattern to follow for Naming of unittests:
- Given – inputs
- When – under certain conditions
- Then – output generated
Below are some examples:
5 major benefits of Test Driven Development:
“A tested code is always given preference to go out on Production. And through writting unit-tests a developer can reduce it’s development effort to re-work if in case there is a new feature enhancement.”
- Pushes developer to write a clean and robust code.
- Developers have a clear target in mind and that’s very important in agile environment.
- Future enhancements would be much easier, as developer would know what to change & where to change, and yes test new code at the same time.
- Code functionality can be easily understood as let’s say a developer’s job is filled in by another dev then the new dev can easily understand the code functionality by going through the unit tests.
- TDD reduces the chances of accidentally packaging tests with production binaries.
- There are many agile engineering practices that a team could adopts and many of those agile engineering practices are dependent on having unit tests.