使用 Bazel 和 GTest 的 C++ 项目

C++ project with Bazel and GTest

我想用 gtest 创建一个 Bazel C++ 项目来进行单元测试。

什么是最小设置?

(我的电脑上只安装了Bazel,我在Linux下面是运行)

项目结构为:

.
├── 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",
)

您定义了一个 gmock BUILD 文件,定义如下:

  • gmock.BUILD

这个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"],
  )
  • test/BUILD

此构建文件生成测试:

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.solibMyLib.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!";
}

示例。

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

用法:

  • 编译所有目标:

这还将从其 github 存储库下载 gtest 并编译它

bazel build ...
  • 检查 hello 目标:

你可以运行它:

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.

重现结果

为了您的方便,我创建了一个包含此示例的 github repo。我希望它开箱即用。

现在 googletest 提供了一个 BUILD 文件,这就更容易了:

WORKSPACE

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()
    ],
)