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?
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.
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: e.g.
set SQExclusionFilter=(path1\\project)^|(path2\\project)
Credits to Duncan Pocklington from SQ/MS!
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>
Some notion :
A .NET solution is Sonarqube's project. A .NET solutions's project is Sonarqube's project's module.
I find two solutions to exlcure Sonarqube's module (.NET project) from code coverage.
1) Configure exclusion from Web UI Sonarqube
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)
2) Add the property "sonar.coverage.exclusions" in csproj
In your <.NET project>.csproj, add :
<Project ...>
...
<Target Name="BeforeBuild">
<ItemGroup>
<SonarQubeSetting Include="sonar.coverage.exclusions">
<Value>**</Value>
</SonarQubeSetting>
</ItemGroup>
</Target>
...
</Project>
This two solutions work for me, but I would prefer a global setting for Sonarqube's project (.NET Solution).
Summing up the above mentioned answers and also adding one point to it.
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>
To exclude a file from a project
<ItemGroup> <SonarQubeSetting Include="sonar.coverage.exclusions"> <Value>**/FileName.cs</Value> </SonarQubeSetting> </ItemGroup>
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
来源:https://stackoverflow.com/questions/47939122/how-to-make-sonarqube-exclude-a-net-c-project-from-coverage-measures