mbUnit

I would like to take a moment to espouse the wonders of mbUnit. It is to NUnit as C++ is to C, as Perl is to Bourne shell, as Milk Duds are to milk. That is, the former supports the same basic principles as the latter, but gives you more rope with which to hang yourself. And really, who can resist that?

It supports the same "idiomatic" syntax as NUnit, which is a good start, since that's really the only way to do unit tests in C#. And you can do the usual TestFixture/Test stuff, which works, but is kind of clunky and unwieldy.

But the mbUnit folks have taken it a step further. The old way always involved writing cumbersome loops over sets of input and trying a particular call/assertion for each of them. Boring, repetitive, not ultimately traceable. But by allowing tests to have parameters and creating a configurable binding phase, all that goes away. It works like this:


[RowTest]
[Row(1,2)]
[Row(3,4)]
void TestSomething(int a, int b) { ...

That's just the binding, straight up, as a set of rows. Simple but useful, and far cleaner than the alternative. In the testrunner, you'll see that as two tests. This really is the right way to do it, I think, since you can easily trace failed tests to a particular set of inputs. But it really gets cool with CombinatorialTest. I find myself most often using the trivial case:


[Factory(typeof(string)]
public IEnumerable InputFiles()
{
return System.IO.Directory.GetFiles("inputDir");
}

[CombinatorialTest]
public MyTest( [UsingFactories("InputFiles")] string filename )
{ … }
Now that's downright great. And there are all kinds of other useful generators, like UsingValues and UsingLinear. With multiple parameters, Combinatorial test evidently combines them in some significant way, but I honestly don't understand the whole thing. I'm just willing to let it work.

My only complaint? I haven't yet found a good project template for visual studio. I'm sure I could make one, but my laziness is still fighting itself on that one. I know, it's supposed to be easier in VS2005, but my wounds have yet to heal from the last attempt.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s