NCover Nant Team City Integration
Date: 2008-08-25 21:29:03

I've been spending quite a bit of time setting up http://www.ncover.com/[NCover] and then integrating it into http://www.jetbrains.com/teamcity/[Team City].

I've read some posts which cover parts of this process but nothing which covers the end to end process so hopefully my experience can help to fill that void.

== Step 1

Download NCover 1.5.8, NCover Explorer 1.4.0.7, NCover Explorer Extras 1.4.0.5 from http://www.kiwidude.com/dotnet/DownloadPage.html[Kiwidude's website] and the http://www.ncover.com/download/discontinued[NCover website] .

== Step 2

Put the following into your Nant build file:

[source,text]
----

	<loadtasks assembly="..\lib\NCoverExplorer.Extras\NCoverExplorer.NAntTasks.dll"/>
   	<exec program="regsvr32" workingdir="..\lib\NCover-1.5.8" commandline="/s coverlib.dll"/>
----

I put this right at the top of the build but I expect it doesn't matter where it goes as long as it's called at some stage before NCover and NCover Explorer are called.

[source,text]
----

<macrodef name="cover.tests">
	<attributes>
		<attribute name="in.assemblies" />
	</attributes>
	<sequential>
		<ncover
		    program="..\lib\NCover-1.5.8\NCover.Console.exe"
		    commandLineExe="..\lib\nunit-2.4\nunit-console.exe"
		    commandLineArgs="${build.dir}\UnitTests\UnitTests.dll"
		    coverageFile="${report.dir}\Unit.Test.Coverage.xml"
			assemblyList="${in.assemblies}"
		  />

	  <ncoverexplorer
		program="..\lib\NCoverExplorer\NCoverExplorer.Console.exe"
		projectName="Project"
		reportType="ModuleClassSummary"
		outputDir="${report.dir}"
		xmlReportName="TestCoverage.xml"
		htmlReportName="TestCoverage.html"
		showExcluded="True"
		satisfactoryCoverage="80" >
		<fileset>
		  <include name="${report.dir}\Unit.Test.Coverage.xml" />
		</fileset>
		<exclusions>
		  <exclusion type="Assembly" pattern="*.Tests" />
		  <exclusion type="Namespace" pattern="*.Tests*" />
		</exclusions>
	  </ncoverexplorer>
	</sequential>
</macrodef>
----

This http://peelmeagrape.net/projects/nant_macrodef[macro] can then be called as follows:

[source,text]
----

<target name="cover.unit.tests"
	<cover.tests in.assemblies="Project1;Project1" />
</target>
----

N.B. The projects passed in as the 'in.assemblies' argument should be semi colon separated.

== Step 3

The next step is to setup the artifacts for your project. From the Team City admin panel navigate to the project configuration settings and select artifacts.

Add the following to the 'Artifact paths':

[source,text]
----

TestCoverage.html
----

It should now show up as a viewable artifact from the project listing page.

== Step 4

To get the coverage report to show up on a tab on the build summary page we need to edit the *main-config.xml* file

The location of this file can be found by browsing to 'Administration > Server Configuration' from the Team City admin panel

Add the following line after the other 'report-tab' entries in this file:

[source,text]
----

<report-tab title="Code Coverage Summary" basePath="" startPage="TestCoverage.html" />
----

== Potential Problems

I encountered some problems in getting this up and running. They were as follows:

____
NCover: Profiled process terminated. Profiler connection not established
____

After some Googling I found http://weblogs.asp.net/rchartier/archive/2006/01/30/436897.aspx[this post] which explains how to solve the problem.

To summarise this problem occurs when trying to run NCover without Administrative privileges. The coverlib.dll shipped with NCover needs to be registered. This can be done two ways:

1) Put the following code into your build file right at the top

[source,text]
----

<exec program="regsvr32" workingdir="\path\to\ncover" commandline="/s coverlib.dll"/>
----

2) Run the same command from the command line

[source,text]
----

C:\path\to\NCover-1.5.8>regsvr32 CoverLib.dll
----

____
NCover - Requested value '/r' was not found
____

This error occurred when I was using version 1.0.1 of NCover and to cut a long story short, you need to http://www.ncover.com/download/discontinued[upgrade] to get rid of the problem.

More details are on http://www.markhneedham.com/blog/2008/08/19/ncover-requested-value-r-was-not-found/[this post].

The information here has been accumulated from my experiences, http://weblogs.asp.net/lkempe/archive/2008/03/30/integration-of-ncover-into-team-city-for-tech-head-brothers.aspx[this post] on NCover integration and the http://www.jetbrains.net/confluence/display/TCD3/TeamCity+Data+Directory[official documentation].