如何使用Visual Studio 2012中的runsettings文件从代码覆盖范围中排除服务引用?

问题描述:

我正在使用自定义运行设置文件来控制检查哪些项目的代码覆盖率.我使用了Microsoft提供的默认模板,到目前为止,我可以毫无问题地排除我想要的项目.我的下一个动作是从代码覆盖范围中排除添加服务引用时由Visual Studio创建的自动生成的Web代理类.

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.

这似乎应该与默认的runsettings模板一起使用,因为它具有如下所示的部分:

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>

添加服务引用时创建的所有类均以GeneratedCodeAttribute装饰,因此应将它们全部排除.但是,当我运行代码覆盖率时,它们不会被忽略,因此代码覆盖率会报告大量未发现的代码.我已经多次尝试使用正则表达式,以尝试使其正确地选择属性而无济于事.

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.

对于以下任一方法,我将不胜感激: -使该属性排除起作用 -一种不需要我排除整个项目或使runsettings文件非通用的替代方法(我们希望在没有特定编辑的情况下在所有项目中重用此基本文件)

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)

仅供参考-虽然我了解还有其他代码覆盖工具,但我的目标是使Visual Studio能够正常工作,因此在这种情况下,切换到另一种工具的建议对我没有帮助.

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.

MSDN上的页面描述了如何自定义代码覆盖率分析

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>