本文介绍了Azure DevOps-使用``dotnet test --collect:'Code Coverage'''时版本不显示代码覆盖率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.NET Framework项目,该项目是在自托管的Windows构建代理上构建的.

I have a .NET Framework project that is being built on a self hosted Windows build agent.

有一个步骤可以运行测试,并且该步骤需要提供代码覆盖率报告和统计信息.

There is a step to run tests, and that step needs to provide code coverage reports and stats.

当我尝试使用"dotnet测试"时,将运行该步骤并完成测试,还会生成.coverage文件.完成后检查构建摘要时,我会看到标准测试结果和报告以及代码覆盖率"选项卡.代码覆盖率选项卡具有下载链接以获取文件.没有代码覆盖率报告.在初始构建摘要屏幕上,还有一个指向设置代码覆盖率"的链接.

When i try using "dotnet test" the step runs and the tests complete, the .coverage files are also generated. When i check the build summary after it's complete i see the standard test results and report, and also the code coverage tab. The code coverage tab has a download link to get the file. There is no code coverage report. There is also a link to "Setup Code Coverage" on the initial build summary screen.

为什么没有代码覆盖率报告?为什么设置代码覆盖率"链接仍然可见?

Why is there no code coverage report? and why is the "Setup Code Coverage" link still visible?

这真令人沮丧!我肯定会错过一些非常明显的东西,但是文档表明我所做的是正确的.

This is incredibly frustrating! I must be missing something incredibly obvious, but the docs suggest what i have done is correct.

使用VSTest任务而不是dotnet测试可以得到相同的结果,但运行速度要慢得多.

Using VSTest task rather that dotnet tests results in the same outcome, but runs far slower.

        displayName: dotnet test
        inputs: 
          command: test 
          arguments: '--configuration $(BuildConfiguration) --collect:"Code Coverage"'
          workingDirectory: '$(Build.SourcesDirectory)\src'```

推荐答案

我最终通过使用休·林(Hugh Lin)的答案寻求帮助并根据自己的目的进行了修改.

I eventually achieved this by using Hugh Lin's answer for help and modifying for my own purposes.

我们在项目中将Coverlet作为参考,并将ReportGenerator安装到Azure DevOps中,从而使此操作变得容易一些.

We have Coverlet as a reference in the project, and ReportGenerator installed into Azure DevOps, so that made this a little easier.

我发现我们的SOAP API参考存在问题,导致生成报告时出现了巨大的性能问题.一旦我使用"classfilter"将其过滤掉,该过程就变得更易于管理.我还发现,如果没有"disable.coverage.autogenerate"功能,变量"PublishCodeCoverageResults";当任务尝试执行"ReportGenerator"操作时,该任务将永远花费并且很可能失败.在没有"classfilters"的情况下自行完成.这样做是因为"ReportGenerator"内置到"PublishCodeCoverageResults"中立即执行,但由于没有过滤器,因此在这种情况下不起作用.

I found that we had an issue with a SOAP API reference that was causing the huge performance issues with generating a report. Once I filtered that out with a "classfilter"the process became more manageable. I also found that without the "disable.coverage.autogenerate" variable the "PublishCodeCoverageResults" task will take forever and likely fail as it tries to do the "ReportGenerator" step itself by without the "classfilters". It does this because the "ReportGenerator" is built into the "PublishCodeCoverageResults" step now, but due to having no filters it doesn't work for this scenario.

这是针对.NET Framework项目运行的,因此需要对项目进行一些调整,以确保对".net测试"进行测试.工作成功.

This is running against a .NET Framework project so there were a few adjustments to the projects needed to ensure "dotnet test" works successfully.

variables:
  disable.coverage.autogenerate: 'true'


  - task: DotNetCoreCLI@2
    displayName: dotnet test
    inputs: 
      command: test 
      publishTestResults: true
      arguments: '/p:CollectCoverage=true /p:CoverletOutputFormat=cobertura --no-restore'
      workingDirectory: '$(Build.SourcesDirectory)\src'
      configuration: "$(buildConfiguration)"
      
  - task: reportgenerator@4
    inputs:
      reports: '$(Build.SourcesDirectory)\src\*.UnitTests\coverage.cobertura.xml'
      targetdir: '$(Common.TestResultsDirectory)/CoverageReport/'
      classfilters: '-NAMESPACE*'

  - task: PublishCodeCoverageResults@1
    inputs:
      codeCoverageTool: 'Cobertura'
      summaryFileLocation: '$(Build.SourcesDirectory)\src\*.UnitTests\coverage.cobertura.xml'
      reportDirectory: '$(Common.TestResultsDirectory)/CoverageReport/'

这篇关于Azure DevOps-使用``dotnet test --collect:'Code Coverage'''时版本不显示代码覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 23:03