navigation
 Monday, April 07, 2008

My name is Angelo Martinez and I am new here at Falafel Software.  I have known Lino Tadros for a long time and I can assure you that this is a great place to be!  My hope is to start blogging on new ideas/tools/technologies that I discover along the way.  With that in mind...

I had a requirement to build a generic MRU (Most Recently Used) class based on a Key/Value pair.  The idea seemed simple enough:

1)      instantiate the class with a maximum capacity

2)      as items get added, they are placed at the front of the list

3)      when the capacity is reached, older items begin to drop off the list

4)      if an item already on the list is referenced, it is brought to the front

Among other things, the class contains methods such as Add(), Find() and a Count property that reports the number of items currently in the list.  It would be nice to be able to build a set of unit tests to verify the functionality of the new class.  For this, Visual Studio 2008 comes to the rescue!

With your solution opened in the IDE, select Test->New Test.  Then, select the Unit Test Wizard and click OK.  You then provide a name for your test such as “TestMRU”.  This creates a new project called “TestMRU” and adds it to the solution.  The wizard then gathers all the classes defined within the entire solution and categorizes them by project name and then by namespaces within each project.

Now, scroll down until you find the class you’d like to test, e.g. “MRU”.  A list of methods and properties contained within this class is then presented for testing.  Select the checkboxes of the functionality you’d like to test and click OK.  For this example, I’ll select the Count property.

Visual Studio creates a new source file “MRUTest.cs” and a new MRUTest class with testing functions as follows:

public void CountTestHelper<K, V>()

{

    int maxCapacity = 3;

    MRU<string, string> target = new MRU<string, string>(maxCapacity);

    target.Add("Bary", "Nusz");

    target.Add("Lino", "Tadros");

    target.Add("John", "Waters");

    target.Add("Noel", "Rice");

 

    int actual = target.Count;

    Assert.IsTrue(actual == maxCapacity);

}

 

[TestMethod()]

public void CountTest()

{

    CountTestHelper<GenericParameterHelper, GenericParameterHelper>();

}

 

Note the “TestMethod()” attribute that decorates the new CountTest function.  In my case, the MRU class is a generic class, so Visual Studio inserts GenericParameterHelper objects as placeholders for the Key/Value types.

 

Let’s now test the ability of the MRU class to maintain a max capacity of 3 items.  We’ll instantiate the MRU class as an object called “target” and specify the maximum capacity of 3 with a constructor parameter.  For simplicity, we’ll specify that the Key/Value pair consists of 2 strings representing first and last name pairs.  The instantiation now looks like this:

 

    int maxCapacity = 3;

    MRU<string, string> target = new MRU<string, string>(maxCapacity);

 

Now, add a bunch of items into the MRU…

 

    target.Add("Bary", "Nusz");

    target.Add("Lino", "Tadros");

    target.Add("John", "Waters");

    target.Add("Noel", "Rice");

 

Note that since the maxCapacity was set to 3, the addition of “Noel” should bump “Bary” off the MRU list.  Thus, we are expecting the Count property to return a 3 despite having added 4 items to the list.

 

    int actual = target.Count;

    Assert.IsTrue(actual == maxCapacity);

 

Here, the Assert class is used to verify the conditions of the unit test.  As long as the Count property returns a 3, then we have passed our first simple unit test!  The final test function is as follows:

 

public void CountTestHelper<K, V>()

{

    int maxCapacity = 3;

    MRU<string, string> target = new MRU<string, string>(maxCapacity);

    target.Add("Bary", "Nusz");

    target.Add("Lino", "Tadros");

    target.Add("John", "Waters");

    target.Add("Noel", "Rice");

 

    int actual = target.Count;

    Assert.IsTrue(actual == maxCapacity);

}

 

For some great references on Unit Testing in general I recommend the following: 

 

Steve Trefethen's blog (Falafel Software's in-house guru on testing)

 

Charlie Calvert's comments on testing

 

 

 

Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, i, strike, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview