Sunday, July 16, 2006

NAnt and MSBuild Tasks for NCover

I received an e-mail this week from Jay Flowers asking if I would be interested in helping him put together NCover and NCoverExplorer plugins for CI Factory. The intent is to provide these as an alternative for the CoverageEye package he already includes.

I normally use the NAnt <exec> task for running NCover on our build servers, which does the job fine once you have it setup. There is unfortunately a bit of pain involved in truly dynamic scripts ensuring paths with spaces are handled correctly, building delimited lists of assemblies etc. The NCover command line in combination with NUnit can be a frustrating learning curve, not to mention it has changed between versions so even looking for answers in web blog articles can be confusing when they are out of date or "simplified".

To make this a little more painless for both Jay and anyone else interested I ended up creating NAnt and MSBuild tasks for NCover, supporting NCover 1.3.3 and 1.5.x to date.

I have now included these tasks in my existing NCoverExplorer NAnt/MSBuild task assemblies located in, along with all the source code and example .build/.proj files.

Note: These tasks still require that NCover has it's CoverLib.dll COM registered correctly on your machine. You can always do this as part of your NAnt/MSBuild script prior to using these tasks if not using the default NCover MSI installation.

Here is an example of the NAnt <ncover> task at it's simplest for NCover 1.3.3 - note the use of the version attribute. The <ncover> task will default to NCover 1.5.4 command line syntax if this is not present.

[Update 03 Sep 2006: As of build the <ncover> tasks are self registering without needing local admin rights. The version attribute is also now redundant - it will be picked up from the NCover.Console.exe assembly if not specified]

<ncover program="Tools\NCover\NCover.Console.exe"
version="1.3.3" />

A more complex NAnt example using NCover 1.5.4 showing additional NUnit arguments, customised logging and xml filenames, a coverage exclusion attribute (in the root namespace) and specific non-test assemblies to report coverage on:

<ncover program="Tools\NCover\NCover.Console.exe"
commandLineArgs="MyApp.UnitTests.dll /xml=MyApp.nunit.xml /labels /nologo"
<assemblies basedir="${build.output.dir}">
<include name="MyApp*.dll" />
<exclude name="MyApp.UnitTests.dll" />

Here is a simple MSBuild <NCover> task example:

<UsingTask TaskName="MSBuild.NCoverExplorer.Tasks.NCover"
AssemblyFile="MSBuild.NCoverExplorer.Tasks.dll" />
<NCover ToolPath="Tools\NCover\"
CommandLineArgs="MyApp.UnitTests.dll" />

I did manage to confirm a few bugs/unfinished features in NCover 1.5.4 itself while giving this a test, so where possible I have either worked around or excluded these features until they are one day resolved. FYI, they involved the //q and //h command line arguments, which you should avoid using!

Feel free to leave a comment or drop me an e-mail if you find these tasks useful (or find a bug of course!).

Download (CC.Net/NAnt/MSBuild)

[Update: I'm more than happy to answer questions for people adding these into their builds. However rather than using the blog comments can you please put your query in the NCover or NCoverExplorer forums as I blogged about here.]

[Update 03 Sep 2006: NAnt and MSBuild tasks have been updated and improved. See this thread for the changes listed in build or the 1.3.5 release notes. Documentation can now be found here for MSBuild and here for NAnt (or in the zip file).]

[Update 07 Sep 2006: Bug-fix update for the NCover tasks where multiple arguments specified in the command line. Build now available at the above links.]

[Update 01 Oct 2006: Updates to the NCoverExplorer tasks to support a new regular expression feature for coverage exclusions and improved documentation. Build now available. NOTE you must also upgrade your NCoverExplorer version to the latest 1.3.5 beta.]


At July 17, 2006 4:36 AM, Blogger Kent Boogaart said...

Good stuff - thanks for the effort. I resorted to Exec in my MSBuild scripts, but would definitely have prefered a custom task such as this.

You should consider contributing these to NAntContrib and MSBuild Community Tasks respectively.

At July 17, 2006 9:18 AM, Blogger Matt said...

Awesome stuff. :) Agree with Kent, contribution to NAntContrib (and MSBuild Community) would be great.

If you want a hand let me know.

At July 17, 2006 7:54 PM, Blogger kiwidude said...

Kent/Matt, thanks for the feedback. I had wondered about NAntContrib and MSBuild Community when I wrote the "ncoverexplorer" tasks, however at the time I wanted the freedom to quickly enhance the API as I added new functionality. NAnt in particular had been pretty slow in its releases having a big hiatus at the time. Now that things are more stable and I have the "ncover" tasks too it may be more worth the effort. I will try to take a look into it when I get a chance to figure out who to contact etc...

At July 24, 2006 11:19 PM, Blogger Jasdeep Singh said...

Nice stuff kiwidude! Experienced a minor hitch and was wondering if you could help out. I wasn't able to make the NCover tasks work with multiple test assemblies and I thought that was possible.

I tried giving in the include clause a '*.dll' paramter, have even tried separating the two assemblies with a semi colon (as suggested by the ncover console) in the include clause and have tried multiple includes but still didnt work out.

Any ideas here?

At July 24, 2006 11:48 PM, Blogger kiwidude said...

Jasdeep - are you trying to use the NAnt task or the MSBuild one? The NAnt task assemblies node is just a "fileset" underneath. I find it useful to set the basedir on the assemblies node and then a wildcard pattern in the include node should work fine. Send me an e-mail if you are still having probs to 'support at ncoverexplorer dot org'.

At July 25, 2006 12:49 AM, Blogger Jasdeep Singh said...

Hello. I just sent an email at the address provided with the entire NANT build file.

