Export (0) Print
Expand All

How to: Exclude Code Sections from Code Coverage Using the ExcludeFromCodeCoverage Attribute

Visual Studio 2010

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.

NoteNote

ExcludeFromCodeCoverage is a member of the System.Diagnostics.CodeAnalysis namespace.

You can exclude the following members from your code coverage results:

  • Members

  • Exclusion Details

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

  1. Either create a new managed code project in Visual Studio or open an existing managed project.

  2. 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
    
    NoteNote

    See the following code sample for further details.

Excluding a method from inclusion in code coverage results

  1. Either create a new managed code project in Visual Studio or open an existing managed project.

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

  1. Either create a new managed code project in Visual Studio or open an existing managed project.

  2. 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;
                }
            }
        }
    }
    
    

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;
        }
    }

Community Additions

ADD
Show:
© 2015 Microsoft