There’s a lot of cool things that you can do with nAnt, like running your tests against NUnit or another unit testing framework. Let’s pretend we already have a target called “compile” that will compile our application into an assembly into the build folder. See…

We can create a new target that will compile our test project(s) into a single test assembly and save it to our build folder, where we saved our application assembly. The following target will recurse through a subdirectory called “test” and compile all files with a .cs extension into our test assembly.

<target name="test.compile" depends="compile">
        <csc target="library" output="build\${project::get-name()}.Test.dll" debug="${debug}">
                <include name="src\test\**\*.cs" />
                <exclude name="src\test\**\AssemblyInfo.cs" />
                <include name="build\${project::get-name()}.dll" />
                <include name="tools\nunit\bin\nunit.framework.dll" />
                <include name="tools\rhinomocks\Rhino.Mocks.dll" />

The references element allows us to include other assemblies that our test assembly depends on. In the above case I’m using NUnit, and RhinoMocks. Our test assembly also depends on the application assembly, after all that is what our tests are supposed to be testing.

So far we have compiled our application assembly and test assembly, and saved them to the same directory called build. Now lets create a target that will run our unit tests using the nunit-console.exe.

<target name="test" depends="test.compile">
        <copy todir="build" flatten="true">
            <fileset basedir="tools">
                <include name="**\Rhino.Mocks.dll" />

        <copy todir="build" flatten="true">
            <fileset basedir="tools\nunit\bin">
                <include name="*.dll" />
        <exec basedir="tools\nunit\bin" 
            commandline="${project::get-name()}.Test.dll /xml=${project::get-name()}.Test-Result.xml" />        

The above target will copy over the Rhino.Mocks.dll, and NUnit dlls into our build folder. Then execute “nunit-console.exe” from the command line and pass it in the path to our test assembly, and will tell it what to name the xml file that nunit will spit out with the test results.

As you can see the test results are also shown to the console window. As you can see, I have run 8 tests, with 3 failures. (Looks like I’ve made a breaking change!)

comments powered by Disqus