Reputation: 1252
I have a .Net solution with two dotnet core class libraries and its associated unit test projects in it.
When I run dotnet test **\*.Tests.csproj --collect "Code Coverage"
command then it generates separate .coveragexml files for each unit test project.
My requirement is to merge all the .coveragexml files into one single file and use that to get the total coverage matrix for a whole solution.
Is there any tool or script to achieve this?
Note:
Upvotes: 14
Views: 5389
Reputation: 2157
There is a dotnet tool called dotnet-coverage that does this.
Install in on the machine (or your build server, if this is for your pipeline):
dotnet tool install --global dotnet-coverage
Generate your coverage with your preferred tools, and then you can use dotnet-coverage
to merge them all into a single file.
dotnet test files (.coverage):
dotnet-coverage merge *.coverage --recursive --output merged.coverage
dotnet test XML coverage files (.xml):
dotnet coverage merge *.xml --recursive --output coverage.xml --output-format xml
Coverlet (.cobertura.xml):
dotnet-coverage merge *.cobertura.xml --recursive --output merged.cobertura.xml --output-format cobertura
To use it in an Azure DevOps pipeline, set up your step like this (this is for Coverlet, but can easily be adjusted):
steps:
- task: DotNetCoreCLI@2
displayName: 'dotnet custom'
inputs:
command: custom
custom: coverage
arguments: 'merge *.coverage.xml --recursive --output MergedCoverage.cobertura.xml --output-format xml'
And then publish the merged coverage report:
steps:
- task: PublishCodeCoverageResults@1
displayName: 'Publish code coverage from MergedCoverage.cobertura.xml'
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: MergedCoverage.cobertura.xml
Upvotes: 4
Reputation: 719
If it is not necessary that the outcome of the merge is still in .coveragexml format you could use the ReportGenerator as a merging tool.
It's an open source tool on GitHub and takes several inputformats (and files) and has the option to convert it into several other formats. I'm afraid .coveragexml isn't one of the output formats, but it is one of the input formats.
Here is an example to run in the command line:
OpenCover.Console.exe -target:"dotnet.exe" -targetArgs:"test {solution.sln}" -output:"{outputDir}\OpenCover.xml" {-oldstyle}
More details on the commands can be found in the ReportGenerator docs.
Upvotes: 1