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