How do I exclude types and methods from being covered by dotCover in TeamCity?

C#TeamcityCode CoverageDotcover

C# Problem Overview


I've got an existing C# 4 project which I've checked the test coverage for by using TestDriven.Net and the Visual Studio coverage feature, i.e. Test With -> Coverage from the context menu.

The project contains some code I don't want covered, and I've solved that by adding the [ExcludeFromCodeCoverage] for those types and methods.

We've just upgraded TeamCity to 6.0.3, and I've added dotCover coverage to the NUnit build step.

I've managed to remove coverage for external assemblies such as NHibernate in the "Filters" section (by explicitly state the assemblies for which I want coverage), but I'm struggling with how to exclude types and methods from covered assemblies.

enter image description here

C# Solutions


Solution 1 - C#

Ok, Martin, I figured it out! It only took an hour of randomly poking at the filter syntax... when the documentation says to add a filter like this

+:myassembly=*;type=*;method=***

They really mean this... where anything in <> is replaced entirely by you and anything else is a literal

+:<myassembly>;type=<filter>;method=<filter>

So, the filter I wanted was to include a single assembly (from a bunch of assemblies) and then exclude a few namespaces in that assembly. I wrote

+:Omnyx.Scanner
-:Omnyx.Scanner;type=Omnyx.Scanner.Simulation.*
-:Omnyx.Scanner;type=Omnyx.Scanner.ToolsCommon.*

Solution 2 - C#

Take a look at the Coverage Analysis from the Command Line - Applying filters page. It looks like you can set up exclusions in the Filters section, similar to how you excluded entire assemblies.

Let's say you want to ignore a method called DoStuff contained in a class MyStuff, which is in the MyAwesomeAssembly library. Then your dotCover XML should look something like this:

<Filters>
  <ExcludeFilters>
     <FilterEntry>
       <ModuleMask>MyAwesomeAssembly</ModuleMask>
       <ClassMask>MyStuff</ClassMask>
       <FunctionMask>DoStuff</FunctionMask>
     </FilterEntry>
  </ExcludeFilters>
</Filters>

Disclaimer: I don't use dotCover, so I'm not 100% sure if this will actually work.

Solution 3 - C#

This is what the TeamCity docs says about the filter options:

Specify assemblies to profile one per line using following syntax: +:myassembly=;type=;method=***

Use -:myassembly to exclude an assembly from code coverage. Asterisk wildcard (*) is supported here.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMartin R-LView Question on Stackoverflow
Solution 1 - C#Anthony MastreanView Answer on Stackoverflow
Solution 2 - C#Adam LearView Answer on Stackoverflow
Solution 3 - C#stealfView Answer on Stackoverflow