How do I exclude service references from code coverage using the runsettings file in Visual Studio 2012?

Christopher Lazzaro picture Christopher Lazzaro · Nov 24, 2012 · Viewed 11.5k times · Source

I'm using a custom runsettings file to control what projects are inspected for code coverage. I used the default template provided by Microsoft and have so far been able to exclude the items I want with no issues. My next action is to exclude from code coverage the auto-generated web proxy classes that are created by Visual Studio when you add a service reference.

This seemed something that should work with the default runsettings template since it has a section that looks like this:

<Attributes>
    <Exclude>
        <!-- Don’t forget "Attribute" at the end of the name -->
        <Attribute>^System.Diagnostics.DebuggerHiddenAttribute$</Attribute>
        <Attribute>^System.Diagnostics.DebuggerNonUserCodeAttribute$</Attribute>
        <Attribute>^System.Runtime.CompilerServices.CompilerGeneratedAttribute$</Attribute>
        <Attribute>^System.CodeDom.Compiler.GeneratedCodeAttribute$</Attribute>
        <Attribute>^System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute$</Attribute>
    </Exclude>
</Attributes>

All classes created when the service reference is added are decorated with the GeneratedCodeAttribute so they should all be excluded. However, when I run code coverage they are not ignored so code coverage reports a large block of un-covered code. I've experimented with the regular expression several times in an attempt to get it to select the attribute correctly to no avail.

I'd appreciate suggestions on how to either: - get this attribute exclusion to work - an alternative that doesn't require me to exclude the entire project or that makes the runsettings file non-generic (we want to re-use this base file across all projects without specific edits)

FYI - while I understand there are other code coverage tools, my goal here is to make the Visual Studio one work, so suggestions about switching to another tool are not helpful to me in this case.

Answer

Hullah picture Hullah · May 17, 2013

It appears the issue is the periods in the RegEx. If you escape them as \. it starts working. Not sure why that matters since if it's truly a RegEx, the period should match any character including a period.

So to make the original template work, you'd change it to the following:

<Attributes>
    <Exclude>
        <Attribute>^System\.Diagnostics\.DebuggerHiddenAttribute$</Attribute>
        <Attribute>^System\.Diagnostics\.DebuggerNonUserCodeAttribute$</Attribute>
        <Attribute>^System\.Runtime\.CompilerServices\.CompilerGeneratedAttribute$</Attribute>
        <Attribute>^System\.CodeDom\.Compiler\.GeneratedCodeAttribute$</Attribute>
        <Attribute>^System\.Diagnostics\.CodeAnalysis\.ExcludeFromCodeCoverageAttribute$</Attribute>
    </Exclude>
</Attributes>

Also just to let you know, the <ModulePaths> filters have the same issue to which you could use:

<ModulePaths>
    <Include>
        <ModulePath>.*MyCompany\.Namespace\.Project\.dll$</ModulePath>
    </Include>
    <Exclude>
        <ModulePath>.*ThirdParty\.Namespace\.Project\.dll$</ModulePath>
    </Exclude>
</ModulePaths>