C++ project with Bazel and GTest

匿名 (未验证) 提交于 2019-12-03 01:33:01

问题:

I want to create a Bazel C++ project with gtest for unit tests.

What is the minimal setup?

(I only have Bazel installed on my computer and I am running under Linux)

回答1:

The project structure is:

Files related to Bazel+GTest

  • WORKSPACE

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 a gmock BUILD file defined below:

  • gmock.BUILD

This BUILD file is in charge of 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"],   ) 
  • test/BUILD

This build file generate the tests:

cc_test(     name = "MyTest",     srcs = glob(["**/*.cpp"]),     deps = ["//MyLib:MyLib",            "@googletest//:gtest_main"], ) 

The test/message_test.cpp file is defined by:

#include "gtest/gtest.h"  #include "MyLib/message.hpp"  TEST(message_test,content) {   EXPECT_EQ(get_message(),"Hello World!"); } 

And that is all! The other files are defined as usual:

Files for the supporting example

  • MyLib/BUILD

Creates the libMyLib.so and libMyLib.a libraries.

cc_library(     name="MyLib",     hdrs=glob(["**/*.hpp"]),     srcs=glob(["**/*.cpp"]),     visibility = ["//visibility:public"], ) 

with a basic message.hpp

#include <string>  std::string get_message(); 

and message.cpp

#include "MyLib/message.hpp"  std::string get_message() {    return "Hello World!"; } 

example.

  • bin/BUILD

Creates the hello executable.

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; } 

Usage:

  • Compiles all targets:

This will also download gtest from its github repo and compile it

bazel build ... 
  • Checks the hello target:

You can run it with:

bazel run bin:hello 
  • Running your tests using GTest

That was the main point of this note:

bazel test ... --test_output=errors 

You should get something like:

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. 

Reproduce the results

For your ease I have created a github repo containing this example. I hope it works out of the box.



回答2:

This is even easier now that googletest provides a BUILD file:

In WORKSPACE

git_repository(     name = "gtest",     remote = "https://github.com/google/googletest",     commit = "3306848f697568aacf4bcca330f6bdd5ce671899", ) 

In BUILD

cc_test (     name = "hello_test",     srcs = [         "hello_test.cc",     ],     deps = [         "@gtest//:gtest",         "@gtest//:gtest_main" # Only if hello_test.cc has no main()     ], ) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!