Background: I wrote this article around 5 or so years ago when I first starting MbUnit, its very, very old now but I keep it around to remind me how confusing a new framework can be to a new comer.
NUnit has done so much for unit testing in .NET and its served me well, I have however recently found the need to extend my Unit tests beyond what NUnit can currently do and MbUnit seemed to do the job. So here is my exploration into MbUnit.
To kick off first download MbUnit, its part of the TestDriven.Net package (which includes a version NUnit). The wicki has some great articles here and here to get you started. As usual in my articles its VB.NET.
The code I will test with is very simple,four methods, to add, subtract, multiply and concat.
Public Class Code
Public Function Add(ByVal intNum1 As Int16, ByVal intNum2 As Int16) As Int16 Return intNum1 + intNum2
End Function Public Function Subtract(ByVal intNum1 As Int16, ByVal intNum2 As Int16) As Int16 Return intNum1 - intNum2
End Function Public Function Mutliply(ByVal intNum1 As Int16, ByVal intNum2 As Int16) As Int16 Return intNum1 * intNum2
End Function
Public Function Concat(ByVal strNum1 As String, ByVal strNum2 As String) As String Return strNum1 & strNum2 End Function
End
Class
The NUnit code that will test this is also very simple.
Imports
NUnit.FrameworkImports CodeToTest
<TestFixture()>
Public Class MyTest Dim obj As Code
<SetUp()>
Public Sub Setup() obj = New Code
End Sub <Test()> Public Sub Add_Test()
Assert.AreEqual(2, obj.Add(1, 1))
End Sub <Test()> Public Sub Subtract_Test()
Assert.AreEqual(1, obj.Subtract(2, 1))
End Sub <Test()> Public Sub Multiply_Test()
Assert.AreEqual(1, obj.Mutliply(1, 1))
End Sub <Test()> Public Sub Concat_Test()
Assert.AreEqual("aa", obj.Concat("a", "a"))
End Sub
End
Class
I included a silly way of creating the object in the Setup fixture just so I could test it, not the best but it allows me to test it. To then run using MbUnit I change the imports to read.
Imports
MbUnit.Framework
Imports
MbUnit.Core.Framework
Load these up into the MbUnit GUI, first thing that catches you is the tree structure. Some interesting attributes for author and the ability to categorise the importances on the tests, very cool. Click run and all is green, very pleasing. So what about those categories (importances)? A search through the wiki and google turns up nothing, any clues on setting those up anyone?
Next up was to try the tests from VS, loving the way you can run the tests as a whole or one by one, debug them, and get a nice HTML summary. Not sure why a HTML summary is not available if all the tests are run at once but I can live with that. So onto trying the data fixture.
I based it on the example Johnthan has. I ended up with this.
<DataFixture(), XmlDataProvider("c:\data.xml", "DataFixture/Data")> Public Class MyTest
Dim obj As Code <SetUp()> Public Sub Setup() obj = New Code
End Sub <ForEachTest("//Item")> Public Sub Add_Test(ByVal node As XmlNode) Assert.AreEqual(CType(node.Attributes("result").InnerText, Int16), obj.Add(CType(node.Attributes("item1").InnerText,Int16), CType(node.Attributes("item2").InnerText, Int16)))
End Sub
End Class
The XML file was
<
DataFixture>
<Data>
<Item item1="10" item2="10" result="20" />
<Item item1="20" item2="10" result="30" />
<Item item1="30" item2="10" result="40" />
<Item item1="40" item2="10" result="50" />
<Item item1="50" item2="10" result="60" /> </Data>
</
DataFixture>
A few things to note that I came across.
I had to make sure the XML inputs were typed otherwise it was trying to match object to Int16. Also if non of the tests show up in the MbUnit GUI then check your XPath expression, for a while mine was missing turns out it was a typo in the data input XML file, make sure you double check it.
While the <Test()> attribute is picked up and compiles, MbUnit does not test for it when a DataFixture is being used. Not sure on what the recommended approach is but I ended up moving these to another assembly. So now onto NAnt.
First off check how to add MbUnit to NAnt, the wicki has the info. I end up with the following build file.
<project default="Debug">
<target name="Debug">
<solution solutionfile="CodeToTest.sln" configuration="Debug" />
<mbunit>
<fileset>
<includes name="bin/CodeToTest.dll" />
</fileset>
</mbunit>
</target>
</project>
Only I then get the following error.
The <mbunit> type does not support the nested build element "fileset".
At this point I come to a dead halt, pleased I got this far at least.