MSBuild with NUnit

November 28, 2006 - 3 minute read -
xml nunit msbuild

I've written about Unit Testing and Build Automation in the past, but mostly dealing with Java projects and tools (because I usually write about things I'm working on at the time). Well, I've started a .NET project for the first time in a while so I want to solve some of the samde problems in this environment.

Why MSBuild?

In the Java world, the natural choice for automation is usually Ant. NAnt was created for the as a work alike for the .NET platform and is a very good tool. I've build custom extensions for database automation in the past and used it successfully. So why would I consider MSBuild? With the advent of Visual Studio 2005, Microsoft included MSBuild as the build tool for its projects. All of the project files generated by Visual Studio are MSBuild files in disguise. This makes it really easy to leverage the project files to chain together a nice automated build.

Why NUnit?

Microsoft is a true believer in Not Invented Here and as such they created their own Unit Testing framework that looks and acts exactly the same as NUnit. The MS XUnit framework only comes with the higher-end Team System versions of Visual Studio. In addition it is very hard to deploy the DLLs to build systems. The only way to get the proper DLLs installed is to install the full Visual Studio. That seems like a really big problem to me. With NUnit you can include the needed DLLs in your version control system and reference them in the project which makes it very easy to build and test code and use a Continuous Integration process.

How to Integrate MSBuild and NUnit

Here's the good news, all of the work has been done for you. The good people at Tigris.org (the creators of Subversion) have created a series of MSBuild Tasks filling in the gaps that MSBuild was missing. One of the included Tasks is an NUnit task. Their hard work and the simple script below should get ou started with a build that can run your tests as well.

Example Build File

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
     <!-- Import the MSBuild Tasks -->
	<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />
	<PropertyGroup>
		<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
		<ClassLibraryOutputDirectory>bin\$(Configuration)</ClassLibraryOutputDirectory>
		<ProjectDir>ProjectName</ProjectDir >
		<ProjectTestDir>ProjectNameTest</ProjectTestDir >
		<ProjectFile>$(ProjectDir)\ProjectName.csproj</ProjectFile >
		<TestProjectFile>$(ProjectTestDir)\ProjectNameTest.csproj</TestProjectFile >
	</PropertyGroup></p>
<p>	<!-- Build projects by calling the Project files generated by VS -->
	<Target Name="Build">
		<MSBuild Projects="$(ProjectFile)" />
		<MSBuild Projects="$(TestProjectFile)" />
	</Target></p>
<p>	<!-- Run Unit tests -->
	<Target Name="Test" DependsOnTargets="Build">
		<CreateItem Include="$(ProjectTestDir)\$(ClassLibraryOutputDirectory)\*.Tests.dll">
			<Output TaskParameter="Include" ItemName="TestAssembly" />
		</CreateItem>
		<NUnit Assemblies="@(TestAssembly)" />
	</Target>
</Project></p>
<p>

The "Test" target above uses the NUnit task to run all of the NUnit Tests found in the DLL. There are a number of different ways to create ItemGroups ins MSBuild, all of which are odd and confusing. If anyone has any ideas on what the best practices in this regard, let me know.

One final plug for my favorite Visual Studio plugin, ReSharper. It includes support for NUnit. It will help you create NUnit Tests and then lets you run them and debug them from within your IDE. It's almost like you have a real IDE all of a sudden!

References

NUnit MSBuild Tasks Integrating MSBuild with CruiseControl.NET

Pragmatic Unit Testing in C# with NUnit NUnit Pocket Reference Deploying .NET Applications: Learning MSBuild and ClickOnce