Monday 5 January 2009

NAnt HowTo #3: How To Run NUnit Tests From Your Build File

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">
<exec
program="D:/bin/nunit/nunit-console.exe"
workingdir="D:/projects/MyProject/Integration"
commandline="MyProject.Tests.dll /xml:TestResults.xml /nologo"/>
</target>

So, step by step.
  1. 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?!

  2. We specify path to our nunit-console.exe executable via the program attribute.

  3. We specify working directory (usually it's integration dir where you have all needed assemblies) via the workingdir attribute.

  4. We pass command line parameters via the commandline attribute.
Actually this could be the end of the post but I want to say some words on command line arguments of nunit-console.
  1. First non-keyed (with no preceding /im-a-key: keys) several arguments specify assemblies to run tests from.

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

  3. /nologo key suppresses NUnit copyright information display on each run
You can read more about these command line arguments on the official NUnit website.

That's the end :) Next time I will probably speak on writing NAnt custom tasks. Stay online.


kick it on DotNetKicks.com

3 comments:

Uday The Rising said...

Good one!! Helped me a lot

Ivan Suhinin said...

You are welcome :) By the way, I've moved to http://deviation-alliance.com and all the goodies are there now :)

Muzi said...

Thank you, works like a "BOSS".