Use attribute to omit code from coverage analysis in Visual Studio

tvanfosson picture tvanfosson · Aug 17, 2009 · Viewed 12.2k times · Source

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.

Answer

Shrike picture Shrike · Apr 7, 2010

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