Exclude class or method from code coverage in .net-core

I know that I can use [ExcludeFromCodeCoverage] to exclude code coverage in .Net framework 4.

Does anyone know if there is a way to exclude code coverage from the .dotnet kernel?

+4
source share
3 answers

I finally found a solution!

First of all, you need to use the .runsettings file to configure code coverage:

https://msdn.microsoft.com/en-us/library/jj159530.aspx

BUT the problem is that ExcludeFromCodeCoverageAttribute is sealed, so we cannot use it

https://github.com/dotnet/corefx/blob/93b277c12c129347b5d05de973fe00323ac37fbc/src/System.Diagnostics.Tools/src/System/Diagnostics/CodeAnalysis/ExcludeFromCodeCoverageAttribute

, ,

https://github.com/dotnet/corefx/issues/14488

GeneratedCodeAttribute, .runsettings. , :

https://msdn.microsoft.com/en-us/library/jj159530.aspx

+2

ExcludeFromCodeCoverage

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Interface)]
public class ExcludeFromCodeCoverageAttribute : Attribute
{
    public ExcludeFromCodeCoverageAttribute(string reason = null)
    {
        Reason = Reason;
    }

    public string Reason { get; set; }
}

.runsettings .

...
   <Configuration>
      <CodeCoverage>
        .
        .
        .
        <Attributes>
          <Exclude>
            <Attribute>^YourCompany\.YourNameSpace\.sExcludeFromCodeCoverageAttribute$</Attribute>
          </Exclude>
        </Attributes>
        .
        .
        .
    </Configuration>
...

.

0

Starting with .NET Core 2.0 you can use the attribute ExcludeFromCodeCoverage.

using System.Diagnostics.CodeAnalysis;

namespace YourNamespace
{
    [ExcludeFromCodeCoverage]
    public class YourClass
    {
        ...
    }
}

https://isscroberto.com/2019/07/11/net-core-exclude-from-coverage/

0
source

Source: https://habr.com/ru/post/1656999/


All Articles