Automating MbUnit With MSBuild

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

  • Run scheduled or triggered builds for your project
  • Run any number of pre- and post-build events on your code, such as unit testing, code analysis and code metrics packages.

and all without a sniff of Visual Studio. MSBuild is freely available as part of the .NET 2.0 Framework. Its build scripts are written as XML documents and can be automated or run manually as appropriate. Indeed, Visual Studio .sln files are actually MSBuild files and you can build your solutions directly with msbuild without ever opening Visual Studio if you wanted to. You can find the full syntax reference for MSBuild here.

The MbUnit Custom MSBuild Task

MbUnit includes code for a custom <MbUnit> MSBuild 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. Know where copies of the MbUnit DLLs are. You don’t need to copy them anywhere, but you do need to make sure that the following four MbUnit DLLs are in the same directory.
    • MbUnit.MSBuild.Tasks.dll
    • MbUnit.Framework.dll
    • QuickGraph.dll
    • QuickGraph.Algorithms.dll
    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. With that done, add a <UsingTask> node to your MSBuild project that specifies the full name of the MSBuild task (MbUnit.MSBuild.Tasks.MbUnit) and the location of the MbUnit.MSBuild.Tasks.dll file. Note that MSBuild will assume that all your other MbUnit DLLs are in the same directory as this, hence the need to have them all in one place. The DLL directory can be given as an absolute or relative path.
  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.
    Copy 
    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
       <UsingTask 
          TaskName="MbUnit.MSBuild.Tasks.MbUnit" 
          AssemblyFile="..\libs\MbUnit.MSBuild.Tasks.dll" />
       <!-- defining test assemblies -->
       <ItemGroup>
          <TestAssemblies Include="FizzBuzzTests.dll" />
       </ItemGroup>
       <!-- test target -->   
       <Target Name="Tests">
          <MbUnit
             Assemblies="@(TestAssemblies)"
             ReportTypes="html"
             ReportFileNameFormat="myreportname{0}{1}"
             ReportOutputDirectory="." />
       </Target>
    </Project>
  3. Run the build script

Full Syntax

Copy 
<MbUnit
   Assemblies="@(TestAssemblies)"
   [ReportTypes="reportType"]
   [ReportFileNameFormat="reportName"]
   [ReportOutputDirectory="reportDirectory"]
   [HaltOnFailure = {true|false}]
   [HaltOnError = {true|false}]
/>

<MbUnit> Attributes

<MbUnit> has the following attributes:

  • Assemblies : Specifies the set (ItemGroup) of test file to be run by MbUnit. This element is mandatory.
  • ReportTypes : Specifies what type of report will be generated after the test run. testType is a semi-colon separated list of one or more of the following types - html, text, xml, dox. The default is html.
  • ReportFileNameFormat : Specifies the name of the test report file being generated. By default, reportName is set to “mbunit.{0}.{1}” where {0} is replaced by a long datetime string and {1} is replaced by the time (on a 24hr clock).
  • ReportOutputDirectory : Specifies the directory where the test report file will be saved. reportDirectory can be either an absolute path or relative to the current directory when MSBuild is run. Note that this attribute is mandatory and must be given a value.
  • HaltOnFailure : Specifies whether or not the rest of the MSBuild script should halt if test failures occur. It can have values of true or false. The default is false.
  • HaltOnError : Specifies whether or not the rest of the MSBuild script should halt if an error occurs. It can have values of true or false. The default is false.