Getting started with Test Driven Development
by Joseph on Apr 19th in C#, Euler Project, mbUnit, Test Driven Development
So I decided today that I would knock out two birds with one stone:
- Get started using Test Driven Development so that I can learn how to use it in an upcoming project
- Start earning “street cred” by solving the Euler Project puzzles.
I’ve been putting off TDD due to fear, consternation, and general trepidation related to trying something new. After all, the old way was working, right? Plus, how to choose between nUnit, mbUnit, MSTest, etc. In the end, it came down to just looking at the framework documentation. mbUnit had an easy way to compare the values inside a list
I went over to http://www.gallio.org/ and downloaded the installation package, and off I went. I thought about using TestDriven.Net, but decided to start off simple and work my way up.
My first project on Euler was simple. All I had to do was find the sum of the multiples of 3 & 5 below 1000.
At the risk of giving away the plot, I have included my code below:
public int GetSum()
{
ListNaturalNumbers = new List ();
for (double d = 1; d < 1000; d++)
{
if (d % 3 == 0 || d % 5 == 0)
{
NaturalNumbers.Add(d);
}
}
return Convert.ToInt32(NaturalNumbers.Sum());
}
Honestly, if that’s a huge plot spoiler, you need to be doing something a little differently…
I am proud to say I did this completely Test Driven. I started with a failing test (i.e. Assert.AreEqual(0,1)) and slowly it morphed into a passing test. Along the way, I found the Assert.AreElementsEqual(List1
Impressions thus far?
- mbUnit is startlingly easy to use. The support for List
(not provided in MSTest) was without a doubt a REQUIREMENT for the project I was doing. - TDD in general is nowhere near as scary as I first thought
- The speed for compiling and running tests in VS 2008 was a little dissappointing. Considering that most of my tests were simple arithmetic, I would have liked to see some snappier performance from the test framework. When I ran the finished product it completed in under 1 second. The test run was closer to 30 seconds.
I will continue to discuss my experiences, perhaps even provide a few tutorial examples, since there seems to be a dearth of them currently (one of my biggest reasons for being nervous about the whole thing).