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

3

56

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

Sanorasans answered 12/4, 2011 at 7:1 Comment(0)
D
70

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.*
Dissepiment answered 1/6, 2011 at 22:51 Comment(2)
After trawling through the dotCover source (with dotPeek, naturally), I've managed to work out how you specify multiple filters on the command line. You separate filters with ; and a filter can either be an assembly filter, a type filter or a method filter. So to include AssemblyA but exclude types in the AssemblyA.Blah namespace: +:AssemblyA;-:type=AssemblyA.Blah.*Stickpin
@Stickpin How did you get dotPeek to look at dotCover's source? I've been itching to write an F# tool that trolls the current directory using a command line argument assembly filter and displays the list of excluded/included assemblies and this would make my dream come true!Ulbricht
K
4

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.

Kutzer answered 3/5, 2011 at 4:59 Comment(6)
Adam, thanks for your input. As stated in the question, I'd like to use the "Filters" section of the build step in TeamCity. I'll only resort to XML config if it's the last option (and how do one use dotCover XML config files with TeamCity?).Sanorasans
@Martin Ah, sorry, I misunderstood what you meant. It looks like stealf covered what you need in their answer. Hope that helps. :)Kutzer
ModuleMask, ClassMask and FunctionMask should be attributes, not elements. Additionally, the full namespace-qualified names must be specified in each mask. I also found out that you can easilly modify the file from VS by using the menu dotCover -> Edit Coverage Filters... command.Keeling
See Filtering with dotCover for details on using dotCover filters.Keeling
It's worth mentioning that going with this option means that you can experience the same results locally running dotCover and have your filtering in source control rather than being defined inside a CI tool, but that depends on your preference.Perverse
I can confirm that this works. I use <ModuleMask>*.Tests</ModuleMask> inside //Filters/ExcludeFilters/FilterEntry to exclude unit test assemblies.Sacristan
B
0

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.

Barbaraanne answered 6/5, 2011 at 12:57 Comment(1)
I've read the docs, but can't get it to work. A working sample would be great.Sanorasans

© 2022 - 2024 — McMap. All rights reserved.