C# ExcludedFromCodeCoverage attribute not honor in SonarCloud

In our source code we excluded some methods from CodeCoverage with the ExcludedFromCodeCoverage attribute. These methods only call other classes through there interfaces, so there is no logic in them.

However SonarCloud doesn’t recognize the ExcludedFromCodeCoverage attribute and causes our Quality gate to fail on the rule 30% CodeCoverage on new lines of Code.

How do we solve this, or is this a bug in SonarCloud?


Could you please read the discussion in sonar-dotnet issue #1421 and see if it’s related? In short, our plugin works at file-level exclusions, and you’ll need to use your code coverage tool exclusion mechanism to do that at more granular level - see this answer from the #1421 thread.

Hi @Andrei_Epure

The issue you mention is quit related, the only difference is that the use OpenCover for there analysis, while we use the Visual Studio test task in Azure DevOps. I will futher investigate why these results ignore or not handle the ExcludedFromCodeCoverage attribute correctly.

1 Like

did you make progress on that, @DVB?

We see that it is excluded with the “vs test runner”, but the code in de results is marked as 0% covered, which results back to SonarCloud Code Coverage results and the you don’t pass the quality gate coverage.

Sorry for the delay, @DVB , I opened a ticket #2791 to investigate this problem

Hi @DVB - I added the [ExcludeFromCodeCoverage] attribute to some of our files as well and the SonarCloud analysis correctly excludes those files from code coverage

therefore I closed the investigation ticket #2791