How to Exclude Program.cs from Code Coverage in .NET 6

In the world of .NET development, code coverage is a crucial aspect of ensuring the quality and reliability of your software. It helps you identify areas of your codebase that are tested and those that are not. However, there are cases where you may want to exclude specific files or classes from code coverage analysis. One such scenario is excluding the Program.cs file in .NET 6, which doesn’t have a class declaration like other C# files. In this article, we will explore how you can achieve this and maintain accurate code coverage reports for your .NET 6 projects.

Understanding Code Coverage

Before we dive into excluding Program.cs, let’s briefly understand what code coverage is and why it matters. Code coverage is a metric that measures the percentage of your code that is executed during automated tests. It helps you identify untested code paths, reducing the risk of undiscovered bugs in your application. Tools like Coverlet and OpenCover are commonly used in the .NET ecosystem to calculate code coverage.

Excluding Files from Code Coverage

In earlier versions of .NET, excluding files from code coverage was relatively straightforward. You could apply the [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] attribute to the classes or methods you wanted to exclude, and the code coverage tool would skip those portions during analysis. However, when it comes to Program.cs in .NET 6, things are a bit different due to the introduction of top-level statements in C# 9 and 10.

Dealing with Program.cs in .NET 6

In .NET 6, the Program.cs file often contains the entry point of your application, but it doesn’t have a class declaration. This poses a challenge when you want to apply attributes like [ExcludeFromCodeCoverage], which are typically applied at the class level. So, how can you exclude Program.cs from code coverage in .NET 6?

One approach is to use the dotnet test command with specific parameters. You can instruct the code coverage tool to exclude Program.cs during the test run by using the /p:ExcludeByFile option. Here’s an example:

dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:ExcludeByFile="**/Program.cs"

In the above command:

  • /p:CollectCoverage=true enables code coverage collection.
  • /p:CoverletOutputFormat=cobertura specifies the output format for coverage results.
  • /p:ExcludeByFile="**/Program.cs" instructs the tool to exclude any file named Program.cs from code coverage analysis.

By using this command, you can effectively exclude Program.cs from your code coverage reports.

An Alternative Approach

If you prefer a more attribute-based approach, you can define Program.cs as a partial class in your Startup.cs file (or any other appropriate location) and then apply the [ExcludeFromCodeCoverage] attribute to it. Here’s an example:

[ExcludeFromCodeCoverage]
public partial class Program { }

This approach works because Program.cs is a partial class, and you can extend its declaration in another file. By adding the [ExcludeFromCodeCoverage] attribute to the partial class, you achieve the same result—excluding Program.cs from code coverage analysis.

Conclusion

Code coverage is an essential tool for assessing the quality of your .NET 6 projects. While excluding files like Program.cs from code coverage analysis may seem challenging due to its lack of class declaration, you have options. You can use the dotnet test command with the /p:ExcludeByFile option to exclude Program.cs during testing, or you can define Program.cs as a partial class and apply the [ExcludeFromCodeCoverage] attribute to it. Choose the approach that best fits your project’s requirements and coding style, and ensure your code coverage reports accurately reflect the tested portions of your codebase.

Bipul author of nerdy tutorial
Bipul

Hello my name is Bipul, I love write solution about programming languages.

Articles: 146

3 Comments

Leave a Reply

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