Using Attributes to Exclude Code from Coverage in TestDriven.Net

There are times when you don’t want your coverage level penalized for code not covered. Often this occurs with code that is produced from a code generator. Since TestDriven.Net invokes NCover with the //ea CoverageExcludeAttribute switch, it’s relatively easy remove code from the coverage statistics.

First, define the CoverageExclude attribute. Make sure to declare it outside of any namespace scope (I made this mistake and struggled for 10 minutes to figure out why it didn’t work). It’s best to define the attribute in your non-test code to reduce couplings between assemblies. The attribute requires no methods beyond the default constructor.

public class CoverageExcludeAttribute : System.Attribute { }

Then decorate the modules, classes, methods and properties you want to exclude.

public class SomeClass { }

The next time you run TestDriven.Net and select coverage, the decorated code will be excluded from coverage.

You can capture more information about who excluded the code and why by doing the following:

public class CoverageExcludeAttribute : System.Attribute
    public string Author { get; private set; }
    public string Reason { get; private set; }

    public CoverageExcludeAttribute(string author, string reason)
        Author = author;
        Reason = reason;

(Don’t you just love auto implemented properties?)

Use it as follows:

[CoverageExclude("Mike", "I not sure yet")]
public class SomeClass { }

If you want reports produced by NCoverExplorer to report these exclusions, you can use the techniques documented here.

← newer older →
.Net, Technology, Life, Whatever

Recent Posts

Tweetz 2.0.0 Released
Tweetz 2.0 Beta
VSColorOutput 2.7 - Time Stamps
Fixed Focal-Length Eyeglasses, a Programmer's Best Friend
How to Choose the Right VPN Service
Two Handy Command Line Scripts
Tweetz 1.3.2
More... (1088)