Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 859
Dara Oladapo Avatar asked Mar 27 '20 20:03

Dara Oladapo


People also ask

How do I get code coverage report in Azure DevOps?

The code coverage summary can be viewed on the Summary tab on the pipeline run summary. The results can be viewed and downloaded on the Code coverage tab.

What is code coverage in DevOps?

Code coverage helps you determine the proportion of your project's code that is actually being tested by tests such as unit tests. To increase your confidence of the code changes, and guard effectively against bugs, your tests should exercise - or cover - a large proportion of your code.


Video Answer


1 Answers

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

like image 51
Dara Oladapo Avatar answered Nov 15 '22 10:11

Dara Oladapo