.NET Core Exclude from Coverage

It’s great to have your code covered by tests, right? And if you’re like me, you’d like to know how much of your code gets covered, the higher this metric, the lower the chance of having undetected software bugs, assuming your tests are good quality, of course.

Sometimes you need to exclude some classes from this metric to get more accurate results according to your project specifics.

For a .NET Core project, the framework provides the attribute ExcludeFromCodeCoverage. Tag your class with it and make sure to add the import to System.Diagnostics.CodeAnalysis.

This attribute is available to .NET Core 2.0 or superior.

using System.Diagnostics.CodeAnalysis;
namespace YourNamespace
{
[ExcludeFromCodeCoverage]
public class YourClass
{
}
}

view raw
YourClass.cs
hosted with ❤ by GitHub

tired good night GIF

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s