Test Driven Development (TDD)
Definition - What does Test Driven Development (TDD) mean?
Test driven development (TDD) is an software development approach in which a test is written before writing the code. Once the new code passes the test, it is refactored to an acceptable standard.
TDD ensures that the source code is thoroughly unit tested and leads to modularized, flexible and extensible code. It focuses on writing only the code necessary to pass tests, making the design simple and clear.
Techopedia explains Test Driven Development (TDD)
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 testability. Testing on a small amount of code is performed to trap errors that occur in the tested code. Then the functionality is implemented. This is referred to as "red green refactor" where red means fail and green shows a pass. These steps are then repeated. The first goal of a programmer is to focus on the task at hand and to pass it.
The different steps involved in a test driven development cycle are:
- Add a test: Every new feature in TDD starts with a test which has to fail as it is put into effect before any features are implemented. The essential requirement to write a test before feature implementation is a clear understanding of the requirement by the developer. This is accomplished through user stories and use cases. Hence a developer understands the requirement before writing the program code.
- Run all tests and check if the new code fails: This ensures correct working of the test harness and that the new test does not pass by mistake without any new code. This step also tests the test and eliminates the possibility that the new test always passes.
- Write code: The next step that follows is writing code that clears the test. The new code is not perfect but is later modified as per the requirements. It’s merely designed for testing and does not enclose other functionalities.
- Run automated tests: 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: This is similar to removing duplication. A refactoring does not damage any existing functionality and helps in removing duplication between production and test codes. The code is now cleaned as required.
- Repeat: The cycle is repeated as in the previous cases with a new test. The essential requirement is that the size of the steps should be small, with around 1 to 10 edits between every test run. If new code does not satisfy a new test, the programmer should perform additional debugging. Continuous integration provides revertible checkpoints.