Andrew Stopford's Weblog

poobah

Sponsors

News

Articles

Family

Old Blogs

MbUnit RowTest

"You said no RowTest post!"

I know I did but this v3 feature I'd like to talk about, Row value headers, let's look at this example.

   1:      [Test]
   2:      [Row(1, 3, "test")]
   3:      [Row(2, 2, "test")]
   4:      public void Example(int sum1, int sum2, string value)
   5:      {
   6:          Assert.AreEqual(4, sum1 + sum2);
   7:      }

Here we have a row test with sample values (note you can mix types in this way) which seeds the test, so far so not unusual, let's try this however.

   1:      [Test]  
   2:      [Row(1, 3, "test")]
   3:      [Row(2, 2, "test")]
   4:      public void Example(string value, int sum1, int sum2)
   5:      {
   6:          Assert.AreEqual(4, sum1 + sum2);
   7:      }

This test would fail as the Row seeds the test parameters in the order they are set in the Row, so now the string values are seeded with int values. While we could just set the parameters in the right order we can also do the following.

   1:      [Test]
   2:      [Header("sum1", "sum2", "value")]
   3:      [Row(1, 3, "test")]
   4:      [Row(2, 2, "test")]
   5:      public void Example(string value, int sum1, int sum2)
   6:      {
   7:          Assert.AreEqual(4, sum1 + sum2);
   8:      }

Same test parameter order as before but the test passes. The reason is the Header attribute, this states to MbUnit how each row test value relates to the test parameters so rather than change our test method we can instead fine tune the test data instead.

Posted: Aug 25 2008, 11:02 PM by andrewstopford | with 4 comment(s)
Filed under: ,

Comments

Aaron Jensen said:

Out of curiosity, what's the use case for this? It smells like bloat to me...

# September 3, 2008 2:20 AM

Jeff Brown said:

Headers are more interesting when using external data files, such as with CSV (if the header does not appear within the file).  This can make the overall test more resilient.

That said, I probably wouldn't use [Header] together with [Row].

The MbUnit data-binding features are highly composable and can be reused in a wide variety of contexts so fewer special-purpose features are required.

# September 3, 2008 2:21 PM

Ramanjaneya sharma said:

Dear Aaron,

The main use of RowTest is that we can test multiple sets of data with a single test assertion..This is the power of RowTest in Mbunit..Great.

# February 18, 2009 4:20 AM