Search code examples
.net.net-coreazure-devopsmsbuildsonarqube

Issue in Sonarqube coverage reported on Sonarqube and ADO


I am running Sonar analysis in Azure Pipelines on .net core project.

While I get correct Code coverage analysis reported in Azure DevOps, I get a lot lesser code coverage on Sonarqube.

When investigated, I find that for 2 of my folders in the project, on sonarqube, it does not generate Code Coverage even though I see them in Code.

My Pipeline:

trigger:
- sonar-analysis

pool:
  vmImage: 'ubuntu-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:

- checkout: self
  persistCredentials: true
  submodules: true

- task: SonarQubePrepare@5
  inputs:
    SonarQube: 'sonar-connection'
    scannerMode: 'MSBuild'
    projectKey: 'proj'
    projectName: 'proj'
    extraProperties: |
      sonar.cs.opencover.reportsPaths=$(Agent.TempDirectory)/**/coverage.opencover.xml
      sonar.verbose=true
      sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)/**/*.trx

- task: UseDotNet@2
  inputs:
    version: '6.x'

- task: DotNetCoreCLI@2
  inputs:
    command: 'build'
    projects: '**/**/*.csproj'
    arguments: '--verbosity n'

- task: DotNetCoreCLI@2
  displayName: 'dotnet test'
  inputs:
    command: 'test'
    projects: '**/*Tests/*.csproj'
    arguments: '--configuration $(buildConfiguration) --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=cobertura,opencover'
  
- task: PublishCodeCoverageResults@1
  displayName: 'Publish code coverage report'
  inputs:
    codeCoverageTool: 'Cobertura'
    summaryFileLocation: '$(Agent.TempDirectory)/**/coverage.cobertura.xml'

- task: SonarQubeAnalyze@5
  inputs:
    jdkversion: 'JAVA_HOME_11_X64'

- task: SonarQubePublish@5
  inputs:
    pollingTimeoutSec: '300'

If I select Standalone scanner option, then I get correct code coverage reported on Sonarqube, however with MsBuild, it reports lesser coverage with 2 of the folders excluded from the coverage.

I have not explicitly mentioned any Inclusion/Exclusion Patterns.

What could be the reason of this and how do I resolve?

Edit

Providing my Test.csproj file here as it seems that the 2 ProjectReferences mentioned are the ones which are getting excluded from the coverage on Sonarqube.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>

    <IsPackable>false</IsPackable>


  <ItemGroup>
    <PackageReference Include="AutoMapper" Version="12.0.1" />
    <PackageReference Include="coverlet.msbuild" Version="6.0.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
    <PackageReference Include="Moq" Version="4.18.2" />
    <PackageReference Include="xunit" Version="2.4.2" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="coverlet.collector" Version="3.2.0">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\..\Core\Setup\Setup.csproj" />
    <ProjectReference Include="..\..\Infrastructure\Services\Services.csproj" />
  </ItemGroup>

</Project> 

Solution

  • The Scanner for MSBuild uses a number of criteria to decide whether a project is a test project.

    If you are referencing a test framework then the Scanner will decide the whole project is a test project and not analyse it.

    You can try to add the <SonarQubeTestProject>false</SonarQubeTestProject> under the PropertyGroup part of the *.csproj for the relevant project.(Setup.csproj and Services.csproj)

    <PropertyGroup>
      <SonarQubeTestProject>false</SonarQubeTestProject>
    </PropertyGroup>