C ++ project with Bazel and GTest

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

What is the minimum setting?

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

+4
source share
1 answer

Project Structure:

.
โ”œโ”€โ”€ bin
โ”‚   โ”œโ”€โ”€ BUILD
โ”‚   โ”œโ”€โ”€ hello.cpp
โ”œโ”€โ”€ MyLib
โ”‚   โ”œโ”€โ”€ BUILD
โ”‚   โ”œโ”€โ”€ message.hpp
โ”‚   โ”œโ”€โ”€ message.cpp
โ”‚   โ”œโ”€โ”€ ... 
โ”œโ”€โ”€ test
โ”‚   โ”œโ”€โ”€ BUILD
โ”‚   โ”œโ”€โ”€ message_test.cpp
โ”‚   โ”œโ”€โ”€ ... 
โ”œโ”€โ”€ gmock.BUILD
โ””โ”€โ”€ WORKSPACE

Bazel + GTest related files

  • 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 the gmock BUILD file specified below:

  • gmock.BUILD

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

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

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

.

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

:

  • :

gtest github repo

bazel build ...
  • :

:

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, . , .

+11

Source: https://habr.com/ru/post/1684214/


All Articles