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\.
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.
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
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
Click here to download the project
MbUnit Website
|