TDD - Red Green Refactor Example


In this tutorial I would like to explain TDD (Test Driven Development) with the common Red Green Refactor example.

A note on TDD

TDD (Test Driven Development) is a software development process in which the unit test will be written first and after that the original code. In TDD, the design and development of the code is through the unit tests.

In the traditional unit tests, the unit test is written after the original code is written. This is only for long term maintainability. But in TDD the unit test is written first and code evolved through it.

Red Green Refactor

Red Green Refactor is an interesting concept in TDD. The stages are given below:

Red - First a failing unit test is created and it results in red status

Green - We will modify the associated code to just make the unit test pass - resulting in green status

Refactor - Once the test is passing we can refactor the code so that the original implementation is done.

Example: In the following example, I would like to take you through the Red Green Refactor steps. Here we are trying to create a Stack data structure which is already in the library of .Net. We can call our stack as MyStack which operates on string and have Push method.

Step 1: Create a red test project

Create a new test project and name it TDDExample.

TDD1.gif

Step 2: Rename the default UnitTest1.cs to MyStackTest.cs

After renaming the file name just add the following unit test.

[TestMethod]
public void MyStack_Push_Test()
{
    MyStack myStack = new MyStack();
    bool result = myStack.Push("abcd");

    Assert.AreEqual(true, result);
}


Please note that first we written the unit test.. till now the original MyStack class is not created.

So place the cursor over MyStack and use Ctrl+. (to Generate Class for us). Repeat the same for Push() method to generate the default method.

So now the new class will look like:

class MyStack
{
    internal bool Push(string p)
    {
        throw new NotImplementedException();
    }
}


Step 3: Run the unit test and get Red

Run the unit test and you can see the test will fail.

TDD2.gif

Step 4: Modify the method just enough to make it Green

In this step we are just trying to make the unit test pass. We are returning true without adding the implementation inside the method. So the new method will look like:

internal bool Push(string p)
{
    return true;
}

Running the test again, we can see the test passes.

TDD3.gif

Step 5: Refactor

Although the test is passing now, the actual implementation is not right. Now we need to add the right code in our Push() method. This will be the Refactor step of Red Green Refactor style and the unit test should be passing again.

Our actual implementation would be storing the string to an internal list. The final class would look like:

class MyStack
{
    private IList<string> _list = new List<string>();

    public bool Push(string str)
    {
        _list.Add(str);

        return true;
    }
}

Running the unit test will be green again.

TDD4.gif

Conclusion

In this tutorial we were trying to create a small example of TDD involving Red Green Refactor style.
 


Similar Articles