简体   繁体   English

在 Azure DevOps 上显示 NUnit 测试代码覆盖率

[英]Displaying NUnit tests code coverage on Azure DevOps

I've setup a new pipeline on Azure DevOps which builds and run the tests of the projects.我在 Azure DevOps 上设置了一个新管道,用于构建和运行项目的测试。 The tests are written with NUnit.测试是用 NUnit 编写的。

In the pipeline I'm using the VSTest@2 task to run the unit tests and I add the codeCoverageEnabled to true .在管道中,我使用VSTest@2任务来运行单元测试,并将codeCoverageEnabled添加到true

In the end the pipeline runs and when I go in the "Code Coverage" tab of the job, it allows me to download .codecoverage file but it does not display its content in the tab.最后管道运行,当我在作业的“代码覆盖率”选项卡中 go 时,它允许我下载.codecoverage文件,但它不会在选项卡中显示其内容。 My understanding was that this should happen.我的理解是这应该发生。

How can I fix this?我怎样才能解决这个问题?

Thanks谢谢

By default, the code coverage for the VSTest Task is output to a .codecoverage file, which Azure DevOps does not know how to interpret and only provides as a downloadable file.默认情况下,VSTest 任务的代码覆盖率为 output 到.codecoverage文件,Azure DevOps 不知道如何解释,仅作为可下载文件提供。 You'll need to use a few DotNetCoreCLI tasks and coverlet to be able to display code coverage results on the code coverage tab in azure pipelines.您需要使用一些DotNetCoreCLI任务和Coverlet才能在 azure 管道的代码覆盖率选项卡上显示代码覆盖率结果。

So, if you are on .NET CORE, There is a way how you can do that.因此,如果您使用的是 .NET CORE,那么您有办法做到这一点。

Step 1 add Coverlet.collector nuget Package in your test project第 1 步在您的测试项目中添加Coverlet.collector nuget Package

Step 2 Change your azure-pipelines.yml to include the following for your code coverage: If you have any settings from a CodeCoverage.runsettings file, you can keep them too第 2 步更改您的azure-pipelines.yml以包含以下代码覆盖率:如果您有来自CodeCoverage.runsettings文件的任何设置,您也可以保留它们

- task: DotNetCoreCLI@2
  inputs:
    command: 'test'
    projects: '**/*.Tests/*.csproj'
    arguments: -c $(BuildConfiguration) --collect:"XPlat Code Coverage" -- RunConfiguration.DisableAppDomain=true
    testRunTitle: 'Run Test and collect Coverage' 
  displayName: 'Running tests'

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

- script: reportgenerator -reports:$(Agent.TempDirectory)/**/coverage.cobertura.xml -targetdir:$(Build.SourcesDirectory)/coverlet/reports -reporttypes:"Cobertura"
  displayName: Create reports

- task: PublishCodeCoverageResults@1
  displayName: 'Publish code coverage'
  inputs:
    codeCoverageTool: Cobertura
    summaryFileLocation: $(Build.SourcesDirectory)/coverlet/reports/Cobertura.xml  

One other thing to note for the above code is the Report Generator .上述代码要注意的另一件事是Report Generator Depending on which version of .net core you are using it might be required to get a different version of the tool.根据您使用的 .net 内核的版本,可能需要获取不同版本的工具。

More information can also be found on the Microsoft Docs更多信息也可以在Microsoft Docs上找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM