Is there anyway to merge cobertura coverage xml reports together?
Asked Answered
D

5

34

I have c++/c application with a lots of unit tests. I would like to get overall coverage and also individual coverage of each test with condition that each test can be run only once. Format of coverage must be xml (cobertura xml) for jenkins cobertura plugin to process.

So far I generate gcno files upon compilation and gcda files when source is used. Then call gcovr to get xml file.

I would like to create coverage of each unit test (thus creating coverage xml for every unit test) and then merge these xml files into one xml file.

Thanks!

Dutchman answered 20/10, 2014 at 15:16 Comment(4)
See : #19793010Trimetrogon
Unfortunately application is quite complex and is build using cmake script, no ant/maven is used.Dutchman
In my case gcovr.com/en/stable/guide/merging.html (i.e. generating to json files then merging using the --add-tracefile option) did the trick.Omen
Does cobertura now have a inbuilt mechanism to merge xmls? I am looking for something whose support is good in javascript env.Strikebound
D
4

I wrote python script which can merge cobertura reports together. It only merges line "hits" and is able to filter specific packages while merging.

Conditional(branch-rate) coverage and line coverage (line-rate) percentage values are not merged yet. Jenkins plugin recalculates these values so in this context it doesn't have to be implemented (but maybe it will).

Also conditional coverage can't seem to be put together easily, so it doesn't work when reports cooperate on final value (meaning one file has condition-coverage 50% (2/4) and other also condition-coverage 50% (2/4) other two branches. Result is set to be higher of these two numbers so it will be still 50% (2/4)

Dutchman answered 19/11, 2014 at 8:51 Comment(1)
Is it possible to merge all cobertura coverage xml reports together using PowerShell script? As i am not aware of Phyton language and also I will need to display the merged results on TFS 2017 Build summary.Uhf
C
25

The last release of ReportGenerator can merge cobertura files.

You can install it from nuget

usage:

reportgenerator "-reports:target\*\*.xml" "-targetdir:C:\report" -reporttypes:Cobertura

A file Corbertura.xml is generated in the targetdir directory

You can use the dotnet core version to use it on linux or mac

Confirmation answered 19/9, 2018 at 18:22 Comment(1)
Great find. They also have an Azure DevOps pipeline task that I've successfully implemented to resolve issues such as thisOrganotherapy
M
8

The simplest utility I've found for merging Cobertura files is cobertura-merge via NPM.

Usage:

npm install cobertura-merge
cobertura -merge -o output.xml package1=input1.xml package2=input2.xml

Or it can be used directly without installing through npx:

npx cobertura-merge -o output.xml package1=input1.xml package2=input2.xml

Examples sourced from the GitHub README for cobertura-merge.

Million answered 31/10, 2019 at 18:7 Comment(1)
cobertura-merge doesn't merge results, but concatenates results. This doesn't help when multiple test runs are overlayed to get a merged coverage set.Luannaluanne
P
6

It seems you're using gcovr. Since version 4.2, gcovr can merge its JSON reports with the -a option. So you could do:

# run test1
./test1
gcovr ... --xml coverage-test1.xml --json coverage-test1.json
find . -type f -name '*.gcda' -exec rm {} +

# run test2
./test2
gcovr ... --xml coverage-test2.xml --json coverage-test2.json
find . -type f -name '*.gcda' -exec rm {} +

# combine JSON reports
gcovr -a coverage-test1.json -a coverage-test2.json --xml coverage-combined.xml
Position answered 1/2, 2020 at 13:0 Comment(1)
Is there any similar option available for merging xmls in node environmentStrikebound
D
4

I wrote python script which can merge cobertura reports together. It only merges line "hits" and is able to filter specific packages while merging.

Conditional(branch-rate) coverage and line coverage (line-rate) percentage values are not merged yet. Jenkins plugin recalculates these values so in this context it doesn't have to be implemented (but maybe it will).

Also conditional coverage can't seem to be put together easily, so it doesn't work when reports cooperate on final value (meaning one file has condition-coverage 50% (2/4) and other also condition-coverage 50% (2/4) other two branches. Result is set to be higher of these two numbers so it will be still 50% (2/4)

Dutchman answered 19/11, 2014 at 8:51 Comment(1)
Is it possible to merge all cobertura coverage xml reports together using PowerShell script? As i am not aware of Phyton language and also I will need to display the merged results on TFS 2017 Build summary.Uhf
D
2

I was looking at solution as well, but in my case there was about 1000 of Cobertura XML files and all above scripts were really slow in processing such amount.

In the end I've come up with merge-cobertura.py a Python script which does the merge. However it's quite limited in what it supports as all I needed was to merge reports from OpenCppCoverage to be used at Codecov. So it just merges line hits ignoring any branch information (there is none coming from OpenCppCoverage) and assuming all coverage reports are for same path. It also merges all packages into one.

Maybe somebody will find this useful as well, there are some more details in my blog.

Deterioration answered 30/3, 2017 at 8:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.