C# ExcludeFromCodeCoverage on method scope not working

  • versions used: SonarQube 8.4.1
  • steps to reproduce
    Place ExcludeFromCodeCoverage above a method instead of above a class

Works:

[ExcludeFromCodeCoverage]
public class Foo 
{
    public void Bar()
    {
    }
}

Does not work:

public class Foo 
{
    [ExcludeFromCodeCoverage]
    public void Bar()
    {
    }
}

Anyone?

hi @Rouke.Broersma.IS. This has been discussed in this github issue:.

In short: please use the code coverage tool exclusion mechanisms when running the code coverage tool. If the coverage report will contain the entry, then our plugin will parse it and upload it to SonarQube.

I had found that issue but since we do not use coverlet and the excludefromcodecoverage attribute docs say that it works on method scope I assumed microsoft would honor their own attribute in their code coverage tooling… Apparently not. Thanks!

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

Hi @Rouke.Broersma.IS, indeed, from our experience it only works at class level. And it seems that this is what the MS docs specify:

Placing this attribute on a class or a structure excludes all the members of that class or structure from the collection of code coverage information.

The remark might say class or struct but in any case the implementation is wrong IMO because the attribute does not have a restriction on class or struct only. Either microsoft should restrict the allowed attribute usage and prevent a user from placing the attribute in the wrong places, or they should follow the attribute allowed usage in their code coverage tooling.

But, it is what it is. And it is not a problem with sonarqube.

1 Like