Productive Unit Testing with Specialized Assertion Classes in MbUnit

code, tdd 0 comments suggest edit

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).

Found a typo or error? Suggest an edit! If accepted, your contribution is listed automatically here.

Comments

avatar

8 responses

  1. Avatar for Matt
    Matt May 10th, 2007

    NUnit.Framework.CollectionAssert
    NUnit.Framework.FileAssert
    NUnit.Framework.StringAssert
    Just noticed those the other day.

  2. Avatar for Karthik
    Karthik May 10th, 2007

    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?

  3. Avatar for Community Blogs
    Community Blogs May 10th, 2007

    Phil Haack has two great posts. A new fixture for embeding resources in tests , Phil has contributed

  4. Avatar for Chris
    Chris May 10th, 2007

    great post

  5. Avatar for JIRA: MbUnit
    JIRA: MbUnit May 14th, 2007

    As Phil notes ( http://haacked.com/archive/... ), this section of the documentation ( http://www.mertner.com/conf... ) is a little sparse. null

  6. Avatar for malloc();
    malloc(); May 26th, 2007

    MbUnit 2.4

  7. Avatar for Kevin Isom's Blog
    Kevin Isom's Blog May 28th, 2007

    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 ...

  8. Avatar for Kevin Isom
    Kevin Isom July 3rd, 2007

    Unit Testing with MbUnit