# Mark Needham

Thoughts on Software Development

## NCover Nant Team City Integration

I’ve been spending quite a bit of time setting up NCover and then integrating it into 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 Kiwidude’s website and the NCover website .

### Step 2

Put the following into your Nant build file:

1 2 <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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 <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 macro can then be called as follows: 1 2 3 <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': 1 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:   ### 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 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 1 <exec program="regsvr32" workingdir="\path\to\ncover" commandline="/s coverlib.dll"/> 2) Run the same command from the command line 1 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 upgrade to get rid of the problem. More details are on this post. The information here has been accumulated from my experiences, this post on NCover integration and the official documentation. Written by Mark Needham August 25th, 2008 at 9:29 pm Posted in Build Tagged with , , , ## Encapsulation in build scripts using nant with 2 comments When writing build scripts it’s very easy for it to descend into complete Xml hell when you’re using a tool like nant. I wondered previously whether it was possible to TDD build files and while this is difficult given the dependency model most build tools follow. That doesn’t mean we can’t apply other good design principles from the coding world however. Encapsulation is one of the key principles of OOP and it can be applied in build files too. Stephen Chu talks about this in his post on Pragmatic Nant Scripting where he recommends having 3 different levels of targets to help create this encapsulation. I’ve been trying to follow this advice with our build scripts and today Bernardo made the suggestion of using macros in an English readable way. He calls it OO Scripting – it’s effectively a DSL inside a DSL if you like. I was having problems with the ncover nant task – the following error message was being thrown every time I called it: could not find ncover folder in your path in NCoverExplorer.NAntTasks.NCoverUtilities I managed to find the source code for that class and had a look at it but I couldn’t figure out what was going wrong without debugging through it. The strange thing was that it worked fine from the command line which suggested to me that I was getting something simple wrong. I created a cover.tests macro to encapsulate the details of how I was executing the coverage. The plan was to get it working using an exec call to the ncover executable and then phase the ncover nant task back in when I’d figured out what I was doing wrong. This is what I started out with: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <macrodef name="cover.tests"> <attributes> <attribute name="in.assemblies" /> </attributes> <sequential> <copy file="\path\to\Coverage.xsl" tofile="${report.dir}\Coverage.xsl" />   <exec program="..\lib\NCover-1.5.8\NCover.Console.exe"> <arg value="..\lib\nunit-2.4\nunit-console.exe" /> <arg value="${build.dir}\UnitTests\UnitTests.dll" /> <arg value="//a" /> <arg value="${in.assemblies}" /> <arg value="//x" /> <arg value="\${report.dir}\Unit.Test.Coverage.xml" /> </exec> </sequential> </macrodef>

//a is the assemblies to include in the report

//x is the name of the report xml file which will be created

The full list is here.

The macro was called like this:

 1 2 3   

I substituted the ncover task back in with the same parameters as above and low and behold it worked!

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16   

I’m not sure exactly what the problem parameter was but encapsulating this part of the build gave me the option of working that out in a way that impacted very little of the rest of the build file.

*Update*
Fixed the first example to include the opening as pointed out by Vikram in the comments. Thanks again Vikram for pointing that out!

Written by Mark Needham

August 21st, 2008 at 12:40 am

Posted in Build

Tagged with , , , ,

with one comment

I’ve been trying to integrate NCover into our build and probably making life harder for myself than it needs to be.

The title refers to the error message that I was getting when trying to run the ncover nant task on version 1.0.1 of NCover earlier today.

 1 2 3 4 5 6 7 8  [ncover] Starting 'C:\Program Files\NCover\ncover-console.exe (//r "\long\path\to\tmp392.tmp.ncoversettings" )' in 'C:\my-project\trunk\src' [ncover] Unhandled Exception: System.ArgumentException: Requested value '/r' was not found. [ncover] at System.Enum.Parse(Type enumType, String value, Boolean ignoreCase) [ncover] at NCover.Utilities.Arguments.ParseArgument(String arg, CommandLineArgument& key, String& value) in C:\to ols\eclipse3M6\workspace\ncover\src\NCover\Utilities\Arguments.cs:line 192 ...

After some inspired Googling my colleague managed to work out that the problem was that you can’t pass a settings file path which has spaces in to the ncover executable, hence the error message. It’s the same problem in handling spaces that I mentioned in an earlier post on msbuild.

The advice on the forum was to upgrade to one of the more recent versions where the bug has been fixed. Downloads of the free version of NCover (it becomes paid for at version 2.0) are available here.

Written by Mark Needham

August 19th, 2008 at 9:18 pm

Posted in Build

Tagged with , , ,