Code coverage - attribute driven exclusion

We are new to using the PR gateway checks.

We have some code that is failing the new code test coverage.

The code is temporary (yes it really is…) and we want to exclude it from the checks.

I am really struggling to find any support around attribute driven exclusions - or any support at all really for !java users…

We can exclude this check with a file exclusion in the admin portal, but I’d much prefer an attribute driven approach. so the exclusion lives with the code.

Can you advise? I have seen suggestions that ExcludeFromCodeCoverageAttribute is supported and other discussions that it isn’t.

I have also seen suggestion that you can supress stuff - java examples - but that seems to be around other warnings not code test coverage.

help please!

Hey there.

SonarCloud relies on the coverage report being generated to know what lines can be covered by tests (which lines are “executable”), and also which are excluded.

SonarCloud won’t do any post-processing of code (looking for the ExcludeFromCodeCoverageAttribute class), but it will listen to the coverage report.

You might have to explicitly tell your coverage tool to recognize this attribute (for example, for OpenCover requires adding -excludebyattribute:*.ExcludeFromCodeCoverageAttribute to the opencover.exe command line.

So my advice is to make sure everything is :100: in the coverage report you’re generating (coverage is being excluded as you expect). If somehow coverage is still getting reported in SonarCloud after, come back to us!

1 Like