Test-driven development (TDD) is an evolutionary approach to development which combines test-first development where we write a test before we write code.
TDD can be defined as a programming practice that instructs developers to write new code only if an automated test has failed. This avoids duplication of code. The primary goal of TDD is to make the code clearer, simple and bug-free.
TDD relies on very short development cycle. Once all the tests pass it signals the end of iteration.
If there are more features that needs to be implemented in your product, product will go through the same phases again but this time with new feature set and more tests.
Some Key Points of TDD
- Goal of TDD is specification not validation.
- TDD is both an important agile requirement and agile design technique.
- TDD is related to the test-first programming concept of extreme programming.
- TDD is neither about Testing nor about Design.
- TDD ensures that the source code is thoroughly unit tested and leads to modularised, flexible and extensible code.
- Test-driven development refers to a style of programming in which three activities are tightly interwoven: coding, testing and design.
- Add a Test
- Run Test along with your existing code and see if new or existing one fails
- If Test Fail, Write code and Implement missing steps.
- Run Test again and refactor if needed
- Repeat Cycle.
- Improve Productivity: Writing tests in TDD forces you to think about use cases, and improves productivity. Even considering that the amount of code based on writing unit tests, the total implementation will be shorter and less buggy.
- Flexible & Extensive Development: If common TDD practices are followed, the code developed is flexible, extensible and easy to maintain.
- Automatic Regression Detection: Automatic regression detection on every incremental update.
- Easier documentation: unit tests are self-documenting, easier to read and understand.
- Blind Spots: When developers themselves write the unit tests, the tests may share the same blind spots as the code.
- False Sense of Security: At times, high numbers of passing tests can create a false sense of security, causing fewer testing activities during integration testing, potentially causing problems.
- Maintenance Overhead: Tests become part of the maintenance overhead. Badly written tests can further cause more costs in maintenance or updating.