Test Driven Developement

Test Driven Developement

Test-driven development (TDD) is a programming methodology where testing and code development are interleaved. It involves incrementally developing code alongside corresponding tests, ensuring that each increment of code passes its respective test before proceeding to the next increment. Originally associated with agile methods like Extreme Programming, TDD can also be applied within plan-driven development processes.

Fig -

The fundamental TDD process is shown in Figure. The steps in the process are as follows:

  1. You start by identifying the increment of functionality that is required. This should normally be small and implementable in a few lines of code.

  2. 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.

  3. 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.

  4. You then implement the functionality and re-run the test. This may involve refactoring existing code to improve it and adding new code to what’s already there.

  5. Once all tests run successfully, you move on to implementing the next chunk of functionality.

The benefits of test-driven development are:

  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 tests 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 do not need to use debugging tools to locate the problem. Reports of the use of test-driven development suggest that it is hardly ever necessary to use an automated debugger in test-driven development (Martin, 2007).

  4. System documentation:- The tests themselves act as a form of documentation that describes what the code should be doing. Reading the tests can make it easier to understand the code.

Test-driven development (TDD) offers significant benefits by reducing the costs associated with regression testing. Regression testing, which verifies that system changes haven't introduced new bugs, can be costly and impractical when done manually. Automated testing, integral to TDD, dramatically decreases these costs by swiftly rerunning existing tests after system changes.

TDD ensures that all existing tests pass before new functionality is added, providing confidence in the code's integrity. While TDD is most effective in new software development with well-tested components or libraries, it may not be suitable for multi-threaded systems due to potential interleaving issues. Despite its effectiveness, TDD still requires system testing to validate the system against stakeholder requirements, ensuring performance, reliability, and correctness.

TDD has been successful in small to medium-sized projects, with many programmers finding it a more productive approach, though its impact on code quality has yielded mixed results in trials. However, there is no evidence to suggest that TDD leads to poorer quality code.