Setting up MSBuild with NUnit, NCover and NCoverExpxlorer
The following guide will help you to create and execute an MSBuild configuration that will run tests with NUnit and also provide a NCover coverage report. The most challenging part was working with the free community edition for the tools, .NET 4.0, and also setting up the environment on a Windows 7 64 bit laptop.
2. Getting the right tools and know them
We need to download the right tools at the right version, I will provide a brief description of each
This tool will help us to run our unit tests. You can download it from here: http://nunit.org/index.php?p=download. If you go to the bin folder, you will see two versions of the nunit-console application, we will be using nunit-console-x86.exe
It will generate a coverage.xml file, that indicates how much code has been tested on our project. It has a community free option, which is the last option at the following link: http://www.ncover.com/download/current. You need to register in order to download it.
Helps us to create an HTML report file from the coverage.xml generated with NCover. The free version is available at: http://www.kiwidude.com/dotnet/DownloadPage.html
You will need to extract the files at a specific location.
NCoverExplorer Extras (22.214.171.124)
This package contains a couple of plugins that can help us with tasks in MSBuild, which can be downloaded also from: http://www.kiwidude.com/dotnet/DownloadPage.html
We’ll see how do we link these in the MSBuild file. I recommend you to put the extras folder in the same directory you installed NCoverExplorer
As an alternative, you can download and install TestDriven.Net, which contains the three applications described above, you can get it here: http://www.testdriven.net/download.aspx
Note that, still you will need to get the NCoverExtras for the MSBuild task plugins
I recommend using TestDriven, since it also installs you a nice add-on for Visual Studio, which helps you to run tests easily. I will use TestDriven for the rest of the tutorial
3. Doing it
I suggest to go to https://github.com/aharita/calculator while reading the rest, so you can examine the complete source code. In this section I will explain some of the troubles I got into, and how to solve them. Some of those are particular to .NET 4.0 and having Windows 7 64 bit
MSBuild example file
We need to add three tasks in our MSBuild file
<UsingTask TaskName="NCoverExplorer.MSBuildTasks.NCoverExplorer" AssemblyFile="C:\Program Files (x86)\TestDriven.NET 3\NCoverExplorer\NCoverExplorer.Extras\bin\NCoverExplorer.MSBuildTasks.dll"/> <UsingTask TaskName="NCoverExplorer.MSBuildTasks.NCover" AssemblyFile="C:\Program Files (x86)\TestDriven.NET 3\NCoverExplorer\NCoverExplorer.Extras\bin\NCoverExplorer.MSBuildTasks.dll"/> <UsingTask TaskName="NCoverExplorer.MSBuildTasks.NUnitProject" AssemblyFile="C:\Program Files (x86)\TestDriven.NET 3\NCoverExplorer\NCoverExplorer.Extras\bin\NCoverExplorer.MSBuildTasks.dll"/>
We also need to add the proper sections for handling NUnit, NCover and NCoverExplorer
<Target Name="NUnitSimple"> <Exec Command=""C:\Program Files (x86)\TestDriven.NET 3\NUnit\2.6\nunit-console-x86.exe" "C:\Users\aharita\Documents\GitHub\calculator\Calculator.Tests\bin\Debug\Calculator.Tests.dll""/> </Target> <Target Name="NCoverSimple"> <NCover ToolPath="C:\Program Files (x86)\TestDriven.NET 3\NCover\1.5.8\" CommandLineExe="C:\Program Files (x86)\TestDriven.NET 3\NUnit\2.6\nunit-console-x86.exe" CommandLineArgs="C:\Users\aharita\Documents\GitHub\calculator\Calculator.Tests\bin\Debug\Calculator.Tests.dll" /> </Target> <Target Name="NCoverExplorerSimple" DependsOnTargets="NCoverSimple"> <NCoverExplorer ToolPath="C:\Program Files (x86)\TestDriven.NET 3\NCoverExplorer" ProjectName="Calculator Operations Tests" XmlReportName="CoverageSummary.xml" HtmlReportName="CoverageSummary.html" CoverageFiles="C:\Users\aharita\Documents\GitHub\calculator\coverage.xml" /> </Target>
Build.bat helper file
I also have a build.bat file that helps you to run msbuild with the build.xml MSBuild configuration
set COMPLUS_ProfAPI_ProfilerCompatibilitySetting=EnableV2Profiler C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe build.xml
There are some problems that you might have while trying to do the setup, the most common ones are:
Profiler connection not established.
The full error is:
</pre> Profiled process terminated. Profiler connection not established. error MSB6006: "NCover.Console.exe" exited with code 1. <pre>
- You need to use nunit-console-x86.exe on all the MSBuild references
- You need to execute the following in the same command window you execute your msbuild script
- set COMPLUS_ProfAPI_ProfilerCompatibilitySetting=EnableV2Profiler
You can see the example file provided on GitHub
Empty coverage.xml file
Sometimes, you will end up with an empty coverage.xml file, some solution to this:
- Open nunit-console-x86.exe.config and change it as follows:
</pre> <startup useLegacyV2RuntimeActivationPolicy="true"> <!-- Comment out the next line to force use of .NET 4.0 --> <supportedRuntime version="v4.0.30319" /> <supportedRuntime version="v2.0.50727" /> </startup> <pre>
Hopefully, this guide will be helpful and you won’t have the same headaches as I had trying to do the setup. Following are some links resources that helped me through the problem.