我想用GTEST创建一个用于单位测试的Bazel C ++项目。

问题描述 投票:0回答:2
,我正在Linux下运行)

现在,Googletest提供了一个构建文件,这一点甚至更容易:

workspace

c++ googletest bazel
2个回答
30
投票
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


26
投票
workspace

您从github下载gtest:

new_git_repository( name = "googletest", build_file = "gmock.BUILD", remote = "https://github.com/google/googletest", tag = "release-1.8.0", )

您定义了下面定义的gmock构建文件:
  • gmock.build

该构建文件负责编译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!"); }
  • 就是这样!其他文件像往常一样定义: for支持示例的文件

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();
  • 和message.cpp

#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:


所有目标:

    这也将从其github存储库中下载gtest并编译IT
  • bazel build ...
检查Hello Target:

您可以使用以下方式运行以下操作:

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
。我希望它可以开箱即用。
    
  • current推荐的实践是要使用
  • 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"], )
  • 并利用
  • 重复的cache
.

test/BUILD

cc_test( name = "test_greet", srcs = ["greeting_test.cpp"], deps = [ "//src:greeting", "@com_google_googletest//:gtest_main", ], )
    

C++基于Bazel

的示例 这是基于Bazel和Google测试的C ++项目的示例存储库。它还介绍了如何跨平台进行编码和测试。我认为它可以满足您的需求。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.