Merge TeamCity DotCover reports into overall code coverage

I’m just posting this quick tip because I couldn’t see any good information online already and it’s so simple once you’ve found the answer.

Problem

Lots of TeamCity test configurations which all generate code coverage reports using DotCover, but no overall view of the combined coverage.

Solution

TeamCity already knows how to merge DotCover reports, but it has to know about them all in one build configuration.

1) Create a new build configuration with snapshot dependencies on each test project.

Use artifact rules of the form :

+:.teamcity/.NETCoverage/dotCover.dcvr=> folder1

You probably want to set them to not run a new build if there is a suitable one and run build even if dependency failed.

2) Add a VCS root to get your source code in an appropriate location (the checkout root)

3) Add a powershell step that does something a bit like this

foreach ($snapshotFolder in $args)
{
$filename = Join-Path $snapshotFolder “dotCover.dcvr”
Write-Host “##teamcity[importData type=’dotNetCoverage’ tool=’dotcover’ file=’$filename’]”
}

And then pass folder1 folder2 etc. as space separated arguments

This just uses TeamCity service messages to inform TeamCity about the snapshots, it does the rest of the work – look for a Code Coverage tab at the end of the build.

Note for older versions of dotcover the extension was “.snapshot” instead of “.dcvr”

Alternatively you can use this metarunner:

<?xml version=”1.0″ encoding=”UTF-8″?>
<meta-runner name=”Report merged DotCover code coverage”>
<description>The configuration using this should depend on each test configuration to merge with a dependency rule like ‘+:.teamcity/.NETCoverage/dotCover.dcvr =&gt; folderContainingDotcoverSnapshot1′. It’s usually best to use snapshot dependencies but allow failed builds. You will also need to add a VCS root for your source code.</description>
<settings>
<parameters>
<param name=”system.SnapshotFolderNamesSeparatedBySpaces” value=”folderContainingDotcoverSnapshot1 folderContainingDotcoverSnapshot2 etc.” spec=”text description=’A space-separated list of folder paths, each of which should contain a file called dotCover.dcvr from a different dependency. Note that if you used assembly filters they should be the same for all input dependencies.’ display=’normal’ label=’Folders containing snapshot files’ validationMode=’not_empty'” />
</parameters>
<build-runners>
<runner name=”Import from folders” type=”jetbrains_powershell”>
<parameters>
<param name=”jetbrains_powershell_bitness” value=”x86″ />
<param name=”jetbrains_powershell_execution” value=”PS1″ />
<param name=”jetbrains_powershell_noprofile” value=”true” />
<param name=”jetbrains_powershell_scriptArguments” value=”%system.SnapshotFolderNamesSeparatedBySpaces%” />
<param name=”jetbrains_powershell_script_code”><![CDATA[foreach ($snapshotFolder in $args)
{
$filename = Join-Path $snapshotFolder “dotCover.dcvr”
Write-Host “##teamcity[importData type=’dotNetCoverage’ tool=’dotcover’ file=’$filename’]”
}]]></param>
<param name=”jetbrains_powershell_script_mode” value=”CODE” />
<param name=”teamcity.step.mode” value=”default” />
</parameters>
</runner>
</build-runners>
<requirements />
</settings>
</meta-runner>

If you want the Code Coverage tab to work, you will also need to tell TeamCity where to find the source code by adding a configuration parameter to your build configuration called “dotNetCoverage.dotCover.source.mapping” and set to something of the form “%dep.yourDependencyId.teamcity.build.checkoutDir% => %system.teamcity.build.checkoutDir%”.

About GrahamTheCoder

I'm a C# developer based in Cambridge hoping blogging will achieve some or all of the following. Help me organise my thoughts. Practice writing things other than code. Give me a place to refer people to when I'm trying to make a long winded point. I welcome comments and constructive criticism, and hope to look back at my written opinions in the future and laugh at my own naivety.
This entry was posted in C# and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s