Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get C++ code coverage using Bazel?

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).

like image 600
Eric Stdlib Avatar asked Nov 07 '22 13:11

Eric Stdlib


1 Answers

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.

like image 167
Dang_Ho Avatar answered Nov 15 '22 07:11

Dang_Ho