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



我有一个基于"Ubuntu 16.04"的dot-net核心版本:"3.0.100",并试图将代码覆盖率推送到我们自己托管的SonarQube。

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

不过,我还没能将cobertura.xml文件推送到sonarqube。

我读过这篇文章,在我看来,唯一提到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'

谢谢:-(

我认为最简单的解决方案是将coverlet配置为输出多种格式(请参阅文档(。

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

# >= dotnet 7
dotnet test --collect "XPlat Code Coverage;Format=cobertura,opencover"
# < dotnet 6
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;Format=cobertura,opencover"

是否可以使用该文件来覆盖我的C#项目?

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

正如您所读到的,cobertura是用于python和flex的。对于C#,我们需要使用格式为dotCoverOpenCoversonar.cs.dotcover.reportsPathssonar.cs.opencover.reportsPaths

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

$Env:DOTNET_ROOT= (Get-Command dotnet).Path.Replace('dotnet.exe','sdk2.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 = "$pwdcoverlet.exe"
& $coverlet $testDll.FullName --target "dotnet" --targetargs ("vstest {0} --logger:trx" -f $testDll.FullName) --format "cobertura"
gci -Recurse |
?{ $_.Name -eq "coverage.cobertura.xml"} |
%{ &"$pwdreportgenerator.exe" ("-reports:{0}" -f $_.FullName) "-targetdir:reports" "-reportstypes:HTMLInline;HTMLChart" }

你可以查看他的博客在Azure DevOps构建管道中运行ASP.NET NUnit测试并将结果发布到Sonar Cloud(链接断开(以了解一些详细信息。

希望这能有所帮助。

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

有了两个文件,您可以为Azure DevOps代码覆盖率选项卡发布cobertura文件,并通过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)"
.
.
.

最新更新