4

I am able to generate coverage.dat files with bazel command:

bazel coverage //tests/... --instrumented_filter=/src[/:]

This generates report for one of the classes, because coverage.dat files are generated separately for each instrumented file in different directories. How do I get a merged coverage.dat?

Zeitgeist
  • 1,382
  • 2
  • 16
  • 26

1 Answers1

4

The coverage.dat report should contain coverage information about all the classes affected by the --instrumentation_filter. This file should be located under bazel-testlogs/path/to/your/package/TestTarget.

You shouldn't have to write anything additional. Bazel does generate multiple temporary .dat files, but it merges all of them in the final coverage.dat file, whose location is printed by bazel when it finishes to run. That file is the one with the location I described above. Make sure to check that file and check if you're using --instrumentation_filter (*) correctly.

(*) From the command line manual:

When coverage is enabled, only rules with names included by the specified regex- based filter will be instrumented. Rules prefixed with '-' are excluded instead. Note that only non-test rules are instrumented unless -- instrument_test_targets is enabled.

Irina Iancu
  • 186
  • 5
  • Ok, I figured out why my coverage.dat files didn't have correct info - as you said, the coverage.dat file is generated under TestTarget directory, and what I was doing is running each test class individually using java_test() rule as opposed to using Suit to combine all test classes into one. Once combined into one test class and ran from single java_test() rule, the coverage.dat was generated with all the coverage for all the files. – Zeitgeist Sep 25 '17 at 14:04
  • Does Bazel have means of generating actual index.html for the coverage? Right now I am using genhtml coverage.dat command to generate html coverage report from the coverage.dat that Bazel generated. – Zeitgeist Sep 26 '17 at 15:19
  • 1
    No, Bazel doesn't support generating index.html. Most people use genhtml with Bazel as well. – Irina Iancu Sep 27 '17 at 11:36
  • Thanks Irina. I posted another question about whether I can fetch .dat files as output from another Bazel rule here https://stackoverflow.com/questions/46447218/fetch-coverage-dat-files-as-an-output-from-a-bazel-rule – Zeitgeist Sep 27 '17 at 11:55
  • 1
    My bazel version is `Build label: 0.16.0` (timestamp 1533056484). But I don't see bazel printing out the path of the merged file. It only spits out paths of individual coverage files. – Pavan Manjunath Aug 10 '18 at 23:47
  • That is all well and good, however, i think you really DO need to manually combine dat files, because bazel has a 5yro outstanding bug where coverage doesn't respect the exclusive tag. So either you exclude "exclusive" tests, or you wait all day to run all your test cases on 1 cpu. Or, you run non-exclusive tests in parallel, and then exclusive testes on 1cpu, and then *merge* the dat file. But how? – Zendel Oct 19 '22 at 16:09
  • is there a way I can specify `-covermode` when running `bazel coverage` ?because at my end it is generating reports in multiple modes ex set, atomic and count and aggregated report is not getting generated with lcov as it needs all reports to be in one mode. or is there a workaround? – AhmFM Jan 11 '23 at 22:27