Automating MbUnit With NAnt

NAnt is one of the two most commonly used automated build tools for .NET projects, the other being MSBuild. Using it rather than Visual Studio directly means that you can

and all without a sniff of Visual Studio. NAnt is a free, open source project downloadable from http://nant.sourceforge.net. Its build scripts are written as XML documents and can be automated or run manually as appropriate. You can find the reference manual here.

The MbUnit Custom NAnt Task

MbUnit includes code for a custom <mbunit> NAnt task which makes the running of unit tests through MbUnit and subsequent report generation much more straightforward. To get this working, you’ll need to

  1. Copy the following DLLs from the MbUnit installation directory to the NAnt bin directory. If you’ve used other MbUnit DLLs in your test code, you’ll need to include those as well, but these four are the minimum.
  2. Include the <mbunit> task in one of your nAnt scripts. For example, the following script runs the tests in FizzBuzzTests.dll, creates a HTML report of the test run and saves it in the current directory.
    <project default="tests">
       <target name="tests">
          <mbunit
             report-types="Html"
             report-filename-format="myreport{0}{1}"
             report-output-directory="."
             halt-on-failure="true">
             <assemblies>
                <include name="FizzBuzzTests.dll" /> 
             </assemblies>
          </mbunit>
       </target>
    </project>
  3. Run the build script.

Full Syntax

<mbunit
   [report-types="testType"]
   [report-filename-format="reportName"]
   report-output-directory="reportDirectory"
   [halt-on-failure="{true|false}">
   <assemblies>
      <include name="testAssembly" />
      [<include name="testAssembly" /> ...]
   </assemblies> </mbunit>

<mbunit> Attributes

<mbunit> has the following attributes:

<mbunit> Child Elements

<mbunit> has one child element, <assemblies>. This must be included in the task. The name and location of each test file to be run by MbUnit must be included here. The example here uses individual child <include> elements here, but is subject to the same syntax rules as the NAnt fileset type.