【发布时间】:2021-08-10 03:40:10
【问题描述】:
我们使用以下命令行(或多或少)收集管道中的 C# 代码覆盖率:
dotnet test --no-build -l trx -r TheResultsDir --collect "Code coverage" -s CodeCoverage.runsettings
(我们实际上为此使用了内置的DotNetCoreCLI@2 任务)
这会产生一堆.coverage 文件。我们想和他们做两件事:
- 发送到我们的 SonarQube 服务器
- 使用
PublishCodeCoverageResults@1任务在构建本身上发布。
事实证明(惊喜,惊喜),生成的 .coverage 文件只有 VS IDE 才能理解。
像往常一样 - 互联网来救援。我们发现:
- 使用Microsoft.CodeCoverage 工具,我们可以将
.coverage文件转换为SonarQube 可以理解的.xml,而不是PublishCodeCoverageResults@1任务。 - 使用reportgenerator 工具,我们可以将
.xml文件从(1) 转换为PublishCodeCoverageResults@1任务可以理解的Cobertura 格式
这就是我们所做的,它适用于小型项目。但是,现在我们将代码覆盖率引入我们的大型单体应用程序(叹气,helas - 一切都是真的),而且时间很糟糕。对于一种解决方案(多种解决方案):
- 从
.coverage转换为.xml大约需要20 分钟。 - 从
.xml转换为 Cobertura - 2h 33m。
所以,这真的很糟糕。
如果我想同时发送到 SQ 并发布到构建,是否有更好的解决方案?
这是我们使用的实际代码:
从.coverage 到.xml
CodeCoverage.exe analyze /output:CoverageResult.xml CoverageResult.coverage
(.coverage文件有好几个,所以对每个都应用命令)
从.xml 到 Cobertura
reportgenerator.exe -reports:CoverageResults\*.xml -targetdir:CoberturaReport -reporttypes:Cobertura
发布构建
- task: PublishCodeCoverageResults@1
displayName: Publish Coverage Results
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: CoberturaReport/Cobertura.xml
failIfCoverageEmpty: true
通过标准 SQ 任务发布到 SonarQube:
- 准备 SQ 分析
- 运行 SQ 分析
- 发布 SQ 分析
准备任务是:
- task: SonarQubePrepare@4
displayName: Prepare CI SQ Analysis
inputs:
SonarQube: SonarQube
scannerMode: MSBuild
projectKey: $(SonarQubeProjectKey)
projectName: $(SonarQubeProjectName)
projectVersion: $(SonarQubeProjectVersion)
extraProperties: |
sonar.cs.vscoveragexml.reportsPaths=$(Common.TestResultsDirectory)\vstest-coverage\*.xml
sonar.cs.nunit.reportsPaths=$(Common.TestResultsDirectory)\tests\*.TestResult.xml
sonar.inclusions=**/*.cs
sonar.branch.name=$(SonarQubeSourceBranch)
sonar.scm.disabled=true
【问题讨论】:
-
所以你现在关心的是让这两个转换任务并行,对吧?还是要优化两个转换任务的执行时间?
-
并行没用。与 2h 33m 并行运行的 20 分钟仍然是 2h 33m。如果最终结果 - 由于从一种格式转换为另一种格式而导致发布到 SQ 和构建不需要数小时,我不在乎我是如何做到的。
-
我可以将所有与 SQ 相关的任务并行转换为 Cobertura - SQ 分析和 SQ 发布一起也需要很多时间,但这会使构建变得相当复杂。您现在需要两个构建代理,并且资源被大量使用。理想情况下,我想找到一种完全消除 Cobertura 转换的方法。如果可以直接发布 .coverage 文件,那就太理想了。
-
我明白了。这更接近我的猜测。但是好像没有更好的办法直接将
.coverage类型的测试结果发布到build中
标签: c# .net-core azure-devops sonarqube code-coverage