9

I am using genhtml command to generate html coverage report from Bazel generated coverage.dat file:

genhtml bazel-testlogs/path/to/TestTarget/coverage.dat --output-directory coverage

The problem with using genhtml is that I have to provide the paths to the coverage.dat files (which are generated in bazel-testlogs/..) Is it possible to fetch those coverage.dat files as an output from another rule?

I would like to not have to call genthml command directly, but have Bazel handle everything.

Zeitgeist
  • 1,382
  • 2
  • 16
  • 26

3 Answers3

12

I was not able to find a way to get coverage.dat files as an output of a bazel rule. However, I was able to wrap all the locations of all the .dat files as srcs to a filegroup in WORKSPACE directory:

filegroup(
    name = "coverage_files",
    srcs = glob(["bazel-out/**/coverage.dat"]),
)

and then use that filegroup in a custom .bzl rule that wraps the genthml command to generate html coverage report. So now I only have to call

bazel coverage //path/... --instrumentation_filter=/path[/:]

command to generate the coverage.dat files, generate html report and zip it up. Thus, bazel handles everything.

Zeitgeist
  • 1,382
  • 2
  • 16
  • 26
  • Can you show how you used the `filegroup` and `genhtml` in combination to generate the final `index.html` coverage report? Did you use `genrule`? I added both `filegroup` and `genrule` in `BUILD` file in the root directory of my workspace but nothing seems to work. – Pavan Manjunath Aug 12 '18 at 23:48
  • @PavanManjunath not able to find all the code. I do have a custom .bzl file that uses run_shell with cmd to run python ```coverage html``` command, which generates the html file. Not sure if this is helpful, let me know if you need more info. – Zeitgeist Jan 02 '19 at 16:52
  • @Zeitgeist i don't understand, you can't post the code? or you can't find it? can you please share how you generate the HTML? – Sam Gammon Feb 17 '20 at 21:42
  • @sgammon Correct, I do not have access to the code, have been on a different project for years. To generate HTML file, I downloaded `genhtml` executable using `http_file()` in WORKSPACE, and then I created a custom bzl rule in which I incorporated a cmd command that runs that executable. (Being this was done so long ago, there are probably better ways to do things now) – Zeitgeist Feb 19 '20 at 18:21
3

Bazel added support for C++ coverage (though I couldn't find much documentation for it).

I was able to generate a combined coverage.dat file with

bazel coverage -s \
  --instrument_test_targets \
  --experimental_cc_coverage \
  --combined_report=lcov \
  --coverage_report_generator=@bazel_tools//tools/test/CoverageOutputGenerator/java/com/google/devtools/coverageoutputgenerator:Main \
  //...

The coverage file gets added to bazel-out/_coverage/_coverage_report.dat

Eponymous
  • 6,143
  • 4
  • 43
  • 43
Ryan Burn
  • 2,126
  • 1
  • 14
  • 35
0

For Java based project we can get code coverage in following way

To get coverage for complete module ->

  1. Running coverage for complete project module. Run following command ->
bazel coverage ... --compilation_mode=dbg --subcommands --announce_rc --verbose_failures --jobs=auto  --sandbox_debug --build_runfile_links --combined_report=lcov --coverage_report_generator=@bazel_tools//tools/test/CoverageOutputGenerator/java/com/google/devtools/coverageoutputgenerator:Main
  1. Then run following command from parent project directory to get html view. Html report is generated in output-directory-name we specified. From that open index.html to see coverage report.
genhtml -o <output-directory-name> bazel-out/_coverage/_coverage_report.dat

bazel-out directory usually gets created in project parent directory(e.g. where bazel WORKSPACE file is present)

To get coverage for specific IT/Test in a module ->

  1. Running coverage for for specific IT/Test in a module. Run following command from project/sub-project directory ->
bazel coverage <class-name-of-Test-or-IT> --compilation_mode=dbg --subcommands --announce_rc --verbose_failures --jobs=auto  --sandbox_debug --build_runfile_links --combined_report=lcov --coverage_report_generator=@bazel_tools//tools/test/CoverageOutputGenerator/java/com/google/devtools/coverageoutputgenerator:Main
  1. Then run following command from parent project directory to get html view. Html report is generated in output-directory-name we specified. From that open index.html to see coverage report.
genhtml -o <output-directory-name> bazel-out/_coverage/_coverage_report.dat
rishi007bansod
  • 1,283
  • 2
  • 19
  • 45