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

问题描述 投票:0回答:1

我已经在Azure DevOps上设置了新的管道,该管道可以构建并运行项目的测试。测试是用NUnit编写的。

在管道中,我正在使用VSTest@2任务来运行单元测试,并将codeCoverageEnabled添加到true

最后,管道运行,当我进入作业的“代码覆盖率”选项卡时,它允许我下载.codecoverage文件,但未在选项卡中显示其内容。我的理解是,这应该发生。

我该如何解决?

谢谢

c# azure-devops nunit
1个回答
0
投票

默认情况下,VSTest任务的代码覆盖范围输出到.codecoverage文件,Azure DevOps不知道如何解释,而仅提供为可下载文件。您需要使用一些DotNetCoreCLI任务和coverlet才能在Azure管道的“代码覆盖率”选项卡上显示代码覆盖率结果。

因此,如果您使用的是.NET CORE,则有一种方法可以做到这一点。

步骤1在测试项目中添加Coverlet.collector nuget包

步骤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  

上面的代码要注意的另一件事是Report Generator。要获得其他版本的工具,可能需要根据所使用的.net core版本而定。

也可以在Microsoft Docs上找到更多信息

© www.soinside.com 2019 - 2024. All rights reserved.