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

人盡茶涼 提交于 2020-01-02 00:58:31

问题


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.


回答1:


MSDN has a page which describes how to customize code coverage analysis here.

At the bottom of the page there is an example settings file which shows how to exclude attributes, and this matches what you have above.

You could try some of the other exclusion methods they mention, such as excluding by path:

<!-- Match the path of the source files in which each method is defined: -->
<Sources>
    <Exclude>
        <Source>.*\\atlmfc\\.*</Source>
        <Source>.*\\vctools\\.*</Source>
        <Source>.*\\public\\sdk\\.*</Source>
        <Source>.*\\microsoft sdks\\.*</Source>
        <Source>.*\\vc\\include\\.*</Source>
    </Exclude>
</Sources>



回答2:


Thanks for the idea. I ended up adding these lines:

<Source>.*\\Service References\\.*</Source>
<Source>.*\\*.designer.cs*</Source>

and got the results I needed. I'm still bummed that I don't know why the other portions of this file aren't being accepted.




回答3:


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>



回答4:


I was able to make this setting work by setting the attribute naming to:

<Attributes>
  <Exclude>
    <Attribute>.*GeneratedCodeAttribute$</Attribute>
  </Exclude>
</Attributes>

Not sure why, but there must be a part of the full attribute name that does not match the regular expression.



来源:https://stackoverflow.com/questions/13544918/how-do-i-exclude-service-references-from-code-coverage-using-the-runsettings-fil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!