我有 3 个单元测试项目,用于解决方案中的 3 个不同项目。
当我运行构建管道时,这些单元测试项目会生成 3 个结果,而当代码覆盖率任务运行时,它只选择一个报告。
如何将所有报告合并为一个并进行验证?
在管道结果中,如果我转到“代码覆盖率”选项卡,它具有正确的总行覆盖率值。
管道步骤如下
- task: DotNetCoreCLI@2
displayName: dotnet test $(buildConfiguration)
inputs:
command: test
publishTestResults: true
projects: '$(unitTestsDirs)/**/*.csproj'
arguments: '--configuration $(buildConfiguration) --no-build --collect:"XPlat Code Coverage" --settings "./coverage.runsettings" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=cobertura'
- task: PublishCodeCoverageResults@1
displayName: 'Publish code coverage report'
inputs:
codeCoverageTool: 'Cobertura'
summaryFileLocation: '$(Agent.TempDirectory)/**/coverage.cobertura.xml'
- task: BuildQualityChecks@9
displayName: 'Check build quality'
inputs:
checkCoverage: true
coverageFailOption: fixed
coverageType: lines
coverageThreshold: $(CoverageThreashold)
buildPlatform: $(BuildPlatform)
测试输出的位如下
Results File: /azp/_work/_temp/_rancher-ubuntu-on-prem-agents-nhpsb_2024-11-18_15_05_23.trx
Attachments:
/azp/_work/_temp/d0caa73f-caee-48d1-b626-f49093a0fb14/_rancher-ubuntu-on-prem-agents-nhpsb_2024-11-18.15_05_23.cobertura.xml
/azp/_work/_temp/d0caa73f-caee-48d1-b626-f49093a0fb14/coverage.cobertura.xml
Results File: /azp/_work/_temp/_rancher-ubuntu-on-prem-agents-nhpsb_2024-11-18_15_05_30.trx
Attachments:
/azp/_work/_temp/1c8c6bf9-96be-405d-b0b5-b852eb474a1b/_rancher-ubuntu-on-prem-agents-nhpsb_2024-11-18.15_05_31.cobertura.xml
/azp/_work/_temp/1c8c6bf9-96be-405d-b0b5-b852eb474a1b/coverage.cobertura.xml
Results File: /azp/_work/_temp/_rancher-ubuntu-on-prem-agents-nhpsb_2024-11-18_15_05_35.trx
Attachments:
/azp/_work/_temp/6bb84b94-f726-44b0-94ce-3047f7af5c91/_rancher-ubuntu-on-prem-agents-nhpsb_2024-11-18.15_05_36.cobertura.xml
/azp/_work/_temp/6bb84b94-f726-44b0-94ce-3047f7af5c91/coverage.cobertura.xml
Async Command Start: Publish test results
Publishing test results to test run '911742'.
TestResults To Publish 55, Test run id:911742
Test results publishing 55, remaining: 0. Test run id: 911742
Publishing test results to test run '911744'.
TestResults To Publish 50, Test run id:911744
Test results publishing 50, remaining: 0. Test run id: 911744
Publishing test results to test run '911743'.
TestResults To Publish 71, Test run id:911743
Test results publishing 71, remaining: 0. Test run id: 911743
Published Test Run : https://dev.azure.com/xxxxx/Runs?runId=911742&_a=runCharts
Published Test Run : https://dev.azure.com/xxxxx/Runs?runId=911744&_a=runCharts
Published Test Run : https://dev.azure.com/xxxxx/Runs?runId=911743&_a=runCharts
发布步骤有此警告:
##[警告]发现多个文件或目录匹配。使用第一个匹配项:/azp/_work/_temp/1c8c6bf9-96be-405d-b0b5-b852eb474a1b/coverage.cobertura.xml
因此,构建质量检查的覆盖行数量非常少
您可以使用扩展ReportGenerator来合并代码覆盖率报告。
- task: reportgenerator@5
inputs:
reports: '**/coverage.cobertura.xml'
targetdir: 'coveragereport'
publishCodeCoverageResults: true
- task: BuildQualityChecks@9
displayName: 'Check build quality'
inputs:
checkCoverage: true
coverageFailOption: 'fixed'
coverageType: 'lines'
coverageThreshold: '60'
在 dotnet 测试后添加
reportgenerator@5
任务。您可以在此任务中将publishCodeCoverageResults
设置为true
,这样就不需要添加PublishCodeCoverageResults@1
任务。