All posts tagged 'bunit'

My Favorite MbUnit Assertions

Published 12/20/2008 by PhilJapikse in testing

This post is the second of two parts (the first on MbUnit Attributes is here).  There are many new, renamed, improved, combined, etc attributes in MbUnit v3.  Jeff Brown et al have done a fantastic job with the newest versions of Gallio and MbUnit (download here).  (These are not in any particular order.)


Positive and Negative Tests

A nice, consistent update in version 3 is just about every Assertion has a correlated negative Assertion.  For example, for AreEqual there is also AreNotEqual.  Most of the names are obvious like this, except for a few logical differences (Assert.IsTrue and Assert.IsFalse).


I mentioned this transition from Attribute based exception handling to Assertion base in my last post, but I think it is worth repeating.  This allows for more granular control of the test than simply decorating it with the ExceptedException attribute.

There are two flavors, Assert.Throws and Assert.DoesNotThrow.  (I’ve never seen a use for the latter, but it’s there).

public void Should_Test_For_Exception_Thrown()
   var ex = Assert.Throws<ArgumentNullException>(
       () => { throw new ArgumentNullException(exMessage); });

Comparison Operators – Single Values

There is a full suite of comparer assertions (see table below), and they essentially work the same way (see IntelliSense for more detailed information):

Are[Not]EqualCan add custom comparer
IsTrue, IsFalse 
GreaterThan, LessThan
GreaterThanOrEqualTo, LessThanOrEqualTo
Can add custom comparer
[Not]LikeTests string contains full or partial match of RegEx
FullMatchTests for exact match with RegEx
StartWith, EndsWithCase Sensitive comparison of strings
[Not]BetweenCompares actual with Min/Max,
Can add custom comparer
Are[Not]ApproximatelyEqualTests for equality with a delta

Comparison Operators – Sets

Is[Not]EmptyTests for Empty/NotEmpty IEnumerable
Contains, DoesNotContainTests for element in IEnumerable
Tests for substring in string
ContainsKey, DoesNotContainKeyTests for existence of Key in a Dictionary
AreElements[Not]EqualTests elements for equality (order dependent)
Can add a custom comparer
AreElementsEqualIgnoringOrderTests elements for equality (order agnostic)
Can add a custom comparer

Specialty Operators


Tests that object can be assigned a variable of expectedType
Is[Not]InstanceOfTypeTests that an object is an instance of the expected type
Note: inheritance chains will cause a failure

So, there are the Assertions for MbUnit v3. Great stuff from the Gallio/MbUnit team.

Happy Coding!

.NET Musings

Wandering thoughts of a developer, architect, speaker, and trainer

Managed Windows Shared Hosting by OrcsWeb