Project Structure:
.
โโโ bin
โ โโโ BUILD
โ โโโ hello.cpp
โโโ MyLib
โ โโโ BUILD
โ โโโ message.hpp
โ โโโ message.cpp
โ โโโ ...
โโโ test
โ โโโ BUILD
โ โโโ message_test.cpp
โ โโโ ...
โโโ gmock.BUILD
โโโ WORKSPACE
Bazel + GTest related files
There you download gtest from github:
new_git_repository(
name = "googletest",
build_file = "gmock.BUILD",
remote = "https://github.com/google/googletest",
tag = "release-1.8.0",
)
You define the gmock BUILD file specified below:
This BUILD file is responsible for compiling 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"],
)
This build file generates tests:
cc_test(
name = "MyTest",
srcs = glob(["**/*.cpp"]),
deps = ["//MyLib:MyLib",
"@googletest//:gtest_main"],
)
The file test / message_test.cpp is defined as follows:
#include "gtest/gtest.h"
#include "MyLib/message.hpp"
TEST(message_test,content)
{
EXPECT_EQ(get_message(),"Hello World!");
}
And it's all! Other files are defined as usual:
Files for helper example
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"],
)
:
#include "MyLib/message.hpp"
#include <iostream>
int main()
{
std::cout << "\n" << get_message() << std::endl;
return EXIT_SUCCESS;
}
:
gtest github repo
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 repo, . , .