Monday, August 20, 2007

Nunit + MSBuild


I wanted to run the test cases for my libraries as a part of building them, so I will make sure every time I have to build the libraries that they are functioning correctly.


Unfortunatly, the built-in tasks will not do that be default; the option here is to call the nunit-console using the


Then doing a simple google search, I found MSBuildCommunityTasks.


MSBuild Community Tasks Project is an open source project for some missing and useful tasks, one of them is the Nunit task.


After I got the installer,I tried to find the documentation and see how to use the Nunit task, but I haven't seen any example on how to use it.


Try and Error; this is the last resort, but I also got some information by looking into the source code of the Nunit task itself to know how to allow the task to find the nunit-console.exe.


I followed the following steps to actually get my Nunit test cases to run during my build.


1 - import the MSBuild.Community.Tasks.Targets files:



2 - Copy the Nunit\bin directory to my source code folder tree under a top level folder; example:







3 - Created a property in the Msbuild script to hold the relative path to nunit binaries







4 - My library and its tests are part of the same solution, so the MSBuild task <<MSBuild> will build the library and the tests at the sametime.






5 - Then come the play with Nunit task to actually run the test cases:








Notice that:

I had to set the DisableShadowCopy="true" to run the tests in their original location and instruct nunit-console not to make a copy in a temp folder


6 - I liked to organize a little bit, so I have created 2 targets; one for the build and one for the test, then a single target to call them both; which makes my build script looks like this:






<Project xmlns="" DefaultTargets="BuildAll" >


<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>








<Target Name="BuildAll" DependsOnTargets="MyLibrary;MyLibrary-Test">



<Target Name="MyLibrary">

<MSBuild Projects="MyLibrary.sln" Targets="Build" />




<Target Name="MyLibrary-Test">













Peter Windsor said...

I've found the documentation. If you installed with the msi, then in C:\Program Files\MSBuild\MSBuildCommunityTasks there's MSBuild.Community.Tasks.chm

randy said...

I installed nunit and put in the tfsbuild.proj parameters. I get this error in my build log:

Target Test:
Unhandled Exception:
System.IO.FileLoadException: Could not load file or assembly 'nunit.framework, Version=, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'nunit.framework, Version=, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77'

Server stack trace:
at System.ModuleHandle.ResolveType(Int32 typeToken, RuntimeTypeHandle* typeInstArgs, Int32 typeInstCount, RuntimeTypeHandle* methodInstArgs, Int32 methodInstCount)