Dam*_*yer 12 sonarqube azure-devops
我有一个点网核心版本:“3.0.100”,基于“Ubuntu 16.04”构建,并且正在尝试将代码覆盖率推送到我们自托管的 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'
Run Code Online (Sandbox Code Playgroud)
谢谢 :-)
dec*_*rch 21
我认为最简单的解决方案是配置被单以输出多种格式(请参阅文档)。
对于本地测试,这将为 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
Run Code Online (Sandbox Code Playgroud)
用于集成到您的 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"
Run Code Online (Sandbox Code Playgroud)
是否可以使用该文件来覆盖我的 C# 项目?
恐怕没有这样的现成属性可以C#用格式覆盖项目Cobertura。
正如您所读到的,cobertura 适用于 python 和 flex。对于 C#,我们需要使用sonar.cs.dotcover.reportsPathsorsonar.cs.opencover.reportsPaths格式的dotCoveror OpenCover。
要解决此问题,您可以尝试使用 Chameera Dulanga 提供的自定义 powershell 脚本作为解决方法:
$Env:DOTNET_ROOT= (Get-Command dotnet).Path.Replace('dotnet.exe','sdk\2.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" }
Run Code Online (Sandbox Code Playgroud)
您可以查看他的博客在 Azure DevOps 构建管道中运行 ASP.NET NUnit 测试并将结果发布到 Sonar Cloud(链接已损坏)以了解一些详细信息。
希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
12175 次 |
| 最近记录: |