10

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:

Pradeep
  • 1,108
  • 1
  • 15
  • 31

2 Answers2

1

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.

h0p3zZ
  • 690
  • 3
  • 16
  • Stackoverflow best practice is to include examples with links since external links eventually die. – Urasquirrel Dec 19 '22 at 21:10
  • @Urasquirrel this is a link to an open-source tool.. so yeah.. can you explain to me what I should do? :D – h0p3zZ Dec 20 '22 at 23:31
  • No problem mate! Apologies, I wasn't clear. Stackoverflow best practice is to include an example as code in the tic marks like ``` put example here ```. You can just copy the code from the site and put it here so if the link dies, we still have the example. – Urasquirrel Feb 01 '23 at 12:55
  • 2
    @Urasquirrel yeah i know what you mean, but the problem is the code is useless if the link dies, cause you need to tool for it (link) :D But if you think it's necessary I'll put some code into the answer. – h0p3zZ Feb 02 '23 at 00:14
  • @h)p3zZ yes you got it. StackOverflow trains us to do it this way as a best practice. It also encourages us to teach others. (/thumbsup) – Urasquirrel Mar 20 '23 at 19:08
  • 1
    @Urasquirreli added a "code" example – h0p3zZ Apr 01 '23 at 17:31
1

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

Using in pipeline

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
Thomas Glaser
  • 1,670
  • 1
  • 18
  • 26