Test Driven Development (TDD) in a Nutshell

Topics: General - public
Coordinator
Apr 1, 2013 at 2:52 PM
What is TDD, and how do you develop your code a TDD way? I'll as brief as possible. The name itself says it all (Nomen est Omen): Test Driven Development. It means that you write test first, and let it drive your development.

TDD is known by its mantra: Red-Green-Refactor, and that's how TDD really goes. Let's look at each of the phases now.

Red
Before you even create your first class (file/member/whatever) in any other project than Test project, you write your first test first. Before you can run this first test, you need to create class you wish to test. Then you run this test, and let it fail. You'll see red test failed message, because what you wanted to test, isn't implemented yet.

Example VB.NET:
    <TestMethod()>
    Public Sub NewProjectCreation_ConstructorStringIsTheNameOfTheProject()
        Dim pName = "New project's name"
        Dim p As New Project(pName)

        Assert.AreEqual(pName, p.Name)
    End Sub
Example C#:
        [TestMethod]
        public void NewProjectCreation_ConstructorStringIsTheNameOfTheProject()
        {
            var pName = "New project's name";
            var p = new Project(pName);

            Assert.AreEqual(pName, p.Name);
        }
It will fail, because there is no code to assign projectName to .Name property of the Project class in its constructor, yet.

Green
Then you go, and write your code. You run your first test (over and over again), until your code doesn't do what your test is expecting it to do. Here your goal is to stick with the lowest and easiest amount of code needed to pass the test.

Example Project class implementation in VB.NET:
Public Class Project

    Sub New(projectName As String)
        Name = projectName
    End Sub

    Public Name As String
End Class
Example Project class implementation in C#:
    public class Project
    {
        public Project(string projectName)
        {
            Name = projectName;
        }

        public string Name;
    }
Refactor
Last step in the process is refactoring the code. Right now, there's nothing to refactor in test or in Project class, but it is really important to do this step after each Green phase of each new test, or it will bite you in the a** later (happens to me a lot).

You'll notice that Project class has public field Name, what final version of the class probably won't allow (or perhaps will, why not?). Right now we don't want to have it read-only (get only, or private set only), right now it's a field not property, because there's nothing to do in get and/or set method of the property. Implementing further features/demands will drive us to do that, if needed (ever heard for YAGNI (you ain't gonna need it)), and that is the core of TDD.

I hope this simple example explains it good enough to give TDD a shot. It will allow you to manage complex programming projects, and not to be afraid of changing anything, because running tests will show you immediately what and how your changes to the code impact the rest.