【问题标题】:Publish a pipeline Azure Devops code coverage report发布管道 Azure Devops 代码覆盖率报告
【发布时间】:2021-02-11 23:32:11
【问题描述】:

我正在尝试在我的 Azure DevOps Pipeline 中在线发布一份详细的报告,但我得到的只是一个下载此 Coverage 文件的链接。 (自 Visual Studio 2019 以来,社区版本无法再阅读)

这是我的管道:

trigger:
  branches:
    include:
    - '*'

pool:
  vmImage: 'windows-2019'

steps:
- task: NuGetToolInstaller@0
  displayName: Instal Nuget
  inputs:
    checkLatest: true

- task: NuGetCommand@2
  displayName: Restore Nuget Packages
  inputs:
    restoreSolution: '**/*.sln'

- task: UseDotNet@2
  displayName: 'Install .NET Core SDK'
  inputs:
    version: 3.1.x
    performMultiLevelLookup: true

- task: DotNetCoreCLI@2
  displayName: Build Tests
  inputs:
    command: 'build'
    projects: '**/OneTienditaUnitTests/*.csproj'
    arguments: '--configuration Release'

- script: dotnet test OneTienditaUnitTests --logger trx --collect "Code coverage"

- task: PublishTestResults@2
  inputs:
    testRunner: VSTest
    testResultsFiles: '**/*.trx'

- task: XamarinAndroid@1
  displayName: Build Android App
  inputs:
    projectFile: '**/*Android*.csproj'
    outputDirectory: '$(build.binariesDirectory)/Release'
    configuration: 'Release'

如果我像这样使用 Cobertura,则不起作用:

- task: DotNetCoreCLI@2
  displayName: Run Tests
  inputs:
    command: 'test'
    projects: '**/OneTienditaUnitTests/*.csproj'
    arguments: '--configuration Release /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput=../reports/coverage/'
    
- task: PublishCodeCoverageResults@1
  displayName: 'Publish code coverage results'
  inputs:
    codeCoverageTool: Cobertura
    summaryFileLocation: '$(build.sourcesdirectory)\reports\coverage\coverage.cobertura.xml'
    reportDirectory: '$(build.sourcesdirectory)\reports\coverage'

请帮忙?我不是专业的 DevOps

