我正在使用自定义运行设置文件来控制检查哪些项目的代码覆盖率。我使用了 Microsoft 提供的默认模板,到目前为止已经能够毫无问题地排除我想要的项目。我的下一个操作是从代码覆盖率中排除添加服务引用时由 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 成为一个工作,因此在这种情况下,关于切换到另一个工具的建议对我没有帮助。
最佳答案
看来问题出在 RegEx 中的句点。如果您将它们转义为 \.
它开始工作。不知道为什么这很重要,因为如果它真的是一个 RegEx,句点应该匹配 any character包括一个时期。
因此,要使原始模板正常工作,您需要将其更改为以下内容:
<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>
关于unit-testing - 如何使用 Visual Studio 2012 中的 runsettings 文件从代码覆盖率中排除服务引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13544918/