DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Does 100% Code Coverage Mean Tested?
  • Beyond Code Coverage: A Risk-Driven Revolution in Software Testing With Machine Learning
  • Creating a Web Project: Refactoring
  • Enhancing Testing Efficiency: Transitioning From Traditional Code Coverage to Code Change Coverage

Trending

  • The SDLC After AI
  • Adding Graph Retrieval to a RAG Pipeline With Python
  • Build a Voice Assistant With Gemini Live API
  • Testing Java and Spring Applications With Kotlin: Low-Risk to Adoption

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Software Design and Architecture
  3. Performance
  4. How to Exclude Code From Code Coverage

How to Exclude Code From Code Coverage

This blog post focuses on how to leave out all code that will not be covered with unit tests from code coverage and correctly display the numbers shown in code coverage reports.

By 
Gunnar Peipman
·
Mar. 06, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
39.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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 all code that will not be covered with unit tests from code coverage 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. The usual candidates are primitive models and Data Transfer Objects (DTO). One example is given below: 

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 the ExcludeFromCodeCoverage attribute. This attribute tells tooling that a class or some of its members are not planned to be covered with the tests. TheEditFormModel class shown above can be left out from code coverage by simply adding this attribute.

TheExcludeFromCodeCoverage attribute works also on the class member level.

ExcludeFromCodeCoverage Attribute in Action

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

Code coverage

Now I add theExcludeFromCodeCoverate attribute to all classes and members that don't need testing. After this, I generate a new report.

Code coverage report

I left out a load of primitive models, DTOs, 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 a small web application.

Should I Use the 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. The reports above were generated for a relatively small web application and the change in numbers was pretty big. I don't want to say that the same effects appear with every application but there will be changes in numbers.

Over time, these metrics may be an 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. In .NET we can use the ExcludeFromCodeCoverage attribute to leave out whole classes and structs or members like methods, properties, constructors, and events. As example reports show, corrections to code coverage numbers can be significant.

Code coverage

Published at DZone with permission of Gunnar Peipman. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Does 100% Code Coverage Mean Tested?
  • Beyond Code Coverage: A Risk-Driven Revolution in Software Testing With Machine Learning
  • Creating a Web Project: Refactoring
  • Enhancing Testing Efficiency: Transitioning From Traditional Code Coverage to Code Change Coverage

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook