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",
)
build
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有关的files
您从github下载gtest:
new_git_repository(
name = "googletest",
build_file = "gmock.BUILD",
remote = "https://github.com/google/googletest",
tag = "release-1.8.0",
)
该构建文件负责编译GTEST/GMOCK:
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!");
}
Mylib/build
创建
libmylib.so和libmylib.a
库。
cc_library(
name="MyLib",
hdrs=glob(["**/*.hpp"]),
srcs=glob(["**/*.cpp"]),
visibility = ["//visibility:public"],
)
以基本的mess.hpp
#include <string>
std::string get_message();
#include "MyLib/message.hpp"
std::string get_message()
{
return "Hello World!";
}
example.
bin/build
创建hello可执行。
cc_binary(
name = "hello",
srcs = ["hello.cpp"],
deps = ["//MyLib:MyLib"],
)
是:
#include "MyLib/message.hpp"
#include <iostream>
int main()
{
std::cout << "\n" << get_message() << std::endl;
return EXIT_SUCCESS;
}
usage:
所有目标:
bazel build ...
您可以使用以下方式运行以下操作:
bazel run bin:hello
使用GTEST进行测试
这是此注释的重点: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.
为您的放松,我创建了一个包含此示例的
githubrepo。我希望它可以开箱即用。
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",
],
)
C++基于Bazel
的示例 这是基于Bazel和Google测试的C ++项目的示例存储库。它还介绍了如何跨平台进行编码和测试。我认为它可以满足您的需求。