Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
You can exclude classes, methods, properties and events within your managed Visual C#, Visual Basic, Managed C++ or F# code projects from being included in code coverage by using the ExcludeFromCodeCoverage attribute.
Some of the ways in which excluding code from code coverage by using the ExcludeFromCodeCoverage attribute can be useful include the following:
Excluding the refactoring of proven stable legacy code.
Excluding any code owned by another developer or development team that might impact you or your team's code coverage statistics.
Excluding compiler- and tool-generated code.
Note
ExcludeFromCodeCoverage is a member of the System.Diagnostics.CodeAnalysis namespace.
You can exclude the following members from your code coverage results:
|
|
---|---|
Class |
Can exclude private, public, internal or static classes Note Classes that inherit from an excluded class are not excluded. |
Method |
Can exclude private, public, internal or static methods |
Struct |
Can exclude private, public and internal structs |
Property |
Can exclude the get property, the set property or the entire property |
Indexer |
Can exclude the get, the set or the entire indexer |
Event |
Can exclude the add event, the remove event, or the entire event |
You cannot exclude the following:
Namespace
Variable
Empty Space
Excluding a class from inclusion in code coverage results
Either create a new managed code project in Visual Studio or open an existing managed project.
Locate a class that you want to exclude from code coverage and add the ExcludeFromCodeCoverage attribute above it.
In this C# sample, the entire class will be excluded from code coverage.
[ExcludeFromCodeCoverage] class myClass
Note
See the following code sample for further details.
Excluding a method from inclusion in code coverage results
Either create a new managed code project in Visual Studio or open an existing managed project.
Locate a method within a class that you want to exclude from code coverage and add the ExcludeFromCodeCoverage attribute above it.
In this C# sample, the addTwoNumbers method will be excluded from code coverage.
class simpleMath { [ExcludeFromCodeCoverage] public int addTwoNumbers(int firstNumber, int secondNumber) { int total = firstNumber + secondNumber; return total; }
Excluding a class property from inclusion in code coverage results
Either create a new managed code project in Visual Studio or open an existing managed project.
Locate a property within a class that you want to exclude from code coverage and add the ExcludeFromCodeCoverage attribute above it.
In this C# sample, the set accessor code for the property is being excluded from code coverage. You could also choose to exclude either the entire property or the get accessor.
public class Date { private int month = 7; // Backing store public int Month { get { return month; } [ExcludeFromCodeCoverage] set { if ((value > 0) && (value < 13)) { month = value; } } } }
Example
The following code demonstrates a class that will be excluded from code coverage as a result of using the [ExcludeFromCodeCoverage] attribute.
[ExcludeFromCodeCoverage]
class simpleMath
{
public int addTwoNumbers(int firstNumber, int secondNumber)
{
int total = firstNumber + secondNumber;
return total;
}
public int subtractTwoNumbers(int firstNumber, int secondNumber)
{
int total = firstNumber - secondNumber;
return total;
}
}
See Also
Other Resources
Using Code Coverage to Determine How Much Code Is Being Tested