Discount upto 50%

A Brief Study into 3 Phases Of Test Driven Development (TDD)

  • Agile
Created on :
May 26, 2014
Updated on :
October 27, 2022
Default Image

Red, Green and Refactor is the three phase of Test Driven Development and this the sequence that get followed while writing code. When followed, this order of steps helps ensure that you have tests for the code you are writing and you are writing only the code that you have to test for. Adding extra functionalities sometime looks good, but what if same was not requested by the user? It may seem a good idea, but functionality that is unneeded and unused, but requires resources to create and maintain is wasteful. Better is to let the requirement define the functionality and features and not the developers.
Test Driven Development

The Red Phase

I think this is the most difficult phase because the developer has to write test against no code. Considering what to test without having code looks difficult, but it’s only a mindset shift and nothing more. Most of us adopt it easily and then become habitual of it. You write the 1st test even without writing code to declare class and method so basically your test will not even compile. This is again the same as having test fail. Once you fix the compilation error, then the next is to execute your test to fail it. This is where you get a red flag by your unit test framework.

The Green Phase

Next step is to write the code, but only code that needed to pass the 1st test. Compile your code and run unit test framework. The concept of writing “just enough” code to make a test pass is another philosophical hurdle that developers new to TDD sometimes struggle to get over. After the 1st test pass, you start writing the next test to fail and then keep adding code to pass them.

The Refactor Phase

Until now the goal was to write code to get the unit tests to pass. You weren’t considering other aspect like design patterns, maintainability of code, code quality and readability etc. The focus of this phase is to look at these because you have covered business requirement in the form of unit tests. While refactoring you are not worried about missing functionality because whenever you make changes in the code and compile again, then test cases will execute automatically to confirm functionality.

By now you must have understood the three important phases of Test Driven Development (TDD). You may also like to check my TDD Blog to learn more about TDD approach for developing .Net MVC application. You can also refer following YouTube video to have more insight into it:

Do not forget to send us your feedback or queries. We love to hear from you.