【问题标题】:How do I exclude service references from code coverage using the runsettings file in Visual Studio 2012?如何使用 Visual Studio 2012 中的 runsettings 文件从代码覆盖范围中排除服务引用?
【发布时间】:2012-11-24 19:46:09
【问题描述】:

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

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

<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 能够正常工作,因此在这种情况下,关于切换到另一种工具的建议对我没有帮助。

【问题讨论】:

    标签: unit-testing visual-studio-2012 mstest code-coverage visual-studio-test-runner


    【解决方案1】:

    看来问题在于 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>
    

    还只是为了让您知道,&lt;ModulePaths&gt; 过滤器有同样的问题,您可以使用:

    <ModulePaths>
        <Include>
            <ModulePath>.*MyCompany\.Namespace\.Project\.dll$</ModulePath>
        </Include>
        <Exclude>
            <ModulePath>.*ThirdParty\.Namespace\.Project\.dll$</ModulePath>
        </Exclude>
    </ModulePaths>
    

    【讨论】:

      【解决方案2】:

      感谢您的想法。我最终添加了这些行:

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

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

      【讨论】:

      • 这个答案对我有用。特别是关于服务引用的第一行。另一个注意事项是确保您遵循此链接开头的所有说明(步骤 1-5)msdn.microsoft.com/en-us/library/jj159530.aspx。最初我没有意识到您必须在 TEST 菜单下选择您的测试设置文件。
      【解决方案3】:

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

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

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

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

      【讨论】:

        【解决方案4】:

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

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

        不知道为什么,但是完整属性名中肯定有一部分与正则表达式不匹配。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-01-05
          • 1970-01-01
          • 1970-01-01
          • 2022-10-21
          • 2018-12-04
          • 1970-01-01
          • 2016-03-29
          • 2011-03-13
          相关资源
          最近更新 更多