Search
Latest Articles
Richard Costall - Richard Costall was sent a preview copy of Manning's Silverlight 4 in action to review, will it be an 'Action Man' or just 'Out of action!'
Richard Allen - Richie Allen breathes a sigh of relief after all the hard work on Fest10 and has time to reflect on the event. WATCH THE VIDEOS
Skip Navigation Links
Login / Register
Article Quote
Test Driven Developer is not just a way of testing an application; it can also greatly enhance the core design of the software.
Ben Hall
 Member Quotes
 Latest Articles
Manning: Silverlight 4 in Action Review
Richard Costall was sent a preview copy of Manning's Silverlight 4 in action to review, will it be an 'Action Man' or just 'Out of action!'
Fest10 Review
Richie Allen breathes a sigh of relief after all the hard work on Fest10 and has time to reflect on the event. WATCH THE VIDEOS
Packt's Silverlight 4 Data and Services Cookbook review
Richard puts Packt's Silverlight 4 Data and Services Recipes book through it's paces, but how does it fair against Richard's Challenge
Microsoft Silverlight 4 Business Application Development Review
No sooner has Silverlight 4 been released, than a book drops on Rich's doormat - Microsoft Silverlight 4 Business Application Development by Packt
SharePoint Starter No 2
In his second short article on SharePoint Dave answers the question "What is the difference between WSS 3.0 and MOSS 2007?"
Articles...
Conferences Conferences
Mix10
Mix10
Partner Showcase Partner Showcase
ORCSWeb is one of the premier ASP.NET 2.0 and SQL 2005 hosting companies, and also the home of the NxtGenUG Site.
ORCSWeb is one of the premier ASP.NET 2.0 and SQL 2005 hosting companies, and also the home of the NxtGenUG Site.
Powered by ASP.NET 2.0
NxtGenUG Article
Ben Hall Saturday, November 04, 2006
Member Ben Hall talk about the concept of Test Driven Development and how you can start using this approach with your code.
The Article 
Introduction

This article outlines the concept of Test Driven Development and how you can start using the approach with your code.

What is Test Driven Development?

The core concept of TDD is writing a test case before writing code, then running the test case within a unit testing framework so it fails.  After you have a failing test, you develop code to make the test pass.  However, you might be asking yourself, how can you do that as if no code has been written and how can it compile? Well a side rule involves writing just enough code to make sure the test suite and the code compiles but nothing more.

Myth-buster: Test Driven Developer is not just a way of testing an application; it can also greatly enhance the core design of the software.

The reason for writing a test first is because TDD aims for simplicity; if there are no requirements then there is no reason to write code.  This prevents implementing functionality that is not tested and not required by the solution making the code overall easier to manage and maintain.

My personal favourite reason for TDD is the ability to regression test effectively.  If an application has followed the TDD approach then a full test suite relating to every class in your application exists. This means you can easily change and, more importantly, refractor your code when you feel it is required without having to worry about knock-on problems or breaking a component. All you have to do is compile and run the test suite to verify everything is working. You can even get your build server to run all the tests every time someone commits a change.

Sample TDD Application
So that’s enough sales…lets write some code.  I will be using the MbUnit Unit Testing framework, you can download a copy from www.MbUnit.com which contains the necessary assemblies required to write and execute test code. This is a full open source application, developed using C# 2.0, but can run as part of .Net 1.1 or 2.0 projects.

Myth-buster: Test Driven Developer is not just a way of testing an application; it can also greatly enhance the core design of the software.

The example I am going to use is a method which returns three times the amount of the value given as an parameter. The first task is to create a new C# class library project within Visual Studio for the test cases/methods and I will call the project MyFirstTest.  A reference to MbUnit.Framework.dll is required, which can be found in C:\Program Files\MbUnit\.

MbUnit Add Reference Dialog
Figure 1 – MbUnit Add Reference Dialog

Within your class file add a using directive for the framework.

using MbUnit.Framework;
Figure 2 – Using Directive

Next, you need to add a MbUnit attribute [TestFixture] to the class.

[TestFixture]
public class tests
Figure 3 – MbUnit Attribute

You can now start writing your test case.

[Test]
public void AddTest()
{
   int valueA = 11;
   int expectedValue = 33;
   int actualValue = 0;

   actualValue = Methods.ThreeTimesMe(valueA);

   Assert.IsTrue(expectedValue == actualValue, "Failed: The method did not return the correct value");
}
Figure 4 – Test Case

As you can see from the code, I have created a public method returning nothing. I have applied the custom attribute of [Test] and wrote code to access a method ThreeTimesMe. A assert is used to check that the value returned from the method is the same as the expected value, if not it displays the message given as a parameter. Some of you might recognize this style of coding from NUnit. MbUnit is fully compatible with all NUnit test suites however includes additional testing capabilities so with just a change of the references and using directives a project can be migrated across.

Myth-buster: You can use TDD for web applications and web services.

If you try and compile the test project it fails because it cannot find the method ThreeTimesMe. To fix this, create a new project with a method which returns 0 as we just want it to compile at the moment.

public class Methods
{
   public static int ThreeTimesMe(int iValue)
      {
         return 0;
      }
}
Figure 5 – Failing Application Code

If you load the GUI for MbUnit (MbUnit.Gui.exe), load the Test assembly and click run the test will be executed, fail and turn red.
MbUnit Red
Figure 6 – MbUnit Dialog with failing tests

Now we are required to fix the failing test. Within the method ThreeTimesMe, make the code return three times the value given as a parameter. Compile and execute the tests again and everything should turn green.

public class Methods
{
   public static int ThreeTimesMe(int iValue)
      {
         return iValue * 3;
      }
}
Figure 7 – Passing Application Code

MbUnit Green
Figure 8 – MbUnit Dialog with all tests passing

Myth-buster: You can use TDD on legacy code - every time a new feature or fix is required, follow TDD, you can then build an effective test suite around commonly changed code.

Summary

You should now have an understanding of TDD and how you can start creating a test suite. I recommend you download MbUnit and try some code and find out how much better it is with TDD. This is just the tip of the ice-berg when it comes to MbUnit’s features and a user has the power to use Fit style testing, data driven testing and it’s compatible with various object mock frameworks plus a whole lot more.

Important points to take away:
   + Write test code, write code to make test compile, run test, fix failing test.
   + Simplicity is the key to good TDD.
   + Test class must be public, and have the attribute [TestFixture]. Test methods must also be public with the attribute [Test].
   + Applications, such as TestDriven.NET, can provide integration of unit testing into visual studio.
   + Assert class contains the core methods such as IsTrue, AreEqual and Between.

Resources
About Ben
Ben Hall is a UK C# developer/tester who enjoys all aspects of the development lifecycle and technology in general. During the day Ben works for Red Gate Software as a Test Engineer. At night, Ben is a MbUnit Core Commit Member, helping out on the project whenever possible. Ben has also gained his Microsoft Certified Technology Specialist (MCTS) in Web Applications and is a Microsoft Certified Professional (MCP) in Administrating Windows XP.

My blog can be found at : http://blog.benhall.me.uk
Copyright © 2006-2009 NxtGenUG - Powered by ASP.NET 3.5