Use attribute to omit code from coverage analysis in Visual Studio
Asked Answered
R

4

23

I have some classes that, for one reason or another, cannot be or need not be unit tested. I'd like to exclude these classes from my coverage metrics so that I can get a better feel for the coverage on the classes I actually care about. Right now I have to exclude the results after the fact. What I would like to do is use an attribute to mark those classes as excluded so that they aren't included to begin with. Is there any way to decorate a class with an attribute that will automatically exclude it from coverage analysis? Either VS coverage analysis or nCover will work.

FWIW, these are classes that I can assure myself by inspection that the code is correct. Mostly they are wrapper classes around existing framework classes that I've introduced in order to be able to mock the framework class out. Since the wrapper's get mocked out they don't get tested. That's ok because all they do is wrap the framework class' methods that I care about.

Remonstrance answered 17/8, 2009 at 17:54 Comment(2)
Is the problem that you can't get coverage data on invidual methods, so you want to remove coverage data on methods you don't care about to get more appropriate measure on the whole?Jonahjonas
Yes. There are some methods that don't need to be or cannot be easily tested -- wrapper classes created to enable unit testing around framework classes for example. I'd prefer to not have these mess up the statistics for the classes I do care about.Remonstrance
T
38

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

[ExcludeFromCodeCoverage]
public class myUntestableClass
{ }

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

Taraxacum answered 7/4, 2010 at 21:15 Comment(2)
Yes it works with dotcover and vs code analysis as well.Reagent
I'm using a VS extension called Fine Code Coverage and this worked for that as well.Longwood
R
7

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

I'm still looking for alternatives, though.

Remonstrance answered 17/8, 2009 at 18:13 Comment(1)
This is exactly what I was looking for. Thanks for following up!Ferrocyanide
R
6

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

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

public class CoverageExcludeAttribute : Attribute { }

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

<target name="unittests" description="run nunit tests" >        
    <ncover
        ....
        excludeAttributes="CoverageExcludeAttribute"
    />
</target>

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

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

We use both techniques.

Rathbun answered 19/8, 2009 at 23:25 Comment(2)
Thanks. That's exactly what I was looking for. I mostly use nCover in conjunction with TestDriven.Net which looks like it enables the CoverageExcludeAttribute out of the box.Remonstrance
The newest version works with the ExcludeFromCodeCoverage attribute, so I've switched to use that.Remonstrance
G
0

This work for me! 👍

use in .csproj: sonar keys

 <ItemGroup>
    <SonarQubeSetting Include="sonar.issue.ignore.allfile">
        <Value>ExcludeFromCodeCoverage</Value>
    </SonarQubeSetting>
</ItemGroup>

or

 <ItemGroup>
    <SonarQubeSetting Include="sonar.coverage.exclusions">
        <Value>**/FileClassToExclude.cs</Value>
    </SonarQubeSetting>
</ItemGroup>

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

[ExcludeFromCodeCoverage]
public class ExcludeMeFromSonarCoverage
{
    public ExcludeMeFromSonarCoverage()
    {
    }
}
Gertie answered 26/5, 2022 at 18:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.