Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

.net-core

I know i was able to use [ExcludeFromCodeCoverage] to exclude code coverage in .Net framework 4.

Does anyone know if there's a way i can exclude code coverage from .dotnet core?

like image 489
kepung Avatar asked Oct 06 '16 23:10

kepung


2 Answers

Since .NET Core 2.0 you can use ExcludeFromCodeCoverage attribute.

using System.Diagnostics.CodeAnalysis;

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

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

like image 150
Roberto Orozco Avatar answered Oct 07 '22 20:10

Roberto Orozco


I finally found solution!

First of all you need to use .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 can't use it

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

I posted question here and looks like it will be solved in next release

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

FOR NOW I use GeneratedCodeAttribute as mentioned in .runsettings example. You can also use your custom attribute or any other exclude rule like here:

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

like image 25
Vladimir Rodchenko Avatar answered Oct 07 '22 21:10

Vladimir Rodchenko