What is Test-Driven Development (TDD)?
Test-Driven Development (TDD), is a method of software development in which unit testing is repeatedly done on source code. TDD enables the programmer to take small steps while writing software. The test is written before testing the functionality and ensures that the application is suitable for testing.
The concept is to “get something working now and perfect it later“. After each test, refactoring is done and then the same or a similar test is performed again. These steps are then repeated many times until each unit is functioning according to the desired specifications. Testing on a small amount of code is performed to trap errors that occur in the tested code .Test-driven development is related to the test-first programming concepts of extreme programming, which started in 1999 but has created more interest recently.
Test-Driven Development can produce applications of high quality in less time than other older methods. Proper implementation of TDD is possible only when the developers and testers understand the application and its features accurately. The methodical nature of TDD ensures that all the units in an application have been tested for optimum functionality, both individually and in synergy with one another.
For detailed code example, please visit Test Driven Development.
Steps involved in a Test-Driven Development:
- Add a test: The first thing to do is to write an automated test for the code. The essential requirement to write a test before feature implementation is necessary for clear understanding of the requirement by the developer. While writing the automated tests, the developer must take into account all possible inputs, errors, and outputs.
- Run all tests and check if the new code fails:. As long as the new code fails, it means that it is not ready. The code can be fixed until it passes all tests. This ensures that new test does not pass by mistake without any new code.
- Write code: The next step is writing code that clears the failed test. The new code may not be perfect but can be later modified as per the requirements.
- Run automated tests again: If every test case produced easily passes the test, it implies that the code meets all required specifications. Hence the final step of the cycle can be started.
- Refactor code: Once the code passes the test refactoring is done for the cleanup. A refactoring does not damage any existing functionality and helps in removing duplication between production and test codes.
- Repeat: The cycle is repeated as in the previous cases with a new test. If new code does not satisfy a new test, the programmer should perform additional debugging.
Advantages and Disadvantages of TDD
- Test-driven development ensures that all written code is covered by at least one test. This gives the programming team, and subsequent users, a greater level of confidence in the code.
- Test-driven development offers the ability to take small steps when required. It allows a programmer to focus on the task at hand as the first goal is to make the test pass.
- Large numbers of tests help to limit the number of defects in the code. The early and frequent nature of the testing helps to catch defects early and prevents them from becoming expensive problems.
- TDD can lead to more modularized, flexible, and extensible code. This is because the methodology requires the developers to think of the software in terms of small units that can be written and tested independently and integrated together later.
- The automated tests resulting from TDD tend to be very thorough: they detect any unexpected changes in the code’s behavior. This detects problems that can arise where a change later in the development cycle unexpectedly alters other functionality.
- Test-Driven Development does not perform sufficient testing in situations where full functional tests are required to determine success or failure, due to extensive use of unit tests
- Tests become part of the maintenance overhead of a project. For example, badly written tests that are themselves prone to failure, are expensive to maintain.
- A high number of passing unit tests may bring a false sense of security, resulting in fewer additional software testing activities, such as integration testing and compliance testing.