TDD for Beginners - Welcome to Themba's Blog
in

dotnet.org.za

South African .NET Developer Portal

This Blog

Syndication

Tags

Archives

Welcome to Themba's Blog

{Develop with pleasure}

TDD for Beginners

Hi there

In this post I will work-through a beginner on Test Driven Development (TDD). Before we begin, make sure that you installed testdriven.net personal edition. This can be downloaded free on the following address:  http://www.testdriven.net/download.aspx.

I normally start by creating a blank solution, I’ll call this HelloTDDWorld. Add a new project, a class library, call it HelloTDDWorldTests.

Add nunit.framework.dll as a reference to the project. Delete class1.cs and add a new class, call it HelloTDDWorldTestFixture.cs. Add using NUnit.Framework under using System.Text. Above the class name, add [TestFitxure]. You should have something like this

using System;

using System.Collections.Generic;using System.Text;using NUnit.Framework; namespace HelloTDDWorldTests{    [TestFixture]    public class HelloTDDWorldTestFixture    {           }

}

TDD’s principle is:

·         RED: Write a failing test.

·         GREEN: Make the test pass.

·         REFACTOR: Improve the design while ensuring that all test passes

Failing Test

Add the following to the following HelloTDDWorldTestFixture

[Test]

public void GetGreeting_Should_return_HelloTDDWorld(){    HelloTDDWorld helloTDDWorld = new HelloTDDWorld();   

Assert.AreEqual(helloTDDWorld.GetGreeting(), "HelloTDDWorld");

}     

This will obviously fails if you try to build.  First of all the HelloTDDWorld class is not even defined. This is our failing test (RED). Let us make it pass.

 Add a new project to the solution. This is the project we are testing. Create a new project and call it HelloWorld. Delete Class1.cs. Add a new item call it HelloTDDWorld.cs. Add the newly created project as a project reference to HelloTDDWorldTests. Add a using HelloWorld on the using directive. It should look like this now:

 

 using System;using System.Collections.Generic;using System.Text;using NUnit.Framework;using HelloWorld; 

We are still failing because we don’t have GetGreeting method as yet. Let us fix that by adding the method to the class and return an empty string. The class should look like this now:

using System;

using System.Collections.Generic;using System.Text; namespace HelloWorld{    public class HelloTDDWorld    {        public string GetGreeting()        {            return string.Empty;        }    }} This should make our solution to build successful. Let us now run our test. Right click on GetGreeting_Should_return_HelloTDDWorld method and select Run Test(s) to run the test.Obviously the test will fail, beacause we are returning an empty string. To make the test pass. Go to HelloTDDWorld class in GetGreeting method and return the “HelloTDDWorld” string.

The method should look like this now:

using System;using System.Collections.Generic;using System.Text; namespace HelloWorld{    public class HelloTDDWorld    {        public string GetGreeting()        {            return "HelloTDDWorld";        }    }} Run the test and now it should pass. It is that easy. This get confusing when you have to designi an object that uses services from other objects.

That’s where mock object comes into play. Will deal with it in the next post.

 Thats it for now.Thanks, hope you enjoyed this post. Develop with passion...

 

Published Jun 27 2008, 02:58 PM by themba
Filed under:

Comments

No Comments

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Powered by Community Server (Commercial Edition), by Telligent Systems