Test Driven Development - An Introduction

Everybody is talking about Test Driven Development (TDD). But what is TDD? And why it supposed to change the way we develop software? This is the first blog in a serie on Test Driven Development in practice in .NET.

TDD is a design pattern that follows a test-first development approach. The developer writes code that tests the functionality before the functionality is actually created. The TDD development model is also known as Red-Green-Refactor style development. It follows the below flow:

  1. Red: Write a test case before you start writing code for functionality. This test case will fail (red) because there is nothing yet to test.

  2. Green: Write the actual code for the functionality. Keep running your test until it passes (green).

  3. Refactor: Improve your code and run the test after each improvement. As long as your test passes, you know that your refactoring efforts did not break the functionality.

If you keep following this approach, you will end up with a test program that can automatically test your functionality (ideally you would have a coverage of 100%, but that's usually not feasible or needed). This not only gives a great advantage in testing when the project continues, but it also provides a structured approach that forces you to analyze requirements before writing functionality.



Benefits of TDD

Traditional development styles produce products that face problem when the code is modified. Often, bugs are introduced at unexpected places which makes managers and developers hesitant to change functionality. However, if the major part of the project is covered by tests, assurance can be given that no unexpected bugs are introduced.

Clean interface/Code
The TDD method forces to work in a structured manner and to modify your code in small chunks, otherwise unit testing becomes complicated. The result is clean code that is well structured and easy to read.

Encouragement to the developer
It gives a developer the confidence that nothing breaks while changing or refactoring large applications.

Overall, the amount of bugs in the project will be less and the bugs which are there can be resolved more easily. So although TDD requires an initial time investment, it will save time in the long run.


Challenges of implementation of TDD

Time Investment
TDD takes additional time for the initial implementation of functionality. Therefore, management should understand that the pay-off comes later and that this initial investment is definitely worth it.

Design Impact
TDD requires you to plan out how your classes will operate before you write the actual code. Writing the test first forces you to fully comprehend the functionality and what it must accomplish before you write the code.

Difficult to test GUI
Interfaces can to be tested by using mock objects but it can be difficult to do this correctly.


What's next?

This blog gave a general outline of TDD and it's advantages and challenges. In my next blog, I will give a practical example of how to implement unit testing in a. NET development.


Leave a Reply