6

I have a C++ project, and I want to use bazel coverage to get its code coverage information.

However, after running the command, I find the file coverage.dat inside bazel-testlogs does not contain anything.

So am I looking at the right place? Or is there some problem with Bazel?

I am using Bazel 1.0.0.

Complete Example

WORKSPACE

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

git_repository(
    name = "gtest",
    remote = "https://github.com/google/googletest",
    commit = "3306848f697568aacf4bcca330f6bdd5ce671899",
)

lib/a.cc

int f(int x) {
    if (x == 0)
        return x + 1;
    else
        return 1 + x;
}

lib/BUILD

cc_library(
    name = "a",
    srcs = ["a.cc"],
    visibility = ["//test:__pkg__"],
)

test/my_test.cc

#include "gtest/gtest.h"

TEST(FactorialTest, Negative) {
  EXPECT_EQ(1, 1);
}

test/BUILD

cc_test(
    name = "my_test",
    srcs = ["my_test.cc"],
    copts = ["-Iexternal/gtest"],
    deps = ["@gtest//:gtest_main", "//lib:a"],
)

After I run bazel coverage //test:my_test, I see a file at bazel-testlogs/test/my_test/coverage.dat generated, but it is empty (0 bytes).

1
  • 1
    github.com/bazelbuild/bazel/issues/9406 There is already an issue for that exact same problem. Unfortunately the bazel developers currently do not actively maintain the coverage functionality.
    – Sagre
    Oct 15, 2019 at 12:50

2 Answers 2

2

Currently Bazel coverage <> is only support for Linux.

I created a project using bazel for exporting C++ code coverage (https://github.com/hohaidang/STM32-from-scratch/tree/master/004_SPI_driver_CPP) Open the unit_test folder for more details about BUILD file

Bazel version 3.5.0

$ bazel coverage unit_test:ut_gpio --combined_report=lcov

$ genhtml bazel-out/_coverage/_coverage_report.dat -o test_coverage

Note: you should put all test files and source files in the same folder. If different folder it will generate invalid .dat file. I still don't know how to fix yet.

0

Run bazel coverage this way:

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 \
    //...
1
  • 4
    If I just run the command with target //test:my_test, I get ERROR: output '_coverage/_coverage_report.dat' was not created ERROR: not all outputs were created or valid Oct 31, 2019 at 4:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.