Row based testing in MbUnit (i.e. RowTest)

Jonathan de Halleux, aka Peli, never ceases to impress me with his innovations within MbUnit. In case you're not familiar with MbUnit, it's a unit testing framework similar to NUnit.

The difference is that while NUnit seems to have stagnated, Jonathan is constantly innovating new features, test fixtures, etc... for a complete unit testing solution. In fact, he's even made it so that you can run your NUnit tests within MbUnit without a recompile.

His latest feature is not necessarily a mind blower, but it's definitely will save me a lot of time writing the same type of code over and over for testing a range of values. I'll just show you a code snippet and you can figure out what it's doing for you.

 

[TestFixture]
public class DivisionFixture
{
[RowTest]
[Row(1000,10,100.0000)]
[Row(-1000,10,-100.0000)]
[Row(1000,7,142.85715)]
[Row(1000,0.00001,100000000)]
[Row(4195835,3145729,1.3338196)]
public void DivTest(double num, double den, double res)
{
Assert.AreEqual(res, num / den, 0.00001 );
}
}

 

And if you're anal like me and wondering why I chose "num" instead of "numerator" etc... Purely for blog formatting reasons. ;)

UPDATE: Jonathan points out that negative assertions are also supported. Here's an illustrative code snippet. I can't wait to try this out.

 

[RowTest] 
[Row(1000,10,100.0000)]
...
[Row(1,0,0, ExpectedException =
typeof(ArithmeticException))]
public void DivTest(double num, double den, double res)
{...}
Technorati tags: ,

What others have said

Requesting Gravatar... Jonathan de Halleux Oct 20, 2004 10:00 AM
# re: Row based testing in MbUnit (i.e. RowFixture)
Thanks!

In fact, this new feature is not mind blowing but it is handy and usefull in any situation where you end up with a table description of the tests.

Btw, it would be nice to update your sample to show that you can also do "negative" testing by specifying the expected exception in the Row constructor...

[RowTest]
[Row(1000,10,100.0000)]
...
[Row(1,0,0, ExpectedException = typeof(ArithmeticException))]
public void DivTest(double num, double den, double res)
{...}
Requesting Gravatar... TestDriven.NET by Jamie Cansdale Dec 17, 2007 1:41 AM
# TestDriven.Net 2.11: Parameterized NUnit Tests
TestDriven.Net has always supported parameterized test methods when used with the MbUnit testing framework

What do you have to say?

(will show your gravatar)
Please add 8 and 7 and type the answer here: