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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Creating a Web Project: Refactoring
  • Enhancing Testing Efficiency: Transitioning From Traditional Code Coverage to Code Change Coverage
  • How To Implement Code Reviews Into Your DevOps Practice
  • Improve Efficiency With Smaller Code Reviews

Trending

  • Performance Optimization Techniques for Snowflake on AWS
  • Build an MCP Server Using Go to Connect AI Agents With Databases
  • Accelerating AI Inference With TensorRT
  • Teradata Performance and Skew Prevention Tips
  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 user avatar
Gunnar Peipman
·
Mar. 06, 19 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
39.1K Views

Join the DZone community and get the full member experience.

Join For Free

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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Creating a Web Project: Refactoring
  • Enhancing Testing Efficiency: Transitioning From Traditional Code Coverage to Code Change Coverage
  • How To Implement Code Reviews Into Your DevOps Practice
  • Improve Efficiency With Smaller Code Reviews

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!