【问题讨论】:

    标签: azure-devops azure-pipelines code-coverage pipeline cobertura


    【解决方案1】:

    要发布报告,您需要使用 Cobertura。对于 TRX,您只会获得下载文件的链接。要创建 Cobertura 报告,您需要在测试项目中安装 coverlet.collector nuget 包。这里有可以解决问题的代码:

    # You just added coverlet.collector to use 'XPlat Code Coverage'
    - task: DotNetCoreCLI@2
      displayName: Test
      inputs:
        command: test
        projects: '**/*Tests/*.csproj'
        arguments: '--configuration $(buildConfiguration) --collect:"XPlat Code Coverage" -- RunConfiguration.DisableAppDomain=true'
        workingDirectory: $(Build.SourcesDirectory)
    
    - 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  
    

    [2021 年更新]

    您不需要额外的任务来安装/运行自定义 ReportGenerator 工具:它现在是读取 coverage.cobertura.xml 文件的默认工具,并且包含在 dotnet CLI 中。

    不过,默认情况下,它会将 cobertura xml 文件保存到代理上的临时目录中。因此,您只需更新PublishCodeCoverageResults 任务的summaryFileLocation 以指向临时目录并跳过“中间人”步骤:

    # You just added coverlet.collector to use 'XPlat Code Coverage'
    - task: DotNetCoreCLI@2
      displayName: Test
      inputs:
        command: test
        projects: '**/*Tests/*.csproj'
        arguments: '--configuration $(buildConfiguration) --collect:"XPlat Code Coverage"'
    
    - task: PublishCodeCoverageResults@1
      displayName: 'Publish code coverage'
      inputs:
        codeCoverageTool: Cobertura
        summaryFileLocation: '$(Agent.TempDirectory)/**/coverage.cobertura.xml'
    

    如果您有多个生成多个覆盖率文件的测试项目,请在测试命令后使用这些步骤。它会在发布文件之前合并文件:

      - task: reportgenerator@4
        displayName: "Merge code coverage reports"
        inputs:
          reports: "**/coverage.cobertura.xml"
          targetdir: "$(Build.ArtifactStagingDirectory)/coverlet"
          reporttypes: "Cobertura"
          verbosity: "Verbose"
    
      - task: PublishCodeCoverageResults@1
        displayName: "Publish code coverage results"
        inputs:
          codeCoverageTool: Cobertura
          summaryFileLocation: "$(Build.ArtifactStagingDirectory)/coverlet/Cobertura.xml"
    

    【讨论】:

    • 它有效,但有一点细节,在“脚本”行中,我不得不在开始时去掉“./”。谢谢,完全没有安装部分。
    • 谢谢。考虑到 MS 文档建议的 XPlat 代码覆盖率,是否有一项任务将所有这些步骤整合到一个任务中? docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test
    • 好吧,您可以创建一个包含这些步骤的模板,并在您的管道中重复使用它。
    • 从 2021 年夏季开始,您不需要额外的任务来安装/运行 ReportGenerator,因为它现在是读取 coverage.covertura.xml 文件的默认工具。您只需将PublicCodeCoverageResults 任务的summaryFileLocation 更新为指向临时目录,就像您已经在处理传递给ReportGenerator 路径的脚本任务一样
    • 我已经尝试了这些步骤,不幸的是发布任务失败:Multiple file or directory matches were found. Using the first match: C:\agent\_work\38\s\artifacts\test-results\$redacted\In\redacted\coverage.cobertura.xml##[error]No code coverage results were found to publish.我不知道发生了什么。当我在本地运行dotnet test 时,我可以看到所有coverage.cobertura.xml 文件按预期生成。我什至可以在管道的dotnet test 输出中看到它们。
    【解决方案2】:

    我已经尝试了很多来解决他的问题......

    首先,问题在于管道的测试任务(在我的例子中是 DotNetCoreCLI@2 任务)。如果此任务收集代码覆盖率,它会自动以 .coverage 格式与测试一起发布。由于某种原因,此 .coverage 文件的推送位于管道(或作业 - 我不知道)的末尾,因此会覆盖所有先前上传的文件。

    解决方案 使用 OpenCover 之类的覆盖工具并使用它收集代码覆盖率,而不是通过打开 CodeCoverage 收集的测试任务收集代码覆盖率。

    例子:

    OpenCover.Console.exe -target:"dotnet.exe" -targetArgs:"test {solution.sln}" -output:"{outputDir}\OpenCover.xml" {-oldstyle}

    之后通过 ReportGenerator 任务将 OpenCover 文件转换为 Cobertura 格式。

    之后,您可以像往常一样通过 PublishCodeCoverageResult 任务发布它。

    您可能需要稍微尝试一下,但考虑到该解决方案,它会起作用。

    【讨论】:

      【解决方案3】:

      我在 Azure DevOps 和代码覆盖率报告呈现方面遇到了同样的问题。我在阳光下尝试了一切,但无济于事。有支持投票的解决方案,但似乎没有一个对我有用。我总是得到那个愚蠢的下载链接。

      阅读@h0p3zZ 的答案后,它给了我一个新的提示。出于某种原因,我的dotnet test 命令同时生成了coverage.cobertura.xml*.coverage 文件,并且出于某种奇怪的原因,该文件优先于coverage.cobertura.xml 文件(这使得devops 呈现下载链接)。

      在我的例子中,我将.runsettings 文件传递​​给dontnet test 命令,如下所示,这使得它输出了一个额外的.coverage 文件。

      - task: DotNetCoreCLI@2
        displayName: Test
        inputs:
          command: test
          projects: '**/*Tests/*.csproj'
          arguments: --configuration $(buildConfiguration) --collect:"XPlat Code Coverage"  --settings CodeCoverage.runsettings
      

      一旦我摆脱了--settings CodeCoverage.runsettings 开关,一切都开始正常工作了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-14
        • 2022-01-12
        • 2021-05-25
        • 2020-11-25
        • 2021-07-23
        • 2019-07-04
        • 2021-08-20
        • 2021-08-13
        相关资源
        最近更新 更多