Quickstart: Building with Bazel

This tutorial aims to get you up and running with GoogleTest using the Bazel
build system. If you’re using GoogleTest for the first time or need a refresher,
we recommend this tutorial as a starting point.

Prerequisites

To complete this tutorial, you’ll need:

  • A compatible operating system (e.g. Linux, macOS, Windows).
  • A compatible C++ compiler that supports at least C++11.
  • Bazel, the preferred build system used by the
    GoogleTest team.

See Supported Platforms for more information about platforms
compatible with GoogleTest.

If you don’t already have Bazel installed, see the
Bazel installation guide.

{: .callout .note}
Note: The terminal commands in this tutorial show a Unix shell prompt, but the
commands work on the Windows command line as well.

Set up a Bazel workspace

A
Bazel workspace
is a directory on your filesystem that you use to manage source files for the
software you want to build. Each workspace directory has a text file named
WORKSPACE which may be empty, or may contain references to external
dependencies required to build the outputs.

First, create a directory for your workspace:

  1. $ mkdir my_workspace && cd my_workspace

Next, you’ll create the WORKSPACE file to specify dependencies. A common and
recommended way to depend on GoogleTest is to use a
Bazel external dependency
via the
http_archive rule.
To do this, in the root directory of your workspace (my_workspace/), create a
file named WORKSPACE with the following contents:

  1. load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
  2. http_archive(
  3. name = "com_google_googletest",
  4. urls = ["https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip"],
  5. strip_prefix = "googletest-609281088cfefc76f9d0ce82e1ff6c30cc3591e5",
  6. )

The above configuration declares a dependency on GoogleTest which is downloaded
as a ZIP archive from GitHub. In the above example,
609281088cfefc76f9d0ce82e1ff6c30cc3591e5 is the Git commit hash of the
GoogleTest version to use; we recommend updating the hash often to point to the
latest version.

Now you’re ready to build C++ code that uses GoogleTest.

Create and run a binary

With your Bazel workspace set up, you can now use GoogleTest code within your
own project.

As an example, create a file named hello_test.cc in your my_workspace
directory with the following contents:

  1. #include <gtest/gtest.h>
  2. // Demonstrate some basic assertions.
  3. TEST(HelloTest, BasicAssertions) {
  4. // Expect two strings not to be equal.
  5. EXPECT_STRNE("hello", "world");
  6. // Expect equality.
  7. EXPECT_EQ(7 * 6, 42);
  8. }

GoogleTest provides assertions that you use to test the
behavior of your code. The above sample includes the main GoogleTest header file
and demonstrates some basic assertions.

To build the code, create a file named BUILD in the same directory with the
following contents:

  1. cc_test(
  2. name = "hello_test",
  3. size = "small",
  4. srcs = ["hello_test.cc"],
  5. deps = ["@com_google_googletest//:gtest_main"],
  6. )

This cc_test rule declares the C++ test binary you want to build, and links to
GoogleTest (//:gtest_main) using the prefix you specified in the WORKSPACE
file (@com_google_googletest). For more information about Bazel BUILD files,
see the
Bazel C++ Tutorial.

Now you can build and run your test:

my_workspace$ bazel test —test_output=all //:hello_test Congratulations! You’ve successfully built and run a test binary using
GoogleTest.

Next steps