Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 416
mvandevy Avatar asked Dec 22 '17 09:12

mvandevy


People also ask

Can SonarQube scan .NET Code?

SonarQube supports the following . NET test coverage tools: Visual Studio Code Coverage. dotnet-coverage Code Coverage.

What is SonarQube C#?

SonarQube is one of the most popular open source static code analysis tools available in the market. It helps software professionals to measure the code quality and identify non-compliant code. The SonarQube community is very active and provides continuous upgrades, new plug-ins and customizations.


1 Answers

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

like image 196
Naveen R Kumar Avatar answered Sep 16 '22 14:09

Naveen R Kumar