MbUnit RowTest

Recently I was asked how to separate multiple test asserts based on different data from the test itself to reduce code duplication and/or have better reporting of the multiple assertions in the test instead of just a single pass/fail, even if the test actually 10 different tests.

MbUnit contains a really nice feature called RowTest which allows you to have a single test, but with multiple different sets of data to execute a test against.  Each set of data is then executed separately as a unique test.

MbUnit RowTest Screenshot 

An example of the test method is this:

[RowTest]
[Row(1, 2, 3)]
[Row(0, 0, 1)]
[Row(-1, -2, -3)]
[Row(2, 2, 5, ExpectedException = typeof(MbUnit.Core.Exceptions.AssertionException))]
public void TestForAddMethod(int a, int b, int expected)
{
      int test = MyMethods.Add(a, b);
      Assert.AreEqual(test, expected);
}

This then creates four separate tests, with one expecting to fail.

When one test fails, it doesn’t stop all the other tests executing.  This is a great feature to improve your unit testing code.

Download the full code class, which includes the NUnit and MbUnit style, from:
http://blog.benhall.me.uk/code/mbunit/mbunitrowtest.cs.txt

 

Just one of the many great features within the MbUnit framework.

 

Technorati tags: , ,

3 thoughts on “MbUnit RowTest”

  1. Weird attribute names – suggests databases where none are involved. Better might be “ParameterisedTest” and “Parameters” instead of RowTest and Row

  2. Wouldn’t you think it is better to move such data into an xml file. Keeping test data separated from test logic? You can also enable sharing of data across tests

  3. Thanks for the comments. I am 99% sure that the test data can be stored in a separate XML file. I will find the code later and post a follow up….There are also different attributes which do a similar thing which I will try and write about soon. Keep an eye on the blog 😉 Ben

Leave a Reply

Your email address will not be published. Required fields are marked *