7

I am setting up an Azure DevOps pipeline for an ASP.NET Core 3.1 Application and I have the following YAML definition test segment) for building, testing and code coverage.

      - 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'

Upon running in Azure DevOps, I get the following error Error message from Azure DevOps

What could I be doing wrong? Project Source: GitHub

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
Dara Oladapo
  • 586
  • 6
  • 12

2 Answers2

10

Finally got it working with the help of a Microsoft MVP. Sharing the code from the test segment that worked.

     - 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 

Resources that helped can be found here

Dara Oladapo
  • 586
  • 6
  • 12
  • 1
    @LanceLi-MSFT it would be helpful if MSFT updated the *.Net Core Task* documentation to reflect that code coverage is not currently supported on Linux agents, and provided the workaround above. There's already an open issue here: https://github.com/MicrosoftDocs/vsts-docs/issues/4934 – urig Apr 09 '20 at 11:34
  • 1
    @urig Agree with you, that's one good idea. I checked the [one](https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/build/dotnet-core-cli?view=azure-devops) and [two](https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/test/publish-code-coverage-results?view=azure-devops) and confirmed it should work for linux and windows according to the document. So I suggest you use `report a problem` button [here](https://developercommunity.visualstudio.com/spaces/21/index.html) to report the issue. The team there would know this issue and release a fix or help to update the document. – LoLance Apr 10 '20 at 01:20
  • 1
    I now see the documentation has been updated to say "If you're building on Windows". I've opened this issue on VS dev community asking for Linux support: https://developercommunity.visualstudio.com/content/problem/986995/net-core-task-does-not-publish-code-coverage-resul.html – urig Apr 11 '20 at 19:52
2

The accepted solution got me close, but I had issues with installing the tool globally. The PATH gets updated in a global install, but is never reloaded so the reportgenerator could not be found. Making the following changes took care of that:

 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 
Per
  • 491
  • 6
  • 19