Friday, May 25, 2007

The Value of Writing Unit Tests

Do unit tests make sense from a business perspective? Jean-Paul S. Boodhoo's take here. BTW, if you're doing Agile development and don't read CodeBetter, now is a great time to start.

Sunday, May 20, 2007

Code Camp Slides and Source

Here's a link to the slides and sample code I used for my talk at Code Camp. And here's a link to more information about Selenium: http://www.openqa.org/selenium/

Thanks for attending!

Wednesday, May 02, 2007

Test Utility Classes

When you start to write a substantial number of unit tests, you may discover that your tests have quite a bit in common. For instance, if you have a CreditCard class, you may have a block of code like this in each of your tests:

Assert.AreEqual( expectedCard.Number,     actualCard.Number );
Assert.AreEqual( expectedCard.FirstName, actualCard.FirstName );
Assert.AreEqual( expectedCard.MiddleName, actualCard.MiddleName );
Assert.AreEqual( expectedCard.LastName, actualCard.LastName );
Assert.AreEqual( expectedCard.ExpMonth, actualCard.ExpMonth );
Assert.AreEqual( expectedCard.ExpYear, actualCard.ExpYear );

Of course, it won't be exactly the same in each test, and that just amplifies the problem. What happens when you want to add a variable to CreditCard? Copy, paste, paste, paste... and inevitably, miss one. One way to manage this complexity is to implement a custom Equals() method in your class. With that in place, you can collapse the previous code into this:

Assert.AreEqual( expectedCard, actualCard );

Sure enough, this reduces the amount of code in your unit tests and makes them less error-prone. However, if the equality test fails, you will know that one of the credit card's values is incorrect, but you won't know which. You can probably figure that out with a little debugging, but I prefer tests to be more explicit when they fail. So to deal with this, I'll typically create a test utility class for each class that looks something like this:

public class CreditCardUtil
{
public static void AssertAreEqual( CreditCard expected, CreditCard actual )
{
Assert.AreEqual( expected.Number, actual.Number );
Assert.AreEqual( expected.FirstName, actual.FirstName );
Assert.AreEqual( expected.MiddleName, actual.MiddleName );
Assert.AreEqual( expected.LastName, actual.LastName );
Assert.AreEqual( expected.ExpMonth, actual.ExpMonth );
Assert.AreEqual( expected.ExpYear, actual.ExpYear );
}
}

This way, the unit test code is just as simple, but you get meaningful error messages when the test fails. This pattern is useful when you're working on a TDD project with a group, because each person on the team will know where to look if they need a particular bit of functionality. Looking for a method that will compare two Invoices? Try InvoiceUtil.AssertAreEqual. Not there? Write it.

You may want to extend these utility classes to do other useful things, such as:
  • Instance an object that is initialized with unique properties (useful for comparison)
  • Perform CRUD database operations
  • Setup all of the object's dependencies
  • Define useful constants, file paths, etc. specific to the object
This may seem obvious, but I've seen a number of people (myself included) treat test code as a second class citizen in their projects. The beginner's approach is typically to:
  1. Find a similar test
  2. Copy it
  3. Change it a little
  4. Repeat
This is fine when the code is well-factored, but in the beginning, it usually isn't. Take the time to tighten up your test code before it's too late. That way, when your manager wants to add CVV numbers to credit card processing, you'll be in a much more responsive position. Remember, test code is production code.

Tuesday, May 01, 2007

New Rhino Mocks Record/Replay Syntax

Oren Eini (aka Ayende Rahien) just released the latest version of Rhino Mocks, v3.0.5. This release includes a great new syntax for record and replay that will make your tests much easier to follow. Check out the syntactic sweetness here, be impressed, then download it here.

Thursday, April 26, 2007

Unit Testing Private Methods

When writing unit tests, I frequently find myself wanting to test private methods. I used to think there were only three approaches to solving this problem.

  1. Test the private method indirectly through some other, public method that invokes it.
    • Pros: Keeps your class interface clean and doesn't compromise your OO design
    • Cons: You may not be able to test all of the cases you'd like through the public method, and your test results may be affected by the code in the public method.

  2. Make the private method protected and create a derived class in your test project that exposes the method publicly.
    • Pros: Allows you test the method directly
    • Cons: Compromises the encapsulation of your initial design, requires writing more test classes.

  3. Refactor the private method into some other class that exposes it publicly.
    • Pros: Doesn't require more test code, you may actually end up with more well-factored design
    • Cons: You probably don't want to make this code public, isn't that why it was private in the first place?
So, given these options, I went with #2. I've written a lot of tests like this, but it's a chore to implement these wrapper classes in your test code, and I always thought there must be a better way. Fortunately, if you're programming in .Net, there is:
  1. Use reflection to invoke the private method directly
    • Pros: Totally rocks.
    • Cons: Invoking the private method looks a little funky
Kudos to Michael Kelly for showing me this approach. Now that I've seen this in action, its the only way I deal with this case. Here's a sample implementation to get you started:

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

public class TestReflector
{
public static T InvokeNonPublicMethod<T>( string methodName, object obj,
params object[] parameters )
{
MethodInfo methodInfo =
obj.GetType().GetMethod( methodName, BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.Static);
if ( methodInfo == null )
{
throw new ApplicationException( "Private method not found for: '" +
methodName + "'." );
}
return (T)methodInfo.Invoke( obj, parameters );
}
}


With that in place, it's very simple to use:

MyClass myClass = new MyClass();
string string1 = "abcdef";
string string2 = "123456";
string result = TestReflector.InvokeNonPublicMethod<string>(
"MyPrivateMethod", myClass, string1, string2 );


This code will support any non-public method with any number of parameters and any return type, except void. If you do need void support, its a simple adaptation of this code. Hope this helps!

Wednesday, April 25, 2007

Unit Testing With Mock Objects - Slides and Sample Code

Thanks to everyone who attended my mock objects talk at Innotech today, I hope it wasn't too obvious that it was the first time I've presented at a conference ;-) As promised, I've posted my slides and source code here. Sorry about the ads, but Blogger doesn't support file uploads and this site was the best I could find on short notice. If you're here for the links, here you go:

If you have any additional resources you'd like to share, let me know and I'll post them here.

Thanks again,
John

Sunday, January 07, 2007

Online Mii Maker

Have some time to waste? Try this!