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

Chr*_*aro 12 unit-testing mstest code-coverage visual-studio-2012 visual-studio-test-runner

我正在使用自定义runsettings文件来控制检查代码覆盖率的项目.我使用了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>
Run Code Online (Sandbox Code Playgroud)

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

我很感激有关如何使用的建议: - 让这个属性排除工作 - 一个不需要我排除整个项目或使runsettings文件非泛型的替代方法(我们想重新使用这个基本文件在没有特定编辑的所有项目中)

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

Chr*_*aro 12

谢谢你的想法.我最后添加了这些行:

<Source>.*\\Service References\\.*</Source>
<Source>.*\\*.designer.cs*</Source>
Run Code Online (Sandbox Code Playgroud)

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


Hul*_*lah 12

问题似乎是RegEx中的期间.如果你\.开始工作时逃脱它们.不确定为什么这很重要,因为如果它真的是一个RegEx,那么这个时期应该与包括一个时期在内的任何角色相匹配.

因此,要使原始模板有效,您需要将其更改为以下内容:

<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>
Run Code Online (Sandbox Code Playgroud)

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

<ModulePaths>
    <Include>
        <ModulePath>.*MyCompany\.Namespace\.Project\.dll$</ModulePath>
    </Include>
    <Exclude>
        <ModulePath>.*ThirdParty\.Namespace\.Project\.dll$</ModulePath>
    </Exclude>
</ModulePaths>
Run Code Online (Sandbox Code Playgroud)


Ale*_*eck 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>
Run Code Online (Sandbox Code Playgroud)