现在有了googletest的BUILD文件,这变得更容易了:
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
git_repository(
name = "gtest",
remote = "https://github.com/google/googletest",
branch = "v1.10.x",
)
cc_test (
name = "hello_test",
srcs = [
"hello_test.cc",
],
deps = [
"@gtest//:gtest",
"@gtest//:gtest_main" # Only if hello_test.cc has no main()
],
)
.
├── bin
│ ├── BUILD
│ ├── hello.cpp
├── MyLib
│ ├── BUILD
│ ├── message.hpp
│ ├── message.cpp
│ ├── ...
├── test
│ ├── BUILD
│ ├── message_test.cpp
│ ├── ...
├── gmock.BUILD
└── WORKSPACE
Bazel+GTest相关文件
你可以从github下载gtest:
new_git_repository(
name = "googletest",
build_file = "gmock.BUILD",
remote = "https://github.com/google/googletest",
tag = "release-1.8.0",
)
cc_library(
name = "gtest",
srcs = [
"googletest/src/gtest-all.cc",
"googlemock/src/gmock-all.cc",
],
hdrs = glob([
"**/*.h",
"googletest/src/*.cc",
"googlemock/src/*.cc",
]),
includes = [
"googlemock",
"googletest",
"googletest/include",
"googlemock/include",
],
linkopts = ["-pthread"],
visibility = ["//visibility:public"],
)
cc_library(
name = "gtest_main",
srcs = ["googlemock/src/gmock_main.cc"],
linkopts = ["-pthread"],
visibility = ["//visibility:public"],
deps = [":gtest"],
)
该构建文件用于生成测试:
cc_test(
name = "MyTest",
srcs = glob(["**/*.cpp"]),
deps = ["//MyLib:MyLib",
"@googletest//:gtest_main"],
)
test/message_test.cpp文件的定义如下:
#include "gtest/gtest.h"
#include "MyLib/message.hpp"
TEST(message_test,content)
{
EXPECT_EQ(get_message(),"Hello World!");
}
就这些了!其他文件像往常一样定义:
支持示例的文件
创建libMyLib.so 和 libMyLib.a 库。
cc_library(
name="MyLib",
hdrs=glob(["**/*.hpp"]),
srcs=glob(["**/*.cpp"]),
visibility = ["//visibility:public"],
)
使用基本的 message.hpp
#include <string>
std::string get_message();
和 message.cpp 相关
#include "MyLib/message.hpp"
std::string get_message()
{
return "Hello World!";
}
例子。
创建 hello 可执行文件。
cc_binary(
name = "hello",
srcs = ["hello.cpp"],
deps = ["//MyLib:MyLib"],
)
which is:
#include "MyLib/message.hpp"
#include <iostream>
int main()
{
std::cout << "\n" << get_message() << std::endl;
return EXIT_SUCCESS;
}
用法:
这也将从其 github 存储库下载 gtest 并对其进行编译。
bazel build ...
您可以使用以下命令运行它:
bazel run bin:hello
这是本笔记的主要内容:
bazel test ... --test_output=errors
INFO: Analysed 3 targets (0 packages loaded).
INFO: Found 2 targets and 1 test target...
INFO: Elapsed time: 0.205s, Critical Path: 0.05s
INFO: Build completed successfully, 2 total actions
//test:MyTest
PASSED in 0.0s
Executed 1 out of 1 test: 1 test passes.
重现结果
为了方便起见,我创建了一个GitHub仓库来包含这个例子。希望它可以直接使用。
gtest
作为第三方库时,您只需要一个 git_repository
或 local_repository
规则即可。然后 bazel 就知道如何将 gtest 编译为库,因此您不需要显式地复制 BUILD
文件。 - Yixing Liu目前的推荐做法是使用http_archive
,以避免依赖于系统中的git
并利用存储库缓存。
WORKSPACE
中# 5376968f6948923e2411081fd9372e71a59d8e77 is the commit sha for v1.12.0.
# Periodically update to the latest to "live at head"
http_archive(
name = "com_google_googletest",
sha256 = "199e68f9dff997b30d420bf23cd9a0d3f66bfee4460e2cd95084a2c45ee00f1a",
strip_prefix = "googletest-5376968f6948923e2411081fd9372e71a59d8e77",
urls = ["https://github.com/google/googletest/archive/5376968f6948923e2411081fd9372e71a59d8e77.zip"],
)
test/BUILD
中cc_test(
name = "test_greet",
srcs = ["greeting_test.cpp"],
deps = [
"//src:greeting",
"@com_google_googletest//:gtest_main",
],
)
gmock.BUILD
文件。 - Dat