具有Bazel和GTest的C ++项目 [英] C++ project with Bazel and GTest

查看:276
本文介绍了具有Bazel和GTest的C ++项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用gtest创建一个用于单元测试的Bazel C ++项目.

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

最小设置是什么?

(我的计算机上仅安装了 Bazel ,并且我在Linux下运行)

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

推荐答案

项目结构为:

.
├── bin
│   ├── BUILD
│   ├── hello.cpp
├── MyLib
│   ├── BUILD
│   ├── message.hpp
│   ├── message.cpp
│   ├── ... 
├── test
│   ├── BUILD
│   ├── message_test.cpp
│   ├── ... 
├── gmock.BUILD
└── WORKSPACE

与Bazel + GTest相关的文件

  • WORKSPACE

您从github下载了gtest:

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",
)

您定义了以下定义的gmock BUILD文件:

You define a gmock BUILD file defined below:

  • gmock.BUILD

此BUILD文件负责编译gtest/gmock:

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
    • 此构建文件生成测试:

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

      仅此而已!其他文件的定义与往常一样:

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

      支持示例的文件

      • MyLib/BUILD

      创建 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!";
      }
      

      示例.

      • 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并进行编译

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

      bazel build ...
      

      • 检查问候目标:
      • 您可以通过以下方式运行它:

        You can run it with:

        bazel run bin:hello
        

        • 使用GTest运行测试
        • 这是本笔记的重点:

          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.
          

          再现结果

          为了您的方便,我创建了一个包含此示例的 github存储库.我希望它能开箱即用.

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

          这篇关于具有Bazel和GTest的C ++项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