How to exclude code from code coverage

Recently I blogged about how to generate nice code coverage reports for ASP.NET Core and .NET Core applications. This blog post focuses on how to leave out from code coverage all code that will not be covered with unit tests and get numbers shown on code coverage reports correct.

What to exclude from code coverage?

I think almost all applications have some classes we don’t want to test. Usual candidates are primitive models and Data Transfer Objects (DTO). One example is here.

public class EditFolderModel
{
    public int Id { get; set; }
    public string Title { get; set; }
    public int? parentFolderId { get; set; }
}

Without any additional information these classes will be part of code coverage calculations.

Excluding code from code coverage

The easiest way to exclude code from code coverage analysis is to use ExcludeFromCodeCoverage attribute. This attribute tells tooling that class or some of its members are not planned to be covered with tests. EditFormModel class shown above can be left out from code coverage by simply adding the attribute.

[ExcludeFromCodeCoverage]
public class EditFolderModel
{
    public int Id { get; set; }
    public string Title { get; set; }
    public int? parentFolderId { get; set; }
}

ExcludeFromCodeCoverage attribute works also on class member level.

public class ErrorViewModel
{
    [ExcludeFromCodeCoverage]
    public string RequestId { get; set; }

    public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
}

ExcludeFromCodeCoverage attribute in action

I take my simple demo application and generate code coverage report for it. There’s no code excluded – everything is counted in.

Code coverage report: No ExcludeFromCodeCoverage attributes applied

Now I add ExcludeFromCodeCoverate attribute to all classes and members that doesn’t need testing. After this I generate new report.

Code coverage report: Using ExcludeFromCodeCoverage attributes

I left out load of primitive models, DTO-s, constructors and scaffolded ASP.NET Identity code-behind files. As a result the numbers on my code coverage report changed around 50 percent and that’s huge even for small web application.

Should I use ExcludeFromCodeCoverage attribute?

I think it makes a lot of sense to exclude from code coverage reports the code that will never be covered with tests by us. Reports above were generated to relatively small web application and change in numbers was pretty big. I don’t want to say that same effects appear with every application but there will be changes in numbers.

Over time these metrics may be important part of estimating the need for unit tests. If these numbers lie then the work ahead may seem way bigger than it actually is. This is why it is important to keep these metrics correct.

Wrapping up

Although code coverage reports are easy to set up there’s still some work needed to get numbers right. On .NET we can use ExcudeFromCodeCoverage attribute to leave out whole classes and structs or members like methods, properties, constructors and events. As example reports show then corrections to code coverage numbers can be significant.

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    Leave a Reply

    Your email address will not be published. Required fields are marked *