4

I have a C program which I compile with -fprofile-arcs -ftest-coverage flags.Then I run the program on 5 different inputs, this will override the .gcda file and give me a combined report.But I want to have the coverage report of individual tests and store them in a folder and when I run any coverage tool on this folder I get report for each test as well as a combined report.Is there a way to do this?

VVish
  • 251
  • 4
  • 11

2 Answers2

4

Both gcovr and lcov can merge coverage data from multiple runs, but gcov has no built-in functionality.

Gcovr 5.0 added the -a/--add-tracefile option which can be used to merge multiple coverage runs. After each test, use gcovr to create a JSON report. Afterwards, you can use gcovr -a cov1.json -a cov2.json to merge multiple coverage data sets and generate a report in the format of your choosing. You can add as many input JSON files as you want, and use a glob pattern (like gcovr -a 'coverage-*.json') if you have many files.

You can also consider whether using the lcov tool with its --add-tracefile option would work: You can run lcov after each test to generate an lcov-tracefile (which you can turn into a HTML report with genhtml). Afterwards, you can merge the tracefiles into a combined report. It is not possible to use lcov's tracefiles with gcovr.

yugr
  • 19,769
  • 3
  • 51
  • 96
amon
  • 57,091
  • 2
  • 89
  • 149
  • Thanks for the answer. I tried with lcov and it works but what if I have hundreds of test cases then I need to add each tracefile individually and that is not convenient. I was thinking whether I should use some tools like TestWell CTC++ or RapiCover. – VVish Oct 13 '18 at 16:01
  • 1
    @Vatsal Re “I need to add each tracefile individually”: you can write a script to run the tests and add the tracefiles. Re “whether I should use some tools”: open source tools like lcov, gcov, and gcovr are mostly developed by hobbyists in their free time. Due to this lack of development resources they can be quite limited. (Disclosure: I am the gcovr maintainer.) It is very well possible that paid tools offer more convenient features, but I'm not familiar with them and suspect they can get rather expensive. – amon Oct 13 '18 at 16:24
  • 1
    thank you for your help @amon.I'll write the scripts to run tests and add the trace files as it seems a convenient and cheap option. And thanks for maintaining gcovr its a great tool. – VVish Oct 14 '18 at 17:09
  • 1
    "gcov has no built-in functionality" - I believe this is no longer true, please see another answer. – yugr Jan 09 '22 at 06:12
1

To add to another answer, gcov can also merge coverage data from multiple runs with the help of gcov-tool:

$ gcov-tool merge dir1 dir2

(by default results will be stored into merged_profile folder).

Unfortunately gcov-tool allows merging only two profiles at a time but you can use gcov-tool-many to work around this.

yugr
  • 19,769
  • 3
  • 51
  • 96