便携式 Class 库不支持 ExcludeFromCodeCoverage 属性

ExcludeFromCodeCoverage attribute is not supported within the Portable Class Library

我发现便携式 Class 库不支持 "ExcludeFromCodeCoverage" 属性。

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

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

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

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

这对我有用。

我发现如果您添加自己的 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
  {
  }
}