1

I have observed that the "ExcludeFromCodeCoverage" attribute is not supported within the Portable Class Library.

Is there any workaround for filtering code coverage for business logic within a dll?

Hence, I usually apply this attribute on property getters/setters and code behind files.

Scott Nimrod
  • 11,206
  • 11
  • 54
  • 118

3 Answers3

4

I found that if you add your own ExcludeFromCodeCoverageAttribute implementation that the VS coverage tool will obey it. However, you have to put it in the right namespace, too. 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
  {
  }
}
Travis Illig
  • 23,195
  • 2
  • 62
  • 85
  • This solution is nice and clean and does not require to fiddle with it like with [DebuggerNonUserCode] for debugging session... – jackomo Aug 23 '17 at 14:04
1

Referencing a similar question, I have swapped out the [ExcludeFromCodeCoverage] attribute with [DebuggerNonUserCode].

This works for me.

Community
  • 1
  • 1
Scott Nimrod
  • 11,206
  • 11
  • 54
  • 118
0

Usually Code Coverage libraries allow you to specify what attribute to exclude. So you could just create your own attribute and configure the coverage util to use that as its exclusion criteria.

Steven Mark Ford
  • 3,372
  • 21
  • 32