This post continues my NAnt HowTo series. In previous posts I've covered topics of compiling your project and splitting your build file. Today I want to tell you how to run NAnt unit tests from your NAnt build script.
First of all you may notice that main NAnt distribution has TWO NUnit tasks: nunit and nunit2. First one is designed to work with NUnit 1.0 and second one with NUnit 2.2. This simple moment can warn you that something is not as good as it seems here. And you will be right :) Me personally had a problem running NUnit of some lately version using this nunit2 task. After some googling I've found a solution in Scott Hanselman's blog where he spoke with his friend on same topic. As a result of this conversation Scott recommended using nunit-console.exe instead. I've tried it - it works :) Now changing NUnit version will not break as nunit-console command-line specification is not something to change when switching from 2.x to 2.(x+1).
How can we do this? Simple enough:
<target name="tests.unit.run" description="Run unit tests">
commandline="MyProject.Tests.dll /xml:TestResults.xml /nologo"/>
So, step by step.
- We use <exec> task to run an executable. This also means that if our nunit-console.exe executable fails (read: some test fails) it will break our build. Of course, you may use failonerror="false" attribute on your <exec> task but I do not recommend doing so - why would anyone ever need tests if their failure will be ignored?!
- We specify path to our nunit-console.exe executable via the program attribute.
- We specify working directory (usually it's integration dir where you have all needed assemblies) via the workingdir attribute.
- We pass command line parameters via the commandline attribute.
- First non-keyed (with no preceding /im-a-key: keys) several arguments specify assemblies to run tests from.
- Argument after /xml: key is a bit more interesting. It indicates the XML file where test results will be stored. You may not need it at the moment but you'll definitely need this file when you'll be integrating your NAnt build script with CruiseControl.NET or any other integration software.
- /nologo key suppresses NUnit copyright information display on each run
That's the end :) Next time I will probably speak on writing NAnt custom tasks. Stay online.