C#: ExcludeFromCodeCoverage not functioning properly

SonarQube version: 9.9 LTS
Installed: zip


Users of our SonarQube instance have added the [ExcludeFromCodeCoverage] attribute to a method. SonarQube is excluding most of the lines of that specific method from coverage, but a few are marked as “Not Covered”.
I am aware that SQ is depend on the code coverage tool that is used. However, when i download the code coverage results i do see that that method is excluded.


Option 1: Code coverage results in Visual Studio when method is not excluded:

Option 2: Code coverage results in Visual Studio when method is excluded with the ExcludeFromCodeCoverage attribute:
As you can see, that specific method is no longer included.

In SonarQube we can see:

Option 1:
Here we see that the whole method is not covered, working as expected.

Option 2:
Here we can see that the method is mostly excluded, except for a few lines of code.
This is not what i would expect when i look at the code coverage results i opened in Visual Studio.


Why are 5 lines shown as not covered despite the rest of the method being excluded from coverage?

Hey there.

That’s definitely odd. I’m inclined to say there must still be something indicating coverage for these lines in the report (always easier to say it’s someone else’s fault :smiley:).

Can you provide the raw coverage report that is resulting in these results (I can open a PM channel for you to share them)?

Hello Colin,

Yes, i have the coveragexml available which i can send to you.
When looking at the module i do not see that specific method being mentioned at all. But maybe you have more experience reading them :slight_smile:

Colin found the lines being mentioned in the coverage report after all.
So its not SonarQube thats displaying this wrong, but in my opinion a bug in the coverage tool or test framework or something.

It is a lambda expression within the method that has the ExcludeFromCodeCoverage attribute that is causing this. Maybe because the lambda expression is not in scope for the ExcludeFromCodeCoverage because its actually an anoymous function?
Anyhow, not on SQ to fix :slight_smile:

1 Like

Thanks for the follow up @mcvermeulen!