57

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

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
Martin R-L
  • 4,039
  • 3
  • 28
  • 28

3 Answers3

69

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.*
Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
  • 9
    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.*` – bruceboughton Sep 30 '11 at 14:17
  • @bruceboughton 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! – C Bauer Sep 18 '15 at 19:31
3

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.

R. Schreurs
  • 8,587
  • 5
  • 43
  • 62
Adam Lear
  • 38,111
  • 12
  • 81
  • 101
  • 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?). – Martin R-L May 04 '11 at 06:21
  • @Martin Ah, sorry, I misunderstood what you meant. It looks like stealf covered what you need in their answer. Hope that helps. :) – Adam Lear May 06 '11 at 13:48
  • 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. – Marek Dzikiewicz May 02 '12 at 07:17
  • See [Filtering with dotCover](http://blogs.jetbrains.com/dotnet/2010/07/filtering-with-dotcover) for details on using dotCover filters. – Marek Dzikiewicz May 02 '12 at 07:51
  • 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. – Paulie Waulie Jul 03 '17 at 17:07
  • I can confirm that this works. I use `*.Tests` inside `//Filters/ExcludeFilters/FilterEntry` to exclude unit test assemblies. – R. Schreurs Apr 17 '20 at 08:54
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.

stealf
  • 1