How to Use a File to Exclude Files From Code Coverage

Template for a good new topic, formatted with Markdown:

  • ALM used - Azure DevOps
  • CI system used – Azure DevOps
  • Languages of the repository – C#

How do you add a file to our solution that will tell SonarCloud to skip files listed in it when determining code coverage?

Hey there.

  1. You can set coverage exclusions in the SonarCloud UI.
  2. You can also add sonar.coverage.exclusions as an analysis parameter added to your scanner command
  3. or set per-project analysis parameters in a .csproj file.
1 Like

Thank you. We successfully used #1.

For #2, it looks like this is for SonarQube instead of SonarCloud. Is it possible to use this approach with SonarCloud?

For #3, We have a way to include and exclude projects.

1 Like

Hey there.

What makes you say that? The documentation linked is for SonarCloud.

Thanks. Does /d:sonar.dotnet.exclude allow us to exclude files?

Here is what we tried by setting the following .props file that did not work:

<Project>
  <!-- See https://docs.sonarqube.org/7.2/AnalysisParameters.html -->
  <PropertyGroup>
    <!-- Exclude current project from analysis-->
    <!-- <SonarQubeExclude>true</SonarQubeExclude> -->
  </PropertyGroup>
  <!-- To exclude a file from from analysis -->
  <ItemGroup>
    <SonarQubeSetting Include="sonar.coverage.exclusions">
      <Value>
        Dir11\Dir12\Dir13\**,
        Dir21\Dir22\**,
        Dir31\Dir32\Dir33\**,
        **\SomeName1*.cs,
        **\*Somename2.cs
      </Value>
    </SonarQubeSetting>
  </ItemGroup>
</Project>

a) We are looking for a way to have a single file with exclusions in our repository
b) Each exclusion should be on a separate line (if possible)

Hey there.

I’m not sure what sonar.dotnet.exclude is referring to (sonar.coverage.exclusions is the right parameter to use if you’re trying to ignore code coverage), I’m not sure where you found documentation for configuring a .props file this way.

If you’re looking to exclude files in a single place that isn’t the .csproj of affected projects or the SonarCloud UI – your best bet is to put them in your version-controlled azure-pipelines.yml file (where you can define additional properties that are passed to the scanner).

#2 sonar.coverage.exclusions does not seem to have any affect. The only value that has a clear affect is sonar.sources=pages. But then it seems impossible to exclude tests in that folder and the coverage is way off compared to our local stats. ie pages locally is 80% covered but on sonar cloud on 13%.

Using Nuxt / Vue & Jest.

Hi,

What effect are you expecting it to have? It is used to prevent files from being included in coverage metric calculations.

It’s not used to exclude tests. Tests don’t need coverage. It’s used to keep source files out of the calculations.

This dusty, but still relevant, blog post may help.

 
HTH,
Ann

1 Like

Thanks Ann - read that article.

I understand tests don’t need coverage and I’m trying to exclude several folders from being included in the coverage calculation just like we have setup locally with jest.setup.ts.

Is there a link to an example of how to use azure-pipelines.yml file to define additional properties that are passed to the scanner?

Hi @p97 ,

You can add more analysis parameters with the extraProperties key:

- task: SonarCloudPrepare@1
  inputs:
    SonarCloud: 'sonarcloud'
    organization: 'jt'
    scannerMode: 'MSBuild'
    projectKey: 'jt_sonar-scanning-someconsoleapp'
    projectName: 'sonar-scanning-someconsoleapp'
    extraProperties: |
      sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)\**\*.trx
      sonar.cs.vscoveragexml.reportsPaths=$(Agent.TempDirectory)\**\*.coveragexml
      sonar.verbose=true

A post was split to a new topic: Coverage required on test files