Search code examples
c++googletestbazel

C++ project with Bazel and GTest


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)


Solution

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

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

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