如何通过dotnet测试产生的VS覆盖范围发布Cobertura覆盖率报告-收集“代码覆盖率”?

时间:2020-04-10 15:24:23

标签: .net-core azure-devops code-coverage

因此可以从https://docs.microsoft.com/en-us/azure/devops/pipelines/ecosystems/dotnet-core?view=azure-devops#collect-code-coverage推论出该过程,并包含以下步骤:

  1. 运行覆盖范围为而没有的测试,因为我们需要控制生成的二进制覆盖率报告的位置(请参见How to set a custom coverage result file path when running dotnet test --collect "Code coverage"?
  2. 将VS二进制覆盖率结果转换为XML
  3. 安装报告生成器工具
  4. 使用报告生成器工具将VS XML覆盖范围转换为Cobertura
  5. 使用“发布代码覆盖率Azure DevOps”任务来发布Cobertura报表
  6. 发布测试结果

可以使用以下YAML模板来实现这些步骤:

parameters:
  BuildConfiguration: Debug

steps:
- task: DotNetCoreCLI@2
  name: Test
  displayName: Test
  inputs:
    command: 'test'
    publishTestResults: false
    arguments: '-c ${{ parameters.BuildConfiguration }} --no-build -l trx -r $(Common.TestResultsDirectory)\tests --collect "Code coverage"'

- task: PublishTestResults@2
  displayName: Publish Test Results
  inputs:
    testResultsFormat: VSTest
    testResultsFiles: '*.trx'
    searchFolder: $(Common.TestResultsDirectory)\tests
    testRunTitle: $(Build.DefinitionName)-$(Build.BuildNumber)
  condition: succeededOrFailed()

- powershell: |
    $cc = "$(Get-ToolFolderFromNuGet Microsoft.CodeCoverage)\..\build\netstandard1.0\CodeCoverage\CodeCoverage.exe"
    $BinaryCoverageFile = (Get-Item "$(Common.TestResultsDirectory)\tests\*\*.coverage").FullName
    & $cc analyze /output:$(Common.TestResultsDirectory)\vstest-coverage.xml $BinaryCoverageFile
  displayName: Convert Coverage Result To Xml

- task: DotNetCoreCLI@2
  inputs:
    command: custom
    custom: tool
    arguments: install --tool-path . dotnet-reportgenerator-globaltool
  displayName: Install ReportGenerator tool

- script: .\reportgenerator.exe -reports:$(Common.TestResultsDirectory)\vstest-coverage.xml -targetdir:$(Common.TestResultsDirectory)\coverage\report -reporttypes:"Cobertura"
  displayName: Create Cobertura Coverage Report

- task: PublishCodeCoverageResults@1
  displayName: Publish Coverage Results
  inputs:
    codeCoverageTool: 'Cobertura'
    summaryFileLocation: '$(Common.TestResultsDirectory)/coverage/report/Cobertura.xml'
    failIfCoverageEmpty: true

它使用我的powershell函数Get-ToolFolderFromNuGet从NuGet下载程序包,但不使用自定义代码。

无论如何,问题在于,发布测试结果任务将二进制覆盖范围结果作为超链接发布在应该覆盖范围结果应该位于的同一选项卡中:

enter image description here

这没用。如果我注释掉“发布测试结果”,则覆盖页面将显示预期结果:

enter image description here

但是,现在我当然失去了“测试”页面:

enter image description here

有人知道如何解决这个难题吗?

P.S。

我在这里打开了一个错误-https://github.com/microsoft/azure-pipelines-tasks/issues/12670

编辑1

我试图在最后发布测试结果,甚至删除二进制覆盖率结果文件。没有任何帮助。

1 个答案:

答案 0 :(得分:0)

通过检查diag日志中的“发布测试结果”任务,我设法弄清了解决方法。确实,从日志中:

2020-04-10T15:48:12.1799774Z ##[debug]pattern: '*.trx'
2020-04-10T15:48:12.1839752Z ##[debug]findPath: 'd:\_wf\06\13\TestResults\tests'
2020-04-10T15:48:12.1840795Z ##[debug]statOnly: 'false'
2020-04-10T15:48:12.1843783Z ##[debug]findPath: 'd:\_wf\06\13\TestResults\tests'
2020-04-10T15:48:12.1844681Z ##[debug]findOptions.allowBrokenSymbolicLinks: 'true'
2020-04-10T15:48:12.1845119Z ##[debug]findOptions.followSpecifiedSymbolicLink: 'true'
2020-04-10T15:48:12.1845244Z ##[debug]findOptions.followSymbolicLinks: 'true'
2020-04-10T15:48:12.1850467Z ##[debug]  d:\_wf\06\13\TestResults\tests (directory)
2020-04-10T15:48:12.1858807Z ##[debug]  d:\_wf\06\13\TestResults\tests\1f627391-dd45-48b3-af92-5213c471eb04 (directory)
2020-04-10T15:48:12.1864156Z ##[debug]  d:\_wf\06\13\TestResults\tests\tfsbuild_TDC5DFC1BLD24_2020-04-10_11_47_29 (directory)
2020-04-10T15:48:12.1868508Z ##[debug]  d:\_wf\06\13\TestResults\tests\tfsbuild_TDC5DFC1BLD24_2020-04-10_11_47_29\In (directory)
2020-04-10T15:48:12.1873069Z ##[debug]  d:\_wf\06\13\TestResults\tests\tfsbuild_TDC5DFC1BLD24_2020-04-10_11_47_29\In\TDC5DFC1BLD24 (directory)
2020-04-10T15:48:12.1880777Z ##[debug]  d:\_wf\06\13\TestResults\tests\tfsbuild_TDC5DFC1BLD24_2020-04-10_11_47_29\In\TDC5DFC1BLD24\tfsbuild_TDC5DFC1BLD24_2020-04-10.11_47_23.coverage (file)
2020-04-10T15:48:12.1884045Z ##[debug]  d:\_wf\06\13\TestResults\tests\tfsbuild_TDC5DFC1BLD24_2020-04-10_11_47_29.trx (file)
2020-04-10T15:48:12.1885576Z ##[debug]7 results

因此在那里找到了二进制覆盖文件。但是它的原始位置不同,我们可以从测试任务本身的常规构建日志中看到它:

2020-04-10T15:47:54.0912521Z M i c r o s o f t   ( R )   C o v e r a g e   C o l l e c t i o n   T o o l   V e r s i o n   1 6 . 0 . 3 0 3 1 9 . 3 0 0 2 
2020-04-10T15:47:54.0913009Z  
2020-04-10T15:47:54.0913107Z  
2020-04-10T15:47:54.0913209Z  C o p y r i g h t   ( c )   M i c r o s o f t   C o r p o r a t i o n .     A l l   r i g h t s   r e s e r v e d . 
2020-04-10T15:47:54.0913270Z  
2020-04-10T15:47:54.0913307Z  
2020-04-10T15:47:54.0913340Z  
2020-04-10T15:47:54.0913400Z  
2020-04-10T15:47:54.3182888Z  Results File: d:\_wf\06\13\TestResults\tests\tfsbuild_TDC5DFC1BLD24_2020-04-10_11_47_29.trx
2020-04-10T15:47:54.3183717Z 
2020-04-10T15:47:54.3184201Z Attachments:
2020-04-10T15:47:54.3184419Z   d:\_wf\06\13\TestResults\tests\1f627391-dd45-48b3-af92-5213c471eb04\tfsbuild_TDC5DFC1BLD24_2020-04-10.11_47_23.coverage
2020-04-10T15:47:54.3196805Z Test Run Successful.
2020-04-10T15:47:54.3197219Z Total tests: 445
2020-04-10T15:47:54.3197713Z      Passed: 445
2020-04-10T15:47:54.3199584Z  Total time: 30.1160 Seconds

因此,原始的二进制覆盖率文件位于d:\_wf\06\13\TestResults\tests\1f627391-dd45-48b3-af92-5213c471eb04\tfsbuild_TDC5DFC1BLD24_2020-04-10.11_47_23.coverage中,但已将其复制到d:\_wf\06\13\TestResults\tests\tfsbuild_TDC5DFC1BLD24_2020-04-10_11_47_29\In\TDC5DFC1BLD24\tfsbuild_TDC5DFC1BLD24_2020-04-10.11_47_23.coverage

我的错误是只删除原始位置。在两个地方都删除后,“代码覆盖率”页面将按预期方式显示-不再有无用的超链接,而是实际覆盖率。

YAML中经过更正的powershell任务如下所示:

- powershell: |
    $cc = "$(Get-NuGetPackageBaseFolder Microsoft.CodeCoverage)\build\netstandard1.0\CodeCoverage\CodeCoverage.exe"
    $BinaryCoverageFile = Get-Item "$(Common.TestResultsDirectory)\tests\*\*.coverage"
    & $cc analyze /output:$(Common.TestResultsDirectory)\vstest-coverage.xml $BinaryCoverageFile.FullName
    Remove-Item (Get-ChildItem -Path "$(Common.TestResultsDirectory)\tests" -Recurse -Filter $BinaryCoverageFile.Name).FullName
  displayName: Convert Coverage Result To Xml

当然,发布测试结果任务必须在此任务之后进行。