Using Bazel to generate coverage report
Asked Answered
I

3

10

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.

Indebtedness answered 27/9, 2017 at 11:54 Comment(3)
Hi, Have you ever met ERROR: output '_coverage/_coverage_report.dat' was not created when run bazel coverageSubantarctic
@Subantarctic Do not recall this particular error, this was a while ago...Indebtedness
Hi, Can you remember the version of gcc you use to compile?Subantarctic
I
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.

Indebtedness answered 2/10, 2017 at 14:3 Comment(4)
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.Culch
@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.Indebtedness
@Indebtedness 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?Maleficence
@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)Indebtedness
D
5

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

Dominy answered 22/12, 2018 at 23:40 Comment(4)
Which bazel version are you using?Epiboly
I tested with version 0.21.0Dominy
FWIW, your link is deadMcleroy
use bazel version 6.x to get the report .dat in $(bazel info output_path)/_coverage/_coverage_report.datFare
S
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
Savina answered 14/1, 2022 at 14:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.