Thanks for the help. Please let me know if you have any questions.

At July 26, 2006 6:13 PM, Blogger kiwidude said...

Re Jasdeep's issue - I believe this came down to not having the working directory set correctly. Set it to the folder where your assemblies you want to test/cover reside and all should work fine.

At July 31, 2006 4:43 AM, Blogger David Keaveny said...

Just wanted to add my Nice work comment to the others - while I've had NAnt <exec>ing NCover before, I prefer to use custom tasks when possible.

I'm having one small problem though - while the unit tests run OK, the coverage.xml file doesn't get generated properly, there's just a stub file with no coverage results. Any thoughts?

At July 31, 2006 9:01 AM, Blogger kiwidude said...

Thanks for the kind words David. Most likely causes for your problem - you have not set the working directory correctly or you have not COM registered CoverLib.dll in the NCover folder. Add the "verbose=true" argument and e-mail me the NAnt output if still having problems - email address four comments above.

At August 10, 2006 12:11 PM, Anonymous Vikash said...

Hi, thanks for your great work !. I followed all your directions, put the custom task into the Nant bin folder but I still recieve teh folowing error:

Invalid element trycatch

At August 10, 2006 1:21 PM, Blogger kiwidude said...

Vikash - if your nant script makes use of the trycatch task then you need to be sure you have "nantcontrib" installed. If you or anyone else has further queries on these tasks please instead post them in the forums over at

At August 11, 2006 3:39 AM, Anonymous tsune said...

Perhaps, isn't it a typing error?

at NAnt ncover task for NCover 1.5.*

coverageExclusions="CoverageExcludeAttribute" --> excludeAttributes="CoverageExcludeAttribute"

At August 11, 2006 8:33 AM, Blogger kiwidude said...

Thanks for the spot tsune, fixed.

At August 23, 2006 2:36 AM, Blogger David Keaveny said...

Don't worry, I eventually found out what the problem was - the version I was using did not support multiple assemblies. I downloaded the v1.3.4.1736 version, and that works just fine.

Is there a changelog published somewhere? That might save another morning or two of head-scratching!

I must say, that after getting NAnt/NUnit/NCover/NCoverExplorer all up and running with CruiseControl.NET, I look like an absolute f***ing hero in my department. The beautiful code coverage reports appearing in CC.NET are the cherry on top :-)

At September 03, 2006 9:07 PM, Blogger kiwidude said...

Hey David, glad to hear you got there in the end and you are pleased with the result! As far as the changelog goes - I admit it is an omission from the zip compared to what I do for the NCoverExplorer release notes. I have just now pushed up a new version of this post with links to yet another update of the tasks - all changes are mentioned in the NCoverExplorer 1.3.5 beta forum thread referenced in there.

At September 07, 2006 2:21 PM, Anonymous Romu said...

Hi kiwidude, I used a previous version of ncover NAnt task without any problem.

With the new one (1789), I get an issue with the "commandLineArgs" parameter.

I used it to pass to NUnit several test assemblies separated by spaces and this worked well, this doesn't work anymore.

FYI, it works with a single test assembly.

At September 07, 2006 2:35 PM, Blogger kiwidude said...


Can you please post your query in the NCover forums (or email me) instead of here? If you can show your xml ncover task I can take a look. Also you could try the NCoverExplorer 1.3.5 beta which generates nant/msbuild tasks etc to check against your own syntax. Thanks.

At September 07, 2006 2:54 PM, Anonymous Romu said...

Ok, I'm goiing to post now

At March 05, 2007 2:32 PM, Blogger Rama said...

Can some one help me with a ccnet.config file which uses MSBuild to build and NCover to find coverage using Cruisecontrol.NET ?

Would appreciate if you can send the config file it to my account

Thanks a ton

At February 27, 2008 10:14 PM, Blogger jota jota said...

Thanks a bunch brother. This was the most straightforward explanation. Implementation, integration with Nant and was simplistic.


At April 14, 2008 11:31 AM, Anonymous Rory said...

Great blog!

We have one problem though. We're using NCover 2.1 with MSBuild and MSTest. We get a coverage file, but although it contains data, it appears to be missing the visitcount attribute on all the seqpnt elements, and therefore the XSL generates no output. This is puzzling, since when we view the build log of the project through CruiseControl, it states "Sequence Point Coverage: 40.0%" and "Branch Coverage: 40.3%". Do you have any ideas that might help us resolve this problem?

At April 14, 2008 7:05 PM, Blogger kiwidude said...

Hi Rory,

Any problems related to 2.x I suggest you post on the forums at where I know the guys will be more than happy to help you out.

At October 29, 2008 2:43 PM, Blogger NAV said...

Nice hard work, really appreciated! Can anyone help me to Config ccnet.config file for my firstproject with Cruise control, everything work well so far , as i can force the build but now i want to run my unit test with Nunit, how to configure them with Cruise control. Any kind of help/direction will be appreciated.

At November 25, 2008 12:04 AM, Blogger us said...

I have a problem running Nant Script. I get coverage file but it doesn't have any data. So the html output say 0% coverage. I am using NCover 1.5.8

At November 25, 2008 7:30 AM, Blogger kiwidude said...

us - I suggest you either take a look at my blog posts on "NCover Problems / Fixes" and if that doesn't fix it for you then post to the "legacy" version forums at

At October 27, 2009 3:01 PM, Anonymous Anonymous said...

Links be busted :(

At October 27, 2009 6:21 PM, Blogger kiwidude said...

The Downloads link on the RHS wasn't - or I would also suggest going to which supports bot the free and commercial versions. This is a very old post dude!


Post a Comment

Links to this post:

Create a Link

<< Home