[ACCEPTED]-Use attribute to omit code from coverage analysis in Visual Studio-ncover

Accepted answer
Score: 32

Starting with VS2010 we have ExcludeFromCodeCoverageAttribute. Commenters 5 have noted this to work in NCover as well 4 as DotCover + NUnit. Example usage:

public class myUntestableClass
{ }

Also 3 see this link. They suggest using VSInstr as 2 command line tool, it have /EXCLUDE options 1 (it's not as handy).

Score: 7

I've found some information on a couple of Diagnostics attributes 13 DebuggerNonUserCodeAttribute and DebuggerHiddenAttribute that indicates that using these attributes 12 will cause the coverage analyzer in VS to 11 leave these out of its results. I've tried 10 it with the DebuggerNonUserCodeAttribute 9 and it seems to work. I can probably live 8 with this for most of the classes that I'm 7 thinking of, though I don't like the side 6 effect of not being able to step into these 5 classes. That shouldn't be a problem for 4 the wrapper classes, but it may end up being 3 so with classes that are inherently hard 2 to test and I need debugger access to.

I'm 1 still looking for alternatives, though.

Score: 5

With NCover you can create an attribute 11 and then tell NCover to ignore that attribute.

In 10 our projects, we have defined this attribute 9 (no namespace, so it is easy to use):

public class CoverageExcludeAttribute : Attribute { }

We 8 use NAnt, so we have a target that looks 7 like this:

<target name="unittests" description="run nunit tests" >        

Question 9 in the NCover FAQ describes 6 this method. We based our solution on this.

Alternatively, you 5 can use the exclude feature of the NCoverExplorer 4 to exclude namespaces and assemblies from 3 the final report. This merely removes the 2 data from the report, but the end result 1 is the same.

We use both techniques.

Score: 0

This work for me! 👍

use in .csproj: sonar keys

    <SonarQubeSetting Include="sonar.issue.ignore.allfile">


    <SonarQubeSetting Include="sonar.coverage.exclusions">

And then 1 use in your class file .cs by ExcludeFromCodeCoverage from microsoft

public class ExcludeMeFromSonarCoverage
    public ExcludeMeFromSonarCoverage()

More Related questions