pycobertura icon indicating copy to clipboard operation
pycobertura copied to clipboard

Support for merge cobertura.xml files.

Open marwin1991 opened this issue 2 years ago • 16 comments

Hello,

the functionality that will allow to merge many cobertura files would be great. This problem is common:

https://stackoverflow.com/questions/64370596/can-i-combine-mypy-reports-with-pytest-coverage-reports https://stackoverflow.com/questions/26469072/is-there-anyway-to-merge-cobertura-coverage-xml-reports-together

And there is even a npm module that solves it, but it will need to install whole node on python based Docker image to use it. https://www.npmjs.com/package/cobertura-merge

What do you think about it??

marwin1991 avatar Nov 24 '21 12:11 marwin1991

Hi @marwin1991 What are the exact rules for merging, if I know them, then I can implement it :)

gro1m avatar Feb 09 '22 19:02 gro1m

That sounds like a whole new utility to me. Such as pycobertura merge...

aconrad avatar Feb 10 '22 16:02 aconrad

I guess the logic is:

  • if coverage1.xml has line <line filename="foo" hits="0"> and coverage2.xml has <line filename="foo" hits="1"> then the resulting merge would have <line filename="foo" hits="1">.
  • coverage rates should get recomputed based on the new total number of lines in the merged report.
  • I think we'd probably want to have the ability to merge 2 Cobertura files that aren't part of the same code base or that have no file paths in common, e.g. the same code base could host C files and Ruby files but the report from each language will generate 2 very different coverage reports.

aconrad avatar Feb 10 '22 16:02 aconrad

Great idea! IMO the most common example is when you have f.e. JS in frontend and Java/Python/Ruby in backend. When it comes to visualise the result in GitLab you need one file

marwin1991 avatar Feb 10 '22 16:02 marwin1991

@marwin1991 Could provide exemplary test input files and a test output file? I think this would be most helpful...

gro1m avatar Mar 12 '22 18:03 gro1m

Hello, here I have a simple cobertura.xml for spring boot java project: cobertura.xml.txt

I will try to get some example of JS like React or Angular :)

marwin1991 avatar Mar 14 '22 20:03 marwin1991

Hi @marwin1991 Thanks, would be good if you could also provide the wished output file with the 2 input files you provide :) Also from your example, I assume that this is also relevant for you: https://github.com/aconrad/pycobertura/issues/118.

gro1m avatar Mar 14 '22 21:03 gro1m

Hi @aconrad @marwin1991 Regarding the implementation of the merge command, there are the following steps in my view:

  1. Parsing the input xml files (could be an arbitrary number) and extract the statistics.
  2. Merge the statistics using a merge function
  3. Write the statistics in an output xml file (whose name is also given on the command line, would be the last parameter).4.

Am I correct?

And @aconrad: What do we already have for the first part?

gro1m avatar Mar 15 '22 18:03 gro1m

For 1. I think we have most of it implemented, pycobertura is first a cobertura parser.

I believe that coveragepy has a functionality to merge/combine coverage reports. Is this relevant to us? https://coverage.readthedocs.io/en/stable/cmd.html#cmd-combine

Could users use that before using pycobertura? If yes, then maybe we don't need to implement this feature, although I'm not against it either. We just need to consider what our options are. Thoughts?

aconrad avatar Mar 22 '22 15:03 aconrad

@marwin1991 Can you explain why coverage combine does not satisfy your needs or why the integration into pycobertura would be extremely helpful? - see also the link by @aconrad above: https://coverage.readthedocs.io/en/stable/cmd.html#cmd-combine.

gro1m avatar Apr 18 '22 09:04 gro1m

As far as I can tell, coverage combine doesn't merge XML cobertura files.

epenet avatar Jan 24 '23 13:01 epenet

As far as I can tell, coverage combine doesn't merge XML cobertura files.

Hm, I think you're right. It might only combine coverage data generated by coverage.py but not take other formats as input.

aconrad avatar Jan 25 '23 20:01 aconrad

I'd be okay to introduce a pycobertura merge command. Although I don't have much time to work on it so PRs are welcome.

aconrad avatar Jan 25 '23 20:01 aconrad

I needed to do this as a one-off, and I tweaked this script as a basis: https://github.com/janhybs/Flow123d-python-utils/blob/master/src/coverage/coverage_merge_module.py

This is what I ended up doing on Home Assistant (they split tests into 10 groups so they can run on separate VM): Step 1 => download 10 zip artifacts from github Step 2 => extract the 10 xml files from the 10 zip files Step 3 => use the script above to merge the 10 xml files Step 4 => run a custom script to "fix" the line-rate as the script kept the original values Step 5 => run pycobertura show to display the result

epenet avatar Jan 25 '23 21:01 epenet

I needed to do this as a one-off, and I tweaked this script as a basis: https://github.com/janhybs/Flow123d-python-utils/blob/master/src/coverage/coverage_merge_module.py

This is what I ended up doing on Home Assistant (they split tests into 10 groups so they can run on separate VM): Step 1 => download 10 zip artifacts from github Step 2 => extract the 10 xml files from the 10 zip files Step 3 => use the script above to merge the 10 xml files Step 4 => run a custom script to "fix" the line-rate as the script kept the original values Step 5 => run pycobertura show to display the result

Could please upload the input xml files and the output xml file somewhere.

gro1m avatar Feb 11 '23 18:02 gro1m

Sadly I have managed to loose my merge script, my input files and my output file in a messed up GIT rebase.

You can download some fresh input XML files from https://github.com/home-assistant/core/actions/runs/4161973175 (see the 12 artifacts at the bottom)

epenet avatar Feb 13 '23 10:02 epenet