HowTo: MsBuild &NUit

 

imageBecause I’m using NUnit instead of MSTest in my actual project I want to run the NUnit Tests in my build as well. If you want to find out how to compare MSTest with MSBuild click here. It’s not that more difficult with NUnit.

 

Assumption

At the moment I work with NUnit Version 2.5.9 (actually in development). In Version 2.5.8 exists a bug which makes you unable to close the NUnit Agent. Probably the problem belongs to .NET 4.0 so please try to get the newest version.

After the installation you will find the Nunit file here:

C:\Program Files (x86)\NUnit 2.5.9

copy NUnit 2.5.9 /Runner /Files into the project directory

One possibility is to replace the NUnit Test Runner (in this case the nunit-console.exe) with a system path or another way is to copy all the files into the project directory.

Everything beneath “C:\Program Files (x86)\NUnit 2.5.9\bin\net-2.0″ should be saved in the project directory or beneath.

I saved the file here: “PROJEKTNAME\Tools\NUnit\”

MsBuild Community Pack

Like in some other MSBuild blogposts I´ve used the MSBuild Community Pack because there is a nice “NUnit” task included.

Demo solution

image

Exapt the NUnit Test Runner files you see everything thats important at the picture on the left. In Build\Lib you will find the MSBuild Community Pack and we also have a .bat file for easier achievement and of course our MsBuild file. The “MSBuildNUnit. Tests project is my “Demo” test project. Here is the link for the nunit.framework situated and one test.

The MSBuild File

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Run">
<Import Project="$(MSBuildStartupDirectory)\Lib\MSBuild.Community.Tasks.Targets"/>
	<PropertyGroup>
    <!-- After Compile: Result will be saved in OutDir -->
		<OutDir>$(MSBuildStartupDirectory)\OutDir\</OutDir>

    <!-- Configuration -->
    <Configuration>Release</Configuration>

    <!-- Solutionproperties-->
		<SolutionProperties>
      OutDir=$(OutDir);
      Platform=Any CPU;
      Configuration=$(Configuration)
    </SolutionProperties>
	</PropertyGroup>
	<ItemGroup>
		<Solution Include="..\MSBuildNUnit.sln">
			<Properties>
				$(SolutionProperties)
			</Properties>
		</Solution>
	</ItemGroup>
	<Target Name="Run">
    <Message Text="Run called." />

    <CallTarget Targets="BuildSolution" />
    <CallTarget Targets="RunTests" />
  </Target>

  <Target Name="BuildSolution">
    <Message Text="BuildSolution called." />
    <MSBuild Projects="@(Solution)"/>
	</Target>

  <Target Name="RunTests">
    <!-- Run Unit tests -->
    <CreateItem Include="$(OutDir)*.Tests.dll">
      <Output TaskParameter="Include" ItemName="TestAssembly" />
    </CreateItem>
    <NUnit ToolPath="..\Tools\NUnit" DisableShadowCopy="true" Assemblies="@(TestAssembly)" />
  </Target>

</Project>

Between Line 32 and 43 you can see the NUnit integration. In OutDir I pick up all assemblies including a “test” and call them with the NUnit Task. I also need to define the ToolPath for the runner because otherwise the script won’t find the runner.

As result we have a TestResults.xml at the end of the build in the build directory.

All in all: A good and easy thing.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

About the author

Written by Code Inside Team

Learn more about our team.

One Response

Comment on this post

Recent Posts

  • Automated Security Analyser for ASP.NET websites

    Evil Hackers are lurking everywhere and many Web-applications are delicately and share “too much” with the attacker. A quick (first!) overview offers the Tool “ASafaWeb”. All the website does is making a few requests and writing an Analyses including problem solving’s. There are no permanent disadvantages (bad requests/ DoS attacks and so on). Example: KnowYourStack.com ...

  • image1489-570x194.png
    „Sign in with Twitter“ for your own ASP.NET WebApp

      “Sign in with Twitter” is a popular practice to authenticate the users on your website. One advantage compared to an own registration is the lower inhibition for the user. But on the other hand Twitter doesn’t fess up with all the information’s and you will get into a kind of addiction. At the end ...

  • image1485-570x194_thumb.png
    CodePlex is going to be updated

      CodePlex the Microsoft Open Source Project Hosting Plattform hasn’t changed that much in the last few years and for a few times I thought Microsoft stopped the whole developing process. But now I found out that there is still life in the project. Maybe it is because of the success of GitHub or because ...

  • image1474_thumb.png
    What does Adobe in the flash-free web? Magazine-Style Layouts with CSS Regions!

      Adobe is well known for Photoshop and Flash but of course there is a lot more. According to the “Future Post” from Google Adobe declared one of their big subjects on a Blogpost. I’m talking about the W3C Working Draft to CSS Regions. Adobe cooperates with the WebKit Team and W3C on this. What ...

  • image1471-523x194.png
    HTML 5 Games, Tooling & 3D

      Game Developing is an interesting subject for all kind of software developer. But as a web developer without any Flash-skills there aren’t that much starting points. With HTML5 and the combination between Javascript, CSS3 and fast browsers there are the first “robust” HTML5 games. HTML5 games? Is this real? Neowin created a “Top 10” ...

Support us