1*28f6c2f2SEnji Cooper# Quickstart: Building with Bazel 2*28f6c2f2SEnji Cooper 3*28f6c2f2SEnji CooperThis tutorial aims to get you up and running with GoogleTest using the Bazel 4*28f6c2f2SEnji Cooperbuild system. If you're using GoogleTest for the first time or need a refresher, 5*28f6c2f2SEnji Cooperwe recommend this tutorial as a starting point. 6*28f6c2f2SEnji Cooper 7*28f6c2f2SEnji Cooper## Prerequisites 8*28f6c2f2SEnji Cooper 9*28f6c2f2SEnji CooperTo complete this tutorial, you'll need: 10*28f6c2f2SEnji Cooper 11*28f6c2f2SEnji Cooper* A compatible operating system (e.g. Linux, macOS, Windows). 12*28f6c2f2SEnji Cooper* A compatible C++ compiler that supports at least C++14. 13*28f6c2f2SEnji Cooper* [Bazel](https://bazel.build/), the preferred build system used by the 14*28f6c2f2SEnji Cooper GoogleTest team. 15*28f6c2f2SEnji Cooper 16*28f6c2f2SEnji CooperSee [Supported Platforms](platforms.md) for more information about platforms 17*28f6c2f2SEnji Coopercompatible with GoogleTest. 18*28f6c2f2SEnji Cooper 19*28f6c2f2SEnji CooperIf you don't already have Bazel installed, see the 20*28f6c2f2SEnji Cooper[Bazel installation guide](https://bazel.build/install). 21*28f6c2f2SEnji Cooper 22*28f6c2f2SEnji Cooper{: .callout .note} Note: The terminal commands in this tutorial show a Unix 23*28f6c2f2SEnji Coopershell prompt, but the commands work on the Windows command line as well. 24*28f6c2f2SEnji Cooper 25*28f6c2f2SEnji Cooper## Set up a Bazel workspace 26*28f6c2f2SEnji Cooper 27*28f6c2f2SEnji CooperA 28*28f6c2f2SEnji Cooper[Bazel workspace](https://docs.bazel.build/versions/main/build-ref.html#workspace) 29*28f6c2f2SEnji Cooperis a directory on your filesystem that you use to manage source files for the 30*28f6c2f2SEnji Coopersoftware you want to build. Each workspace directory has a text file named 31*28f6c2f2SEnji Cooper`WORKSPACE` which may be empty, or may contain references to external 32*28f6c2f2SEnji Cooperdependencies required to build the outputs. 33*28f6c2f2SEnji Cooper 34*28f6c2f2SEnji CooperFirst, create a directory for your workspace: 35*28f6c2f2SEnji Cooper 36*28f6c2f2SEnji Cooper``` 37*28f6c2f2SEnji Cooper$ mkdir my_workspace && cd my_workspace 38*28f6c2f2SEnji Cooper``` 39*28f6c2f2SEnji Cooper 40*28f6c2f2SEnji CooperNext, you’ll create the `WORKSPACE` file to specify dependencies. A common and 41*28f6c2f2SEnji Cooperrecommended way to depend on GoogleTest is to use a 42*28f6c2f2SEnji Cooper[Bazel external dependency](https://docs.bazel.build/versions/main/external.html) 43*28f6c2f2SEnji Coopervia the 44*28f6c2f2SEnji Cooper[`http_archive` rule](https://docs.bazel.build/versions/main/repo/http.html#http_archive). 45*28f6c2f2SEnji CooperTo do this, in the root directory of your workspace (`my_workspace/`), create a 46*28f6c2f2SEnji Cooperfile named `WORKSPACE` with the following contents: 47*28f6c2f2SEnji Cooper 48*28f6c2f2SEnji Cooper``` 49*28f6c2f2SEnji Cooperload("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") 50*28f6c2f2SEnji Cooper 51*28f6c2f2SEnji Cooperhttp_archive( 52*28f6c2f2SEnji Cooper name = "com_google_googletest", 53*28f6c2f2SEnji Cooper urls = ["https://github.com/google/googletest/archive/5ab508a01f9eb089207ee87fd547d290da39d015.zip"], 54*28f6c2f2SEnji Cooper strip_prefix = "googletest-5ab508a01f9eb089207ee87fd547d290da39d015", 55*28f6c2f2SEnji Cooper) 56*28f6c2f2SEnji Cooper``` 57*28f6c2f2SEnji Cooper 58*28f6c2f2SEnji CooperThe above configuration declares a dependency on GoogleTest which is downloaded 59*28f6c2f2SEnji Cooperas a ZIP archive from GitHub. In the above example, 60*28f6c2f2SEnji Cooper`5ab508a01f9eb089207ee87fd547d290da39d015` is the Git commit hash of the 61*28f6c2f2SEnji CooperGoogleTest version to use; we recommend updating the hash often to point to the 62*28f6c2f2SEnji Cooperlatest version. Use a recent hash on the `main` branch. 63*28f6c2f2SEnji Cooper 64*28f6c2f2SEnji CooperNow you're ready to build C++ code that uses GoogleTest. 65*28f6c2f2SEnji Cooper 66*28f6c2f2SEnji Cooper## Create and run a binary 67*28f6c2f2SEnji Cooper 68*28f6c2f2SEnji CooperWith your Bazel workspace set up, you can now use GoogleTest code within your 69*28f6c2f2SEnji Cooperown project. 70*28f6c2f2SEnji Cooper 71*28f6c2f2SEnji CooperAs an example, create a file named `hello_test.cc` in your `my_workspace` 72*28f6c2f2SEnji Cooperdirectory with the following contents: 73*28f6c2f2SEnji Cooper 74*28f6c2f2SEnji Cooper```cpp 75*28f6c2f2SEnji Cooper#include <gtest/gtest.h> 76*28f6c2f2SEnji Cooper 77*28f6c2f2SEnji Cooper// Demonstrate some basic assertions. 78*28f6c2f2SEnji CooperTEST(HelloTest, BasicAssertions) { 79*28f6c2f2SEnji Cooper // Expect two strings not to be equal. 80*28f6c2f2SEnji Cooper EXPECT_STRNE("hello", "world"); 81*28f6c2f2SEnji Cooper // Expect equality. 82*28f6c2f2SEnji Cooper EXPECT_EQ(7 * 6, 42); 83*28f6c2f2SEnji Cooper} 84*28f6c2f2SEnji Cooper``` 85*28f6c2f2SEnji Cooper 86*28f6c2f2SEnji CooperGoogleTest provides [assertions](primer.md#assertions) that you use to test the 87*28f6c2f2SEnji Cooperbehavior of your code. The above sample includes the main GoogleTest header file 88*28f6c2f2SEnji Cooperand demonstrates some basic assertions. 89*28f6c2f2SEnji Cooper 90*28f6c2f2SEnji CooperTo build the code, create a file named `BUILD` in the same directory with the 91*28f6c2f2SEnji Cooperfollowing contents: 92*28f6c2f2SEnji Cooper 93*28f6c2f2SEnji Cooper``` 94*28f6c2f2SEnji Coopercc_test( 95*28f6c2f2SEnji Cooper name = "hello_test", 96*28f6c2f2SEnji Cooper size = "small", 97*28f6c2f2SEnji Cooper srcs = ["hello_test.cc"], 98*28f6c2f2SEnji Cooper deps = ["@com_google_googletest//:gtest_main"], 99*28f6c2f2SEnji Cooper) 100*28f6c2f2SEnji Cooper``` 101*28f6c2f2SEnji Cooper 102*28f6c2f2SEnji CooperThis `cc_test` rule declares the C++ test binary you want to build, and links to 103*28f6c2f2SEnji CooperGoogleTest (`//:gtest_main`) using the prefix you specified in the `WORKSPACE` 104*28f6c2f2SEnji Cooperfile (`@com_google_googletest`). For more information about Bazel `BUILD` files, 105*28f6c2f2SEnji Coopersee the 106*28f6c2f2SEnji Cooper[Bazel C++ Tutorial](https://docs.bazel.build/versions/main/tutorial/cpp.html). 107*28f6c2f2SEnji Cooper 108*28f6c2f2SEnji Cooper{: .callout .note} 109*28f6c2f2SEnji CooperNOTE: In the example below, we assume Clang or GCC and set `--cxxopt=-std=c++14` 110*28f6c2f2SEnji Cooperto ensure that GoogleTest is compiled as C++14 instead of the compiler's default 111*28f6c2f2SEnji Coopersetting (which could be C++11). For MSVC, the equivalent would be 112*28f6c2f2SEnji Cooper`--cxxopt=/std:c++14`. See [Supported Platforms](platforms.md) for more details 113*28f6c2f2SEnji Cooperon supported language versions. 114*28f6c2f2SEnji Cooper 115*28f6c2f2SEnji CooperNow you can build and run your test: 116*28f6c2f2SEnji Cooper 117*28f6c2f2SEnji Cooper<pre> 118*28f6c2f2SEnji Cooper<strong>my_workspace$ bazel test --cxxopt=-std=c++14 --test_output=all //:hello_test</strong> 119*28f6c2f2SEnji CooperINFO: Analyzed target //:hello_test (26 packages loaded, 362 targets configured). 120*28f6c2f2SEnji CooperINFO: Found 1 test target... 121*28f6c2f2SEnji CooperINFO: From Testing //:hello_test: 122*28f6c2f2SEnji Cooper==================== Test output for //:hello_test: 123*28f6c2f2SEnji CooperRunning main() from gmock_main.cc 124*28f6c2f2SEnji Cooper[==========] Running 1 test from 1 test suite. 125*28f6c2f2SEnji Cooper[----------] Global test environment set-up. 126*28f6c2f2SEnji Cooper[----------] 1 test from HelloTest 127*28f6c2f2SEnji Cooper[ RUN ] HelloTest.BasicAssertions 128*28f6c2f2SEnji Cooper[ OK ] HelloTest.BasicAssertions (0 ms) 129*28f6c2f2SEnji Cooper[----------] 1 test from HelloTest (0 ms total) 130*28f6c2f2SEnji Cooper 131*28f6c2f2SEnji Cooper[----------] Global test environment tear-down 132*28f6c2f2SEnji Cooper[==========] 1 test from 1 test suite ran. (0 ms total) 133*28f6c2f2SEnji Cooper[ PASSED ] 1 test. 134*28f6c2f2SEnji Cooper================================================================================ 135*28f6c2f2SEnji CooperTarget //:hello_test up-to-date: 136*28f6c2f2SEnji Cooper bazel-bin/hello_test 137*28f6c2f2SEnji CooperINFO: Elapsed time: 4.190s, Critical Path: 3.05s 138*28f6c2f2SEnji CooperINFO: 27 processes: 8 internal, 19 linux-sandbox. 139*28f6c2f2SEnji CooperINFO: Build completed successfully, 27 total actions 140*28f6c2f2SEnji Cooper//:hello_test PASSED in 0.1s 141*28f6c2f2SEnji Cooper 142*28f6c2f2SEnji CooperINFO: Build completed successfully, 27 total actions 143*28f6c2f2SEnji Cooper</pre> 144*28f6c2f2SEnji Cooper 145*28f6c2f2SEnji CooperCongratulations! You've successfully built and run a test binary using 146*28f6c2f2SEnji CooperGoogleTest. 147*28f6c2f2SEnji Cooper 148*28f6c2f2SEnji Cooper## Next steps 149*28f6c2f2SEnji Cooper 150*28f6c2f2SEnji Cooper* [Check out the Primer](primer.md) to start learning how to write simple 151*28f6c2f2SEnji Cooper tests. 152*28f6c2f2SEnji Cooper* [See the code samples](samples.md) for more examples showing how to use a 153*28f6c2f2SEnji Cooper variety of GoogleTest features. 154