Dara Oladapo
Dara Oladapo

Reputation: 596

Azure DevOps Code Coverage for .NET Core 3.1

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

Upvotes: 7

Views: 3728

Answers (2)

Per
Per

Reputation: 521

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 

Upvotes: 2

Dara Oladapo
Dara Oladapo

Reputation: 596

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

Upvotes: 11

Related Questions