13

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

这似乎应该与默认的 runsettings 模板一起使用,因为它有一个看起来像这样的部分:

<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 修饰,因此应将它们全部排除。但是,当我运行代码覆盖率时,它们不会被忽略,因此代码覆盖率会报告一大块未覆盖的代码。我已经多次尝试使用正则表达式,试图让它正确选择属性,但无济于事。

我很感激有关如何: - 让这个属性排除工作 - 一个不需要我排除整个项目或使 runsettings 文件非通用的替代方案(我们想重新使用这个基本文件跨所有项目,无需特定编辑)

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

4

4 回答 4

13

看来问题是正则表达式中的句点。如果你在\.它开始工作时逃脱它们。不知道为什么这很重要,因为如果它真的是正则表达式,句点应该匹配任何字符,包括句点。

因此,要使原始模板正常工作,您需要将其更改为以下内容:

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

也只是为了让您知道,<ModulePaths>过滤器具有您可以使用的相同问题:

<ModulePaths>
    <Include>
        <ModulePath>.*MyCompany\.Namespace\.Project\.dll$</ModulePath>
    </Include>
    <Exclude>
        <ModulePath>.*ThirdParty\.Namespace\.Project\.dll$</ModulePath>
    </Exclude>
</ModulePaths>
于 2013-05-17T19:32:10.753 回答
12

谢谢你的主意。我最终添加了这些行:

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

并得到了我需要的结果。我仍然很沮丧,我不知道为什么这个文件的其他部分没有被接受。

于 2012-11-26T06:21:01.447 回答
4

MSDN 有一个页面描述了如何在此处自定义代码覆盖率分析。

在页面底部有一个示例设置文件,它显示了如何排除属性,这与您上面的内容相匹配。

您可以尝试他们提到的其他一些排除方法,例如按路径排除:

<!-- 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>
于 2012-11-24T21:22:32.523 回答
4

我可以通过将属性命名设置为:

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

不知道为什么,但必须有一部分完整的属性名称与正则表达式不匹配。

于 2013-03-21T13:38:31.210 回答