mu88
(mu88)
December 3, 2022, 4:30pm
1
Dear community
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
1 Like
Colin
(Colin)
December 6, 2022, 3:11pm
2
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
mu88
(mu88)
December 7, 2022, 7:08am
3
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>
mu88
(mu88)
December 13, 2022, 8:15am
4
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.
Colin
(Colin)
December 13, 2022, 9:13am
5
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
mu88
(mu88)
December 13, 2022, 10:16am
6
Meaning I should add src/WebApp/Program.cs
to sonar.coverage.exclusions
or sonar.exclusions
?
mu88
(mu88)
December 13, 2022, 12:22pm
7
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>
system
(system)
Closed
December 20, 2022, 12:22pm
8
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.