[英]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.我有一个 C++ 项目,我想使用
bazel coverage
来获取它的代码覆盖率信息。
However, after running the command, I find the file coverage.dat
inside bazel-testlogs
does not contain anything.但是,运行命令后,我发现
bazel-testlogs
中的文件coverage.dat
不包含任何内容。
So am I looking at the right place?那我找对地方了吗? Or is there some problem with Bazel?
还是巴泽尔有问题?
I am using Bazel 1.0.0.我正在使用 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).运行
bazel coverage //test:my_test
,我在bazel-testlogs/test/my_test/coverage.dat
看到了一个文件,但它是空的(0 字节)。
Currently Bazel coverage <> is only support for Linux.目前 Bazel 覆盖 <> 仅支持 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 创建了一个用于导出 C++ 代码覆盖率的项目( https://github.com/hohaidang/STM32-from-scratch/tree/master/004_SPI_driver_CPP )打开 unit_test 文件夹以获取有关 BUILD 的更多详细信息
Bazel version 3.5.0 Bazel 版本 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.
如果不同的文件夹会生成 invalid.dat 文件。 I still don't know how to fix yet.
我仍然不知道如何修复。
Run bazel coverage
this way:以这种方式运行
bazel coverage
:
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 \
//...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.