written 8.5 years ago by |
Test-Driven Development (TDD)
Test-driven development is an approach to program development in which you interleave testing and code development.
Test-driven development was introduced as a part of agile methods such as extreme programming. The fundamental TDD process is shown in fig. 1.
- The steps in the Test-driven development process are as follows:
You start by identifying the increment o functionality that is required. This should normally be small and implementable in few line of code.
You Write a test for this functionality and implement this as an automated test. This means that the test can be executed and will report whether or not it has passed or failed.
You then run the test, along with all other tests that have been implemented. Initially you have not implemented the functionality so the new test will fail. This is deliberate as it shows that the test adds something to the test set.
You then implement the functionality and re-run the test. This may involve refactoring existing code to improve it and add new code to what’s already there.
Once all tests run successfully, you move on to implementing the next chunk of functionality.
- An automated testing environment, such as the JUnit environment that supports java program testing, is essential for TDD.
- As code is developed in very small increments, you have to be able to run every test each time that you add functionality or refactor the program.
- Therefore, the tests are embedded in a separate program that runs the tests and invoke the system that is being tested.
- Using this approach, it is possible to run hundreds of separate tests in few seconds.
- Automated testing, which is fundamental to test-first development, dramatically reduces the costs of regression testing. Existing tests may be re-run quickly and cheaply. After making change in test-first development, all existing tests must run successfully before any further functionality is added.
- As a programmer, you can be confident that the new functionality that you have added has not caused or revealed problem with existing code.
Test-driven development is of most use in new software development where the functionality is either implemented in new code or by using well tested standard libraries.
Test-driven development may also be ineffective with multi-threaded systems, the different test may be interleaved at different times in different test runs, and so many produces different results.
- If you use test-driven development, you still need a system testing process to validate the system; that is, to check that it meets the requirements of all of the system stockholders.
- System test also test performance, reliability, and checks that system does not do things that it shouldn’t do, such as produce unwanted outputs etc.
- Test-driven development has proved to be a successful approach for small and medium- sized projects and it is most productive way to develop a software.
Advantage of Test-driven Approach
1. Code coverage
- In principle, every code segment that you write should have at least one associated test. Therefore, you can be confident that all of the code in the system has actually been executed. Code is tested as it is written so defects are discovered early in the development process.
2. Regression testing
- A test suite is developed incrementally as a program is developed. You can always run regression test to check that changes to the program have not introduced new bugs.
3. Simplified debugging
When a test fails, it should be obvious where the problem lies. The newly written code needs to be checked and modified.
You need not to use debugging tools to locate the problem. Reports of the test-driven development suggest that it is hardly ever necessary to use an automated debugging in test-driven development.
4. System documentation
The tests themselves act as a form of documentation that describe what the code should be doing. Reading the test can make it easier to understand code. One of the most important advantage of test-driven development is that it reduces cost of regression testing.
Regression testing involves running test set that have successfully executed after changes have been made to a system. The regression test checks that these changes have not introduced new bugs into the system and that the new code interacts as expected with the existing code.