-->

How do I exclude types and methods from being cove

2019-01-30 06:46发布

问题:

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.

回答1:

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.*


回答2:

Take a look at the dotCover Tips & Tricks 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.



回答3:

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.