6

我正在为 ASP.NET Core 3.1 应用程序设置 Azure DevOps 管道,并且我有以下 YAML 定义测试段)用于构建、测试和代码覆盖率。

      - task: DotNetCoreCLI@2
        displayName: "dotnet global test tool install"
        inputs:
          command: 'custom'
          custom: 'tool'
          arguments: 'install --global dotnet-reportgenerator-globaltool'

      - task: DotNetCoreCLI@2
        displayName: "dotnet test"
        inputs:
          command: 'test'
          projects: '**/*[Tt]ests'
          arguments: '--no-build --configuration $(buildConfiguration) /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput=$(System.DefaultWorkingDirectory)/TestResults/Coverage'
          testRunTitle: 'Unit Test'
          workingDirectory: '$(System.DefaultWorkingDirectory)'
      - script: reportgenerator -reports:$(System.DefaultWorkingDirectory)/**/cobertura/coverage.xml -targetdir:$(System.DefaultWorkingDirectory)/CodeCoverage -reporttypes:HtmlInLine_AzurePipelines
        displayName: "create code coverage report"


      - task: PublishCodeCoverageResults@1
        displayName: "publish test coverage result"
        inputs:
          codeCoverageTool: 'Cobertura'
          summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/cobertura/coverage.xml'

在 Azure DevOps 中运行时,我收到以下错误 来自 Azure DevOps 的错误消息

我可能做错了什么?项目来源:GitHub

4

2 回答 2

10

终于在 Microsoft MVP 的帮助下让它工作了。共享来自有效的测试段的代码。

     - task: DotNetCoreCLI@2
        displayName: "dotnet global test tool install"
        inputs:
          command: 'custom'
          custom: 'tool'
          arguments: 'install --global dotnet-reportgenerator-globaltool'

      - script: dotnet test WebApp.Web.Tests/WebApp.Web.Tests.csproj --logger "trx;LogFileName=testresults.trx" /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput=$(Build.SourcesDirectory)/TestResults/Coverage/
        displayName: 'dotnet test'

      - script: reportgenerator "-reports:$(Build.SourcesDirectory)/TestResults/Coverage/coverage.cobertura.xml" "-targetDir:$(Build.SourcesDirectory)/TestResults/Coverage/Reports" -tag:$(Build.BuildNumber) -reportTypes:htmlInline
        workingDirectory: $(Build.SourcesDirectory)/WebApp.Web.Tests
        displayName: 'dotnet reportgenerator'
      - task: PublishTestResults@2
        inputs:
          testRunner: VSTest
          testResultsFiles: '**/*.trx'
          failTaskOnFailedTests: true

      - task: PublishCodeCoverageResults@1
        inputs:
          codeCoverageTool: 'cobertura'
          summaryFileLocation: $(Build.SourcesDirectory)/TestResults/Coverage/**/coverage.cobertura.xml
          reportDirectory: $(Build.SourcesDirectory)/TestResults/Coverage/Reports
          failIfCoverageEmpty: false 

可以在这里找到帮助的资源

于 2020-03-27T22:17:11.160 回答
2

接受的解决方案让我很接近,但我在全球安装该工具时遇到了问题。PATH 在全局安装中得到更新,但从未重新加载,因此无法找到报告生成器。进行以下更改可以解决此问题:

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

- task: DotNetCoreCLI@2
  displayName: 'Test $(buildConfiguration)'
  inputs:
    command: test
    projects: $(solution)
    arguments: --configuration $(BuildConfiguration) --collect "XPlat Code Coverage" 

- script: ./reportgenerator -reports:$(Agent.TempDirectory)/**/coverage.cobertura.xml -targetDir:$(Build.SourcesDirectory)/TestResults/Coverage/Reports -reporttypes:Cobertura
  displayName: 'Create Code Coverage Reports'

- task: PublishCodeCoverageResults@1
  displayName: 'Publish Code Coverage'
  inputs:
    codeCoverageTool: Cobertura
    summaryFileLocation: $(Build.SourcesDirectory)/TestResults/Coverage/Reports/Cobertura.xml
    failIfCoverageEmpty: false 
于 2020-04-21T18:05:44.087 回答