So the process can be deduced from https://learn.microsoft.com/en-us/azure/devops/pipelines/ecosystems/dotnet-core?view=azure-devops#collect-code-coverage and contains of the following steps:
- Run the tests with coverage without publishing the test results, because we need to control the location of the generated binary coverage report (see How to set a custom coverage result file path when running dotnet test --collect "Code coverage"?)
- Convert the VS binary coverage result to XML
- Install the reportgenerator tool
- Convert the VS XML coverage to Cobertura using the reportgenerator tool
- Publish the Cobertura report using the Publish Code Coverage Azure DevOps task
- Publish the Test Results
These steps can be implemented with the following YAML template:
parameters:
BuildConfiguration: Debug
steps:
- task: DotNetCoreCLI@2
name: Test
displayName: Test
inputs:
command: 'test'
publishTestResults: false
arguments: '-c ${{ parameters.BuildConfiguration }} --no-build -l trx -r $(Common.TestResultsDirectory)\tests --collect "Code coverage"'
- task: PublishTestResults@2
displayName: Publish Test Results
inputs:
testResultsFormat: VSTest
testResultsFiles: '*.trx'
searchFolder: $(Common.TestResultsDirectory)\tests
testRunTitle: $(Build.DefinitionName)-$(Build.BuildNumber)
condition: succeededOrFailed()
- powershell: |
$cc = "$(Get-ToolFolderFromNuGet Microsoft.CodeCoverage)\..\build\netstandard1.0\CodeCoverage\CodeCoverage.exe"
$BinaryCoverageFile = (Get-Item "$(Common.TestResultsDirectory)\tests\*\*.coverage").FullName
& $cc analyze /output:$(Common.TestResultsDirectory)\vstest-coverage.xml $BinaryCoverageFile
displayName: Convert Coverage Result To Xml
- task: DotNetCoreCLI@2
inputs:
command: custom
custom: tool
arguments: install --tool-path . dotnet-reportgenerator-globaltool
displayName: Install ReportGenerator tool
- script: .\reportgenerator.exe -reports:$(Common.TestResultsDirectory)\vstest-coverage.xml -targetdir:$(Common.TestResultsDirectory)\coverage\report -reporttypes:"Cobertura"
displayName: Create Cobertura Coverage Report
- task: PublishCodeCoverageResults@1
displayName: Publish Coverage Results
inputs:
codeCoverageTool: 'Cobertura'
summaryFileLocation: '$(Common.TestResultsDirectory)/coverage/report/Cobertura.xml'
failIfCoverageEmpty: true
It uses my powershell function Get-ToolFolderFromNuGet
to download a package from NuGet, but other than that no custom code is used.
Anyway, the problem is that the Publish Test Results task publishes the binary coverage result as a hyperlink in the same tab where the coverage results are supposed to be:
This is useless. If I comment out the Publish Test Results, the coverage page shows the expected result:
But now I lost the Tests page, of course:
Does anyone know how to resolve this conundrum?
P.S.
I opened a bug here - https://github.com/microsoft/azure-pipelines-tasks/issues/12670
EDIT 1
I tried to Publish the Test Results at the end and even deleting the binary coverage result file. Nothing helps.