【问题标题】:ExcludeFromCodeCoverage attribute is not supported within the Portable Class Library可移植类库中不支持 ExcludeFromCodeCoverage 属性
【发布时间】:2015-08-02 21:30:51
【问题描述】:

我观察到可移植类库不支持“ExcludeFromCodeCoverage”属性。

是否有任何解决方法可以过滤 dll 中业务逻辑的代码覆盖率?

因此,我通常将此属性应用于属性 getter/setter 和代码隐藏文件。

【问题讨论】:

    标签: unit-testing mstest visual-studio-2015


    【解决方案1】:

    我发现,如果您添加自己的 ExcludeFromCodeCoverageAttribute 实现,VS 覆盖工具将遵守它。但是,您也必须将它放在正确的命名空间中。 YMMV。

    namespace System.Diagnostics.CodeAnalysis
    {
      /// <summary>
      /// Specifies that the attributed code should be excluded from code coverage information.
      /// </summary>
      /// <remarks>
      /// This attribute was added to the assembly because it's not otherwise
      /// available to portable class libraries. Marked internal to avoid reuse
      /// outside this specific library.
      /// </remarks>
      [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Event, AllowMultiple = false, Inherited = false)]
      internal sealed class ExcludeFromCodeCoverageAttribute : Attribute
      {
      }
    }
    

    【讨论】:

    • 这个解决方案很好很干净,不需要像使用 [DebuggerNonUserCode] 调试会话那样摆弄它......
    【解决方案2】:

    引用类似的question,我已将 [ExcludeFromCodeCoverage] 属性替换为 [DebuggerNonUserCode]。

    这对我有用。

    【讨论】:

      【解决方案3】:

      通常代码覆盖率库允许您指定要排除的属性。因此,您可以创建自己的属性并配置覆盖工具以将其用作排除标准。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多