通过 Azure DevOps Pipelines 中的 SonarQube 使用 Cobertura 文件进行代码覆盖

Using Cobertura files for code coverage with SonarQube from Azure DevOps Pipelines

我有一个点网核心版本:“3.0.100”,构建于 'Ubuntu 16.04' 之上,我正在尝试将代码覆盖率推送到我们自托管的 SonarQube。

我一直在使用 Coverlet 生成 Cobertura 文件,然后可以使用 PublishCodeCoverageResults@1 发布到 Devops 管道代码覆盖率查看器。

不过我无法将 cobertura.xml 文件推送到 sonarqube。

我读过 this,在我看来,唯一提到 cobertura 的是 python 和 flex。是否可以使用该文件覆盖我的 C# 项目?

我一直在尝试以下内容,但怀疑我在 extraProperties 中的内容不正确。

- task: SonarQubePrepare@4
  inputs:
    SonarQube: 'My SonarQube'
    scannerMode: 'MSBuild'
    projectKey: 'dcap'
    projectName: 'DCAP'
    extraProperties: 'sonar.flex.cobertura.reportPaths=**/DCAP.Testing.Unit/TestResults/*/coverage.cobertura.xml'

谢谢 :-)

Is it possible to use that file to cover my C# project?

恐怕没有这样的开箱即用 属性 来涵盖 Cobertura 格式的 C# 项目。

正如您所读,cobertura 适用于 python 和 flex。对于 C#,我们需要使用格式为 dotCoverOpenCover.

sonar.cs.dotcover.reportsPathssonar.cs.opencover.reportsPaths

要解决此问题,您可以尝试使用 Chameera Dulanga 提供的自定义 powershell 脚本作为解决方法:

$Env:DOTNET_ROOT= (Get-Command dotnet).Path.Replace('dotnet.exe','sdk.1.300')
dotnet tool install dotnet-reportgenerator-globaltool --tool-path . --version 4.0.12
dotnet tool install coverlet.console --tool-path . --version 1.4.1
mkdir .\reports
$testDll = gci -Recurse | ?{ $_.FullName -like ("*bin\{0}\{1}" -f "$(BuildConfiguration)", "$(TestDllFile)") }
$coverlet = "$pwd\coverlet.exe"
& $coverlet $testDll.FullName --target "dotnet" --targetargs ("vstest {0} --logger:trx" -f $testDll.FullName) --format "cobertura"
gci -Recurse |
?{ $_.Name -eq "coverage.cobertura.xml"} |
%{ &"$pwd\reportgenerator.exe" ("-reports:{0}" -f $_.FullName) "-targetdir:reports" "-reportstypes:HTMLInline;HTMLChart" }

您可以查看他的博客 Running ASP.NET NUnit Tests in Azure DevOps Build Pipeline and Publishing Results to Sonar Cloud (LINK BROKEN) 了解更多详情。

希望对您有所帮助。

解决方案是使用 MSBUILD Escaping 告诉 coverlet 在单元测试套件的一次执行中发出 cobertura 和 opencover 结果文件。

使用两个文件,您可以发布 cobertura 一个用于 Azure DevOps 代码覆盖率选项卡,并通过 extraProperties 将 opencover 一个提供给 SonarQubePrepare。

这是我管道中重要部分的摘录。

variables:
  APEX_SOLUTION: 'Redacted.sln'
  BUILD_CONFIGURATION: 'Release'
  ARTIFACTORY_ROOT: 'Artifactory'
  REPOSITORY_CODE: 'redacted-generic-local'
  PRODUCT_PREFIX: 'BLAH'
  REPOSITORY_GROUP: 'Some/Path'
  REPOSITORY_APPLICATION: 'REDACTED'
  PUBLISH_FOLDER: 'publish'
  PACKAGING_FOLDER: 'packaging'
  COBERTURA_OUTPUT: 'coverage.cobertura.xml'
  OPENCOVER_OUTPUT: 'coverage.opencover.xml'
  IS_INTEGRATION_BRANCH: $[eq(variables['Build.SourceBranch'], 'refs/heads/develop')]
  SONAR_SERVER_ENDPOINT: 'SonarQube'
  SONAR_PROJECT_KEY: 'redacted-web'
  SONAR_PROJECT_NAME: 'REDACTED-Web'
  .
  .
  .
- task: SonarQubePrepare@4
  displayName: 'Prepare analysis on SonarQube'
  inputs:
    SonarQube: $(SONAR_SERVER_ENDPOINT)
    scannerMode: MSBuild
    configMode: manual
    projectKey: $(SONAR_PROJECT_KEY)
    projectName: $(SONAR_PROJECT_NAME)
    extraProperties: |
      sonar.cs.opencover.reportsPaths="$(Build.SourcesDirectory)/TestResults/Coverage/$(OPENCOVER_OUTPUT)"
      sonar.cs.nunit.reportsPaths="$(Agent.BuildDirectory)/TestResult.xml"
      sonar.scm.exclusions.disabled=true
      sonar.exclusions=Redacted/wwwroot/**
      sonar.branch.name=$(Build.SourceBranchName)
      sonar.projectVersion=$(Build.BuildNumber)
  .
  .
  .
- task: DotNetCoreCLI@2
  displayName: 'Unit Tests'
  inputs:
    command: 'test'
    projects: '$(APEX_SOLUTION)'
    arguments: '--configuration $(BUILD_CONFIGURATION) --logger "nunit;LogFilePath=$(Agent.BuildDirectory)/TestResult.xml" /p:CollectCoverage=true /p:CoverletOutputFormat="cobertura%2copencover" /p:CoverletOutput=$(Build.SourcesDirectory)/TestResults/Coverage/ /p:Threshold=50 /p:ThresholdType=branch /p:ThresholdStat=Average /p:Exclude="[Redacted.Views]*"'
    publishTestResults: true

- task: PublishCodeCoverageResults@1
    displayName: 'Publish code coverage report'
    inputs:
      codeCoverageTool: 'Cobertura'
      summaryFileLocation: "$(Build.SourcesDirectory)/**/$(COBERTURA_OUTPUT)"
  .
  .
  .

我认为最简单的解决方案是配置 coverlet 以输出多种格式(参见 documentation)。

对于本地测试,这将为 SonarQube 生成 opencover 格式,为 Azure DevOps 生成 cobertura 格式:

dotnet test --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=cobertura,opencover

用于集成到您的 Azure Pipeline

...
- task: SonarQubePrepare@4
  displayName: Prepare SonarQube
  inputs:
    SonarQube: 'SonarQube Enterprise'
    projectKey: $(ProjectKey)
    projectName: $(ProjectName)
    extraProperties: |
      sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)/*.trx
      sonar.cs.opencover.reportsPaths=$(Agent.TempDirectory)/*/coverage.opencover.xml
...
- task: DotNetCoreCLI@2
  displayName: Run tests
  inputs:
    command: test
    arguments: --configuration $(BuildConfiguration) --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=cobertura,opencover