Vadim's Weblog

Never stop learning.

MbUnit: Arrays with RowTest

Posted by Vadim on May 26, 2007

At my place of work some people were surprised that it’s possible to put array of values in Row attribute. I hope that some people can find this information useful.

[RowTest]
[Row(new string[] { "0", "4"}, new int[] {0, 4})]
public void CompareArrays(string[] strArr, int[] intArr)
{
   int len = strArray.Length;
   Assert.AreEqual(len, intArr.Length);
   for (int i = 0; i < len; i++)
      Assert.AreEqual(strArr[i], intArr[i].ToString());
}

I often use arrays to create an IEnumerable type like List<T>.

[RowTest]
[Row(new string[] { "str1", "str2" }, 2)]
public void ListTest(string[] strArray, int length)
{
   List<string> strList = new List<string>(strArray);
   Assert.AreEqual(length, strList.Count);
}

There’s one known problem with using arrays. You’ll get a compile error like this one “An attribute argument must be a constant expression, typeof expression or array creation expression” if an array is only parameter in Row attribute. Code below will not compile.

[RowTest]
[Row(new string[] { "str1", "str2" })]
public void NoComplie(string[] strArray)
{
}

I use this feature of MbUnit a lot. 

Empower yourself with MbUnit.

2 Responses to “MbUnit: Arrays with RowTest”

  1. noetic said

    For fixing problem with array only param I can propose to introduce fake parameter:

    [RowTest]
    [Row(new string[] { “str1″, “str2″ }, new object())]
    public void Complie(string[] strArray, object fake)
    {
    }

  2. Laura said

    I had the same error with array containing only one item.

Leave a Reply

Fill in your details below or click an icon to log in:

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

 
%d bloggers like this: