Andrew Stopford's Weblog

poobah

Sponsors

News

Articles

Family

Old Blogs

A night with MbUnit

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

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

Comments

TrackBack said:

# November 13, 2004 2:59 PM

TrackBack said:

# June 3, 2005 6:06 AM

Vasilios said:

Nice!

# June 4, 2007 11:59 PM

Antonis said:

Cool.

# June 6, 2007 11:41 AM

Charalambos said:

interesting

# June 7, 2007 5:58 PM

Lambros said:

Cool.

# June 9, 2007 4:11 AM

Thanasis said:

interesting

# June 9, 2007 4:51 PM

Aiolos said:

Nice

# June 10, 2007 11:01 PM

Stavros said:

Cool...

# June 11, 2007 6:24 PM

Neophytos said:

Sorry :(

# June 11, 2007 7:54 PM

Alexander said:

Cool...

# June 13, 2007 6:26 AM

gfbvbxv said:

people are stranger

# September 17, 2007 10:57 PM

Neo said:

# October 28, 2007 3:23 AM

Neo said:

# October 28, 2007 3:23 AM

Music_Mp3_albundall said:

Hello to all :) I can’t understand how to add your site in my rss reader. Help me, please

# February 5, 2009 3:26 AM

Shawn Scott said:

This makes me remember this thing my brother pretty much always said...

But its surely not appropriate right this moment...

# November 11, 2009 3:10 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)