Ignore file in C# coverage

Dear community :wave:t2:

I have an NUnit test project targeting .NET 7 using Coverlet for calculating the code coverage. I want to exclude the class Program (see here) from coverage calculation. Since I’m using minimal API, I had to use the following tweak:

[ExcludeFromCodeCoverage]
public partial class Program
{
}

This works fine when using dotCover in my IDE.

However, when calculating the coverage for SonarCloud via dotnet-coverage (as described here), the class is not excluded anymore.

So I’ve created the settings file dotnet-coverage-settings.xml (see here) with the following content (according to the Microsoft docs):

<?xml version="1.0" encoding="utf-8"?>
<Configuration>
    <CodeCoverage>
        <Sources>
            <Exclude>
                <Source>src/WebApp/Program.cs</Source>
            </Exclude>
        </Sources>
    </CodeCoverage>
</Configuration>

But SonarCloud still complains that the file has 0% coverage, so the exclusion doesn’t seem to work.

Since I’m not really bound to using dotnet-coverage, any other solution would be appreciated as well :slight_smile:

1 Like

Hey there.

Considering the first option (using [ExcludeFromCodeCoverage] – it might just be a case of configuring dotnet-coverage to respect the attribute. Take a peak at this: https://learn.microsoft.com/en-us/dotnet/core/additional-tools/dotnet-coverage#settings

Hi @Colin

Thanks for your reply!

Unfortunately, it doesn’t work with the following file either:

<?xml version="1.0" encoding="utf-8"?>
<Configuration>
    <CodeCoverage>
        <Attributes>
            <Exclude>
                <Attribute>^System\.Diagnostics\.DebuggerHiddenAttribute$</Attribute>
                <Attribute>^System\.Diagnostics\.DebuggerNonUserCodeAttribute$</Attribute>
                <Attribute>^System\.CodeDom\.Compiler\.GeneratedCodeAttribute$</Attribute>
                <Attribute>^System\.Diagnostics\.CodeAnalysis\.ExcludeFromCodeCoverageAttribute$</Attribute>
            </Exclude>
        </Attributes>
        <Sources>
            <Exclude>
                <Source>src/WebApp/Program.cs</Source>
            </Exclude>
        </Sources>
    </CodeCoverage>
</Configuration>

please see here, @Colin: according to the analysis, it seems to be a Sonar issue. I’d be happy if you could provide a workaround.

Hey there.

It looks like the file isn’t represented at all in the coverage report, (there is no Program.cs represented in a <source_files> block) which means that SonarQube assumes coverage is 0 for that file.

In this case, you would need to set coverage exclusions by narrowing the focus.

1 Like

Meaning I should add src/WebApp/Program.cs to sonar.coverage.exclusions or sonar.exclusions?

I solved it with SonarQube.Analysis.xml being like this:

<?xml version="1.0" encoding="utf-8" ?>
<SonarQubeAnalysisProperties  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.sonarsource.com/msbuild/integration/2015/1">
  <Property Name="sonar.coverage.exclusions">**/WebApp/Program.cs</Property>
</SonarQubeAnalysisProperties>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.