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?
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?
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/
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
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:
Create your own ExcludeFromCodeCoverage attribute
[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; }
}
Then add it to your .runsettings file and exclude it.
...
<Configuration>
<CodeCoverage>
.
.
.
<Attributes>
<Exclude>
<Attribute>^YourCompany\.YourNameSpace\.sExcludeFromCodeCoverageAttribute$</Attribute>
</Exclude>
</Attributes>
.
.
.
</Configuration>
...
Don't forget to select your run setting file when analyzing the code coverage.