Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 256
Christopher Lazzaro Avatar asked Nov 24 '12 19:11

Christopher Lazzaro


People also ask

How do I exclude files from code coverage in Visual Studio?

To exclude test code from the code coverage results and only include application code, add the ExcludeFromCodeCoverageAttribute attribute to your test class. To include assemblies that aren't part of your solution, obtain the . pdb files for these assemblies and copy them into the same folder as the assembly .

How do you exclude files from code coverage?

Ignore Code Coverage You can prevent some files from being taken into account for code coverage by unit tests. To do so, go to Project Settings > General Settings > Analysis Scope > Code Coverage and set the Coverage Exclusions property. See the Patterns section for more details on the syntax.

How do I remove code coverage highlighting in Visual Studio?

You can easily switch highlighting on and off by pressing Ctrl + Alt + K , H .


1 Answers

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>
like image 167
Hullah Avatar answered Sep 18 '22 20:09

Hullah