Productive Unit Testing with Specialized Assertion Classes in MbUnit

If you’ve worked with unit test frameworks like NUnit or MbUnit for a while, you are probably all too familiar with the set of assertion methods that come built into these frameworks. For example:

Assert.AreEqual(expected, actual);
Assert.Between(actual, left, right);
Assert.Greater(value1, value2);
Assert.IsAssignableFrom(expectedType, actualType);
// and so on...

While the list of methods on the Assert class is impressive, it leaves much to be desired. For example, I needed to assert that a string value was a member of an array. Here’s the test I wrote.

[Test]
public void CanFindRole()
{
  string[] roles = Roles.GetRolesForUser("pikachu");
  bool found = false;
  foreach (string role in roles)
  {
    if (role == "Pokemon")
      found = true;
  }
  Assert.IsTrue(found);
}

Ok, so that’s not all that terrible (and yes, I could write my own array contains method, but bear with me). But still, if only there was a better way to do this.

Well I obviously wouldn’t be writing about this if there wasn’t. It turns out that MbUnit has a rich collection of specialized assertion classes that help handle the grudge work of writing unit tests. These classes aren’t as well known as the straightforward Assert class.

As an example, here is the previous test rewritten using the CollectionAssert class.

[Test]
public void CanFindRole()
{
  string[] roles = Roles.GetRolesForUser("pikachu");
  CollectionAssert.Contains(roles, "pokemon");
}

How much cleaner is that? CollectionAssert has many useful assertion methods. Here’s a small sampling.

CollectionAssert.AllItemsAreNotNull(collection);
CollectionAssert.DoesNotContain(collection, actual);
CollectionAssert.IsSubsetOf(subset, superset);

Here is a list of some of the other useful specialized assert classes.

  • CompilerAssert - Allows you to compile source code
  • ArrayAssert - Methods to compare two arrays
  • ControlAssert - Tons of methods for comparing Windows controls
  • DataAssert - Methods for comparing data sets and the like
  • FileAssert - Compare files and assert existence
  • GenericAssert - Compare generic collections
  • ReflectionAssert - Lots of methods for using reflection to compare types, etc...
  • SecurityAssert - Assert security properties such as whether the user is authenticated
  • StringAssert - String specific assertions
  • SerialAssert - Assertions for serialization
  • WebAssert - Assertionns for Web Controls
  • XmlAssert - XML assertions

Unfortunately, the MbUnit wiki is sparse on documentation for these classes (volunteers are always welcome to flesh out the docs!). But the methods are very well named and using Intellisense, it is quite easy to figure out what each method of these classes does.

Using these specialized assertion classes can dramatically cut down the amount of boilerplate test code you write to test your methods.

Keep in mind, that if you need the option to port your tests to NUnit in the future (not sure why you’d want to once you have a taste of MbUnit) you are better off sticking with the Assert class, as it has parity with the NUnit implementation. These specialized assertion classes are specific to MbUnit (and one good reason to choose MbUnit for your unit testing needs).

Technorati tags: ,

What others have said

Requesting Gravatar... Matt May 10, 2007 12:01 PM
# re: Productive Unit Testing with Specialized Assertion Classes in MbUnit
NUnit.Framework.CollectionAssert
NUnit.Framework.FileAssert
NUnit.Framework.StringAssert

Just noticed those the other day.
Requesting Gravatar... Karthik May 10, 2007 1:19 PM
# re: Productive Unit Testing with Specialized Assertion Classes in MbUnit
Very cool. All our unit tests are in MSUnit and use the simple assertions. Would be nice to have something like this. Anything like CollectionAssert in the MSUnit classes?
Requesting Gravatar... Community Blogs May 10, 2007 2:35 PM
# MbUnit tips #2
Phil Haack has two great posts. A new fixture for embeding resources in tests , Phil has contributed
Requesting Gravatar... Chris May 11, 2007 7:25 AM
# re: Productive Unit Testing with Specialized Assertion Classes in MbUnit
great post
Requesting Gravatar... JIRA: MbUnit May 15, 2007 2:30 AM
# [MBUNIT-114] Documentation of Specialized Assertions
As Phil notes ( http://haacked.com/archive/2007/05/10/productive-unit-testing-with-specialized-assertion-classes-in-mbunit.aspx ), this section of the documentation ( http://www.mertner.com/confluence/display/MbUnit/Assertions ) is a little sparse. null
Requesting Gravatar... malloc(); May 26, 2007 7:13 PM
# MbUnit 2.4
MbUnit 2.4
Requesting Gravatar... Kevin Isom's Blog May 28, 2007 1:02 PM
# Unit Testing with MbUnit
I've only ever dabbled in unit testing and test driven development in the past. But in my current project I really had to make use of unit tests to make sure that everything worked and would continue to work while I develop the code. So I decided to ...
Requesting Gravatar... Kevin Isom Jul 03, 2007 5:52 PM
#
Unit Testing with MbUnit

What do you have to say?

(will show your gravatar)
Please add 1 and 5 and type the answer here: