How to make Sonarqube exclude a .NET (C#) project from coverage measures

21,043

Solution 1

Summing up the above mentioned answers and also adding one point to it.

  1. To exclude a project from SonarQube Analysis from csproj we can achieve by adding the below code in .csproj of that project

    <PropertyGroup>
    <!-- Exclude the project from analysis -->
    <SonarQubeExclude>true</SonarQubeExclude>
    </PropertyGroup>
    
  2. To exclude a file from a project

     <ItemGroup>
     <SonarQubeSetting Include="sonar.coverage.exclusions">
     <Value>**/FileName.cs</Value>
     </SonarQubeSetting>
     </ItemGroup>
    
  3. And for multiple files

    <ItemGroup>
    <SonarQubeSetting Include="sonar.coverage.exclusions">
    <Value>**/FileName1.cs, **/FileName2.cs</Value>
    </SonarQubeSetting>
    </ItemGroup>
    

Also can refer this for regex patterns used

Solution 2

Not enough rep to comment, but if you apply both answers here, you can simply add:

<PropertyGroup>
    <!-- Exclude the project from analysis -->
    <SonarQubeExclude>true</SonarQubeExclude>
</PropertyGroup>

in the .csproj file to exclude the entire project instead of individual files, or relying on sonarqube filters.

Solution 3

Some notion :

A .NET solution is SonarQube's project. A .NET solutions's project is SonarQube's project's module.

The specific SonarQube MsBuild analyser will find all csproj/vbproj. For each .NET project, the analyser apply the SonarQube's project settings.

If you repository is :

MyApp.sln
MyApp.Shell\
    MyApp.Shell.csproj
    Windows\MainWindows.cs
MyApp.Core\
    MyApp.Core.csproj
    FooService.cs

To ignore the code coverage on MyApp.Shell project, you will intuitively add the setting sonar.coverage.exclusions=MyApp.Shell\*. But the MsBuild analyser analyse each .NET project separately and the root directely is at csproj/vbproj directory. When MyApp.Shell.csproj is analysed, the ignore patern MyApp.Shell\* is apply on Windows\MainWindows.cs.

To exclure a specific project from code coverage, we need to apply this setting al SonarQube's module (.NET project) level. I find two solutions to do that.

1) Add the property "sonar.coverage.exclusions" in csproj

In your <.NET project>.csproj, add :

<Project ...>
  ...
    <ItemGroup>
      <SonarQubeSetting Include="sonar.coverage.exclusions">
        <Value>**</Value>
      </SonarQubeSetting>
    </ItemGroup>
  ...
</Project>

2) Configure exclusion from Web UI Sonarqube

From update to SonarQube 8.*, I can't retrieve the option in IHM.

From : SonarQube MSBuild Scanner doesn't exclude files from analysis

  • Open Sonarqube's project -> Code (in top menu)
  • Open Sonarqube's module (left icon 'Open Component's Page) -> Administration (in top menu) -> General Settings
  • Add ** in Codecoverage exclusion :

(Screenshot in French, but you get the idea) Sonarqube Web UI

This two solutions work for me, but I would prefer a global setting for Sonarqube's project (.NET Solution).

Solution 4

After trying a thousand things I finally found the solution. It's necessary to add an item group in the .csproj.

<ItemGroup>
    <Compile Update="ClassToExclude.cs">
      <!-- Exclude the file from analysis -->
      <SonarQubeExclude>true</SonarQubeExclude>
    </Compile>
  </ItemGroup>
Share:
21,043
mvandevy
Author by

mvandevy

Updated on July 09, 2022

Comments

  • mvandevy
    mvandevy almost 2 years

    Sonarqube allows for individual files to be excluded from code coverage by adding patterns in the sonar.coverage.exclusions key. This can be done on a project level by adding them in the UI and even in a .csproj file by specifying a SonarQubeSetting element. I.e.

    <SonarQubeSetting Include="sonar.coverage.exclusions"> <Value>**/*.cs</Value> </SonarQubeSetting>

    However, both of these approaches don't seem to work. Playing with the patterns, as specified in the SonarQube documentation doesn't provide the desired result. I'm also aware of the existence of the SonarQubeExclude MSBuild property, but I don't want to go down that path as it would exclude my project from all other analysis.

    Is there another possibility that I'm missing? Or is it simply not possible to exclude all of the classes within a project from code coverage?