Exclude Program.cs/top-level program from coverage

See original GitHub issue

Description

I have the top-level ASP.NET Core program src/WebApp/Program.cs using minimal APIs (see here). When calculating the coverage using dotnet-coverage collect 'dotnet test --no-build' -s 'src/Tests/dotnet-coverage-settings.xml' -f xml -o 'coverage.xml' (see here), this code is not excluded from coverage, although I’ve configured it in src/Tests/dotnet-coverage-settings.xml:

<?xml version="1.0" encoding="utf-8"?>
<Configuration>
    <CodeCoverage>
        <Attributes>
            <Exclude>
            <!-- Don't forget "Attribute" at the end of the name -->
                <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>

In Coverlet I could resolve this with the following hack (see here):

[ExcludeFromCodeCoverage]
public partial class Program
{
}

But this doesn’t seem to work with dotnet-coverage.

Environment

  • .NET SDK → 7.0.100
  • dotnet-coverage17.5.0
  • Running as GitHub Action (see here)

Issue Analytics

  • State:closed
  • Created 9 months ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mu88commented, Dec 13, 2022

Okay, thx for your help! I’ll ping Sonar again (see here) and close this issue in the meantime.

1reaction
jakubch1commented, Dec 13, 2022

It looks good in report. Methods are excluded:

<skipped_function id="19052" original_id="19052" token="0x60000d7" name="MoveNext()" type_name="Program.&lt;&lt;&lt;Main&gt;$&gt;g__CreateDbIfNotExistsAsync|0_4&gt;d" reason="attribute_excluded" />
        <skipped_function id="19496" original_id="19496" token="0x60000da" name="MoveNext()" type_name="Program.&lt;&lt;Main&gt;$&gt;d__0" reason="attribute_excluded" />
        <skipped_function id="20280" original_id="20280" token="0x60000de" name="&lt;&lt;Main&gt;$&gt;b__0_0(Microsoft.Extensions.Hosting.HostBuilderContext, System.IServiceProvider, Serilog.LoggerConfiguration)" type_name="Program.&lt;&gt;c" reason="attribute_excluded" />
        <skipped_function id="20446" original_id="20446" token="0x60000df" name="&lt;&lt;Main&gt;$&gt;b__0_1(Microsoft.AspNetCore.Components.Server.CircuitOptions)" type_name="Program.&lt;&gt;c" reason="attribute_excluded" />
        <skipped_function id="20500" original_id="20500" token="0x60000e0" name="&lt;&lt;Main&gt;$&gt;b__0_2(AutoMapper.IMapperConfigurationExpression)" type_name="Program.&lt;&gt;c" reason="attribute_excluded" />
        <skipped_function id="20520" original_id="20520" token="0x60000e1" name="&lt;&lt;Main&gt;$&gt;b__0_3(Microsoft.AspNetCore.Builder.RequestLocalizationOptions)" type_name="Program.&lt;&gt;c" reason="attribute_excluded" />

Could you please upload during run and provide coverage report from github actions?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Excluding Program.cs from code coverage in .net 6
To exclude my code from code coverage in a .net core webapi project, I apply the [System.Diagnostics.Analysis.ExcludeFromCodeCoverage] attribute ...
Read more >
Untitled
Exclude program.cs from code coverage Configuring a Project to Exclude Certain ... zeros of a polynomial Exclude Program.cs/top-level program from coverage ...
Read more >
How to exclude 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 ...
Read more >
Customizing Code Coverage Analysis - Visual Studio
Learn how to use the ExcludeFromCodeCoverageAttribute attribute to exclude test code from coverage results. You can include assemblies ...
Read more >
Exclude Nodes from Coverage Snapshot - dotCover
When a coverage snapshot is created, you can exclude specific items from its current presentation. As soon as you do this, dotCover will ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found