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
What could I be doing wrong? Project Source: GitHub
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With