Sonar not excluding files with attribute [ExcludeFromCodeCoverage]


We are currently experiencing an interesting issue with the Sonarcloud. We noticed that for some classes even though an attribute for [ExcludeFromCodeCoverage] is set the objects are taken into account for the coverage calculation.

Here an example

We noticed that the attribute is working for classes but it seems not to work for records. Are we on the right track? Is this a SonarCloud issue or an issue with our CI pipeline.



Template for a good new topic, formatted with Markdown:

  • ALM used Azure DevOps
  • CI system used Azure DevOps
  • Languages of the repository: c#
  • Error observed (wrap logs/code around with triple quotes ``` for proper formatting)
  • Steps to reproduce:
dotnet test XXXX --configuration $(BuildConfiguration) --filter Type=UnitTest --logger "trx" /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:Exclude="[*.Tests]*" /p:ExcludeByAttribute=Obsolete /p:ExcludeByAttribute=ObsoleteAttribute /p:ExcludeByAttribute=GeneratedCodeAttribute /p:ExcludeByAttribute=CompilerGeneratedAttribute /p:ExcludeByAttribute=ExcludeFromCodeCoverage /p:ExcludeByAttribute=ExcludeFromCodeCoverageAttribute

Welcome to the community!

Would you be able to resolve this by applying the solution in this GitHub issue?

Hey @janos, thank you for the answer.

Unfortunately the suggestion in the github issue is not working. Sonar is still demanding the lines be covered even though I am setting the attribute.

Though when I am changing it the record to a class` then sonar correctly interprets the attribute and does not include it in the coverage calculation.

Could this be an issue with the Sonar tasks in Azure Devops?


Hello @dre - as you’ve seen in this answer, SonarCloud / SonarScanner only parses the OpenCover report. And:

If you add the parameter -excludebyattribute:*.ExcludeFromCodeCoverageAttribute to the opencover.exe command line in the repro build script you should find that the expected classes are excluded from the coverage report.

From what you say, when passing -excludebyattribute:*.ExcludeFromCodeCoverageAttribute to opencover.exe, OpenCover excludes only classes but not records from the code coverage report. Did you report this issue to Issues · OpenCover/opencover · GitHub ?


Thanks @Andrei_Epure. Sorry I missed it in the issue. This answer pointed me into the right direction.

The issue got actually fixed by updating Coverlet to the latest version. Based on this Issue: C# 9 record types show as a miss · Issue #992 · coverlet-coverage/coverlet · GitHub

Now it works. Once more thank you for your help!



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