I finally had a chance to sit down and play with MbUnit last week. What is MbUnit you say? It’s a unit testing tool that works very much like NUnit. NUnit, in case you’ve been living in a cave for the last year or so, is the ’standard’ unit testing software for .NET framework applications. It’s become a standard due to a couple factors. First, it was one of the earliest unit testing apps created during the .Net 1.0 beta. Second reason — exposure. It receives continual free publicity from book authors, websites and developer magazines. Most authors use it as the de facto unit testing tool therefore it gets lots and lots of free press.
Why do authors pick it? Because it was available early on and because it is FREE. No need to worry if their readers have another unit test application. They can download NUnit if needed.
There a number of other unit testing tools — some commercial versions, other open source ones, each in various stages of development.. Plus, don’t forget that Microsoft is including unit testing in Visual Studio Team System although the licensing costs are not cheap (~$5000 to $10,000). Due the the attention that NUnit receives these programs are often ignored.
Why am I looking at MbUnit then? I’ve heard countless good things about it, that’s one explanation. I’ve also heard that it simplify testing because it has test fixtures that automate complicated test patterns.
Another incentive has to do with NUnit itself. It is open source and developed by people who have other jobs. Which means that has suffered in the past from neglect and has gone through months of stagnation and non development. It seems to be in one such slump currently — probably due to the fact the Microsoft hired James W. Newkirk, the former NUnit Developer Lead, for their Platform Architecture Guidance team. Which means that it is not being improved very much lately. There are 5-6 other people working on NUnit but I haven’t seen much progress on new features.
MbUnit on the other hand seems to be rocketing forward with tons of new features being adding every month. It was designed to be extendable and flexible and that means that it is easy to add new functionality to the tests without having to constantly compile the core runtime.
Note: I just found out the Jonathon de Halleux, originator of MbUnit, is working at Microsoft now. Looks like Microsoft is scooping up all the unit testing experts. Therefore the development of MbUnit falls into the hands of Jamie Cansdale, developer of TestDriven.Net .
The rest of this post assumes you have worked with NUnit and are familiar with its object model. I’m writing about the features that are available in MbUnit and not part of NUnit.
Basic Features
The MbUnit GUI has several features that I find useful. The most useful is the report generator. It can create a report in several formats, XML, Text, Dox and Html. I’m biased toward the HTML one, it look pretty good. If you are using TestDriven.Net and run your test within Visual Studio TestDriven.NET will include a link to the HTML report after running the tests.
Assert Class
The main workhorse in unit testing is the Assert class and it’s methods.
Here a list of the extra methods that are in MbUnit
- In - Assert that an object is in a list or dictionary
- NotIn - Assert that an object is not in a list or dictionary
- AreNotSame - Asset the two references are not referencing the same objec
- AreValueEqual - not sure what this one does yet.
- GreaterEqualThan
- GreaterThan
- LowerEqualThan
- LowerThan
- These four methods test whether a value is above or below another value
- IncrementAssertCount - not sure what this does yet
- ResetAssertCount
- NotBetween - Assert a value is outside a min and max value
- Between - Assert a value is between two other values
- Warning
Other Classes
There is a rich set of classes that can greatly simplify your tests. Here is the list
- ArrayAssert
- ConsoleTester
- ControlAssert
- CountdownTimer
- DataAssert
- FileAssert
- PerfCounterInfo
- ReflectionAssert
- SecurityAssert
- SerialAssert
- StringAssert
- XmlAssert
New Atttributes
Some delightful new attributes to decorate your test code.
- AssemblyCleanup
- AssemblyDependsOn
- Author
- ConditionalException
- Copy
- CopyToProvider
- CurrentFixture
- DataProvider
- DecoratorPatthern
- DependsOn
- Duration
- Factory
- Fill
- FixtureCategory
- FixtureDecorationPattern
- Importance
- IndexProvider
- Information
- IntIndesorProvider
- MultipleCulture
- NamespaceProvider
- NonTestPattern
- Pattern
- PerfCounter
- PostIt
- Provider
- ProviderFactory
- ProviderFixturrePatternDecorator
- Read
- Repeat
- TestFixturePattern
- TestFixtureSetup andTestFixtureTeardown
- TestSetup and TestTeardown
- TestPattern
- TestsOn
- TestSequence
- ThreadedRepeat
- Write
- XML
Other Types of Tests
I’m still looking through the object model, I know that I haven’t covered everything. One last item to stimulate your curiosity. RowTesting. Pass in rows of test data to your test method.
VB
<TestFixture()> _
Public Class DivisionFixture
<RowTest(), _
Row(1000, 10, 100.0), _
Row(- 1000, 10, - 100.0), _
Row(1000, 7, 142.85715), _
Row(1000, 1E-05, 100000000), _
Row(4195835, 3145729, 1.3338196)> _
Sub DivisionTest(numerator As Double, denominator As Double, expectedResult As Double)
Assert.AreEqual(expectedResult, numerator / denominator, 1E-05)
End Sub ‘DivisionTest
End Class
C#
[TestFixture]
public class DivisionFixture
{
[RowTest]
[Row(1000,10,100.0000)]
[Row(-1000,10,-100.0000)]
[Row(1000,7,142.85715)]
[Row(1000,0.00001,100000000)]
[Row(4195835,3145729,1.3338196)]
public void DivisionTest(double numerator, double denominator, double expectedResult)
{
Assert.AreEqual(expectedResult, numerator/ denominator, 0.00001 );
}
}
Where to find
MbUnit is now part of the TestDriven.Net distribution and not available as a separate download. That’s OK because you should be using Testdriven.Net anyway. Another superb free tool TestDriven.NET belongs in your toolbox. I use it every day.
Conclusion
I’m impressed with the amount of new functionality provided in the latest release of MbUnit. It has become my number one choice for unit testing. I will report more as I use it in some real projects.