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
- 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. 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
- Copy the following DLLs from the MbUnit installation directory to the NAnt bin
directory.
- MbUnit.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.
- 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>
- 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:
- report-types : Specifies what type of report will be generated
after the test run. testType is one of the following types - html, text,
xml, dox. The default is html.
- report-filename-format : Specifies the name of the test report
file being generated. By default, reportName is set to “mbunit-result-{0}{1}”
where {0} is replaced by a long datetime string and {1} is replaced by the time
(on a 24hr clock).
- report-output-directory : 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 NAnt is run. Note that this attribute
is mandatory and must be given a value.
- halt-on-failure : Specifies whether or not the rest of the NAnt
script should halt if test failures occur. It can have values of true or false.
The default is false.
<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.