Using Bazel to generate coverage report

2019-05-02 10:55发布

问题:

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.

回答1:

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.



回答2:

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