简体   繁体   中英

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. Ie

<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?

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

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.

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) 声纳网络用户界面

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

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>

I had the same problem, and it took me a while to figure it out:

Set up a Directory.Build.props file in the solution directory with this content:

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">

  <!-- This target customises the SonarQube MSBuild runner targets to limit the projects that are analysed.

       Projects whose full path and file name match the specified filter will be marked as "excluded".

       Note that this targets file does not ever set $(SonarQubeExclude) to "false". This is to allow other
       targets to exclude projects for other reasons (e.g. a Fakes projects should always be excluded, regardless
       of whether or not they match the project filter).

       Also, this project will do nothing if $(SonarQubeExclude) has already been set.

       The regular expression uses the normal .NET regular expression syntax.

       Usage:
       (1) include the target in the projects being built, either by directly importing it or by
           dropping it in the one of the standard "ImportBefore" folders.

       (2) set $(SQExclusionFilter) to the desired regular expression
           e.g. the following example matches all projects with "CodeSense\Services" in the path:  .*\\CodeSense\\Services\\.*

  -->
  <PropertyGroup Condition=" $(SonarQubeExclude) == '' AND $(SQExclusionFilter) != '' ">

      <MatchesSQExclusionFilter Condition="$([System.Text.RegularExpressions.Regex]::IsMatch($(MSBuildProjectFullPath), $(SQExclusionFilter), System.Text.RegularExpressions.RegexOptions.IgnoreCase)) ">true</MatchesSQExclusionFilter>
      <SonarQubeExclude Condition="$(MatchesSQExclusionFilter) == 'true' " >true</SonarQubeExclude>

  </PropertyGroup>

  <!-- This target is not required: it simply writes out additional information to simplify debugging -->
  <Target Name="AnalysisProjectInfoExclude_DEBUG" BeforeTargets="CoreCompile"
          Condition="$(SQExclusionFilter) != '' ">

    <Message Importance="high" Text="ExclusionFilter: filter has been set.  Filter= $(SQExclusionFilter)" />
    <Message Importance="high" Text="ExclusionFilter: current project = $(MSBuildProjectFullPath)" />
    <Message Importance="high" Text="ExclusionFilter: match result = $(MatchesSQExclusionFilter)" />

    <Message Importance="high" Condition="$(MatchesSQExclusionFilter) == 'true' "
       Text="ExclusionFilter: project is excluded" />

    <Message Importance="high" Condition="$(MatchesSQExclusionFilter) != 'true' "
       Text="ExclusionFilter: project has not been excluded by the exclusion filter" />

  </Target>

</Project>

This fragment will be included in all project files as explained here https://docs.microsoft.com/en-us/visualstudio/msbuild/customize-your-build

Set the regex in the environment variable SQExclusionFilter accordingly. Use double-backslashes for path separation. In windows shell escape pipe characters with a caret: eg

set SQExclusionFilter=(path1\\project)^|(path2\\project)

Credits to Duncan Pocklington from SQ/MS!

This is an old question, but a more accurate answer is posted here: https://stackoverflow.com/a/49904805/987191

Essentially, the code tab allows drilling down to settings for a project and we can use ** in administration (for the project) -> General Settings -> Analysis Scope -> Coverage exclusions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM