Quickstart: Building with Bazel
To complete this tutorial, you’ll need:
- A compatible operating system (e.g. Linux, macOS, Windows).
- Bazel, the preferred build system used by the GoogleTest team.
See for more information about platforms compatible with GoogleTest.
If you don’t already have Bazel installed, see the Bazel installation guide.
Note: The terminal commands in this tutorial show a Unix shell prompt, but the commands work on the Windows command line as well.
First, create a directory for your workspace:
Next, you’ll create the WORKSPACE
file to specify dependencies. A common and recommended way to depend on GoogleTest is to use a 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:
The above configuration declares a dependency on GoogleTest which is downloaded as a ZIP archive from GitHub. In the above example, 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.
As an example, create a file named hello_test.cc
in your my_workspace
directory with the following contents:
GoogleTest provides 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:
This 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 files, see the Bazel C++ Tutorial.
Congratulations! You’ve successfully built and run a test binary using GoogleTest.
- to start learning how to write simple tests.