Skip to content

Commit 9ff2450

Browse files
derekmaurocopybara-github
authored andcommitted
Update GoogleTest Bazel quickstart for Bzlmod
PiperOrigin-RevId: 652824490 Change-Id: I5e6f57004708e7fa62abb454db9bae81fa265c83
1 parent b62593a commit 9ff2450

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

docs/quickstart-bazel.md

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ To complete this tutorial, you'll need:
1010

1111
* A compatible operating system (e.g. Linux, macOS, Windows).
1212
* A compatible C++ compiler that supports at least C++14.
13-
* [Bazel](https://bazel.build/), the preferred build system used by the
14-
GoogleTest team.
13+
* [Bazel](https://bazel.build/) 7.0 or higher, the preferred build system used
14+
by the GoogleTest team.
1515

1616
See [Supported Platforms](platforms.md) for more information about platforms
1717
compatible with GoogleTest.
@@ -28,7 +28,7 @@ A
2828
[Bazel workspace](https://docs.bazel.build/versions/main/build-ref.html#workspace)
2929
is a directory on your filesystem that you use to manage source files for the
3030
software you want to build. Each workspace directory has a text file named
31-
`WORKSPACE` which may be empty, or may contain references to external
31+
`MODULE.bazel` which may be empty, or may contain references to external
3232
dependencies required to build the outputs.
3333

3434
First, create a directory for your workspace:
@@ -37,30 +37,20 @@ First, create a directory for your workspace:
3737
$ mkdir my_workspace && cd my_workspace
3838
```
3939

40-
Next, you’ll create the `WORKSPACE` file to specify dependencies. A common and
41-
recommended way to depend on GoogleTest is to use a
42-
[Bazel external dependency](https://docs.bazel.build/versions/main/external.html)
43-
via the
44-
[`http_archive` rule](https://docs.bazel.build/versions/main/repo/http.html#http_archive).
45-
To do this, in the root directory of your workspace (`my_workspace/`), create a
46-
file named `WORKSPACE` with the following contents:
40+
Next, you’ll create the `MODULE.bazel` file to specify dependencies. As of Bazel
41+
7.0, the recommended way to consume GoogleTest is through the
42+
[Bazel Central Registry](https://registry.bazel.build/modules/googletest). To do
43+
this, create a `MODULE.bazel` file in the root directory of your Bazel workspace
44+
with the following content:
4745

4846
```
49-
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
47+
# MODULE.bazel
5048
51-
http_archive(
52-
name = "com_google_googletest",
53-
urls = ["https://github.com/google/googletest/archive/5ab508a01f9eb089207ee87fd547d290da39d015.zip"],
54-
strip_prefix = "googletest-5ab508a01f9eb089207ee87fd547d290da39d015",
55-
)
49+
# Choose the most recent version available at
50+
# https://registry.bazel.build/modules/googletest
51+
bazel_dep(name = "googletest", version = "1.15.0")
5652
```
5753

58-
The above configuration declares a dependency on GoogleTest which is downloaded
59-
as a ZIP archive from GitHub. In the above example,
60-
`5ab508a01f9eb089207ee87fd547d290da39d015` is the Git commit hash of the
61-
GoogleTest version to use; we recommend updating the hash often to point to the
62-
latest version. Use a recent hash on the `main` branch.
63-
6454
Now you're ready to build C++ code that uses GoogleTest.
6555

6656
## Create and run a binary
@@ -92,17 +82,20 @@ following contents:
9282
9383
```
9484
cc_test(
95-
name = "hello_test",
96-
size = "small",
97-
srcs = ["hello_test.cc"],
98-
deps = ["@com_google_googletest//:gtest_main"],
85+
name = "hello_test",
86+
size = "small",
87+
srcs = ["hello_test.cc"],
88+
deps = [
89+
"@googletest//:gtest",
90+
"@googletest//:gtest_main",
91+
],
9992
)
10093
```
10194

10295
This `cc_test` rule declares the C++ test binary you want to build, and links to
103-
GoogleTest (`//:gtest_main`) using the prefix you specified in the `WORKSPACE`
104-
file (`@com_google_googletest`). For more information about Bazel `BUILD` files,
105-
see the
96+
the GoogleTest library (`@googletest//:gtest"`) and the GoogleTest `main()`
97+
function (`@googletest//:gtest_main`). For more information about Bazel `BUILD`
98+
files, see the
10699
[Bazel C++ Tutorial](https://docs.bazel.build/versions/main/tutorial/cpp.html).
107100

108101
{: .callout .note}
@@ -115,7 +108,7 @@ on supported language versions.
115108
Now you can build and run your test:
116109

117110
<pre>
118-
<strong>my_workspace$ bazel test --cxxopt=-std=c++14 --test_output=all //:hello_test</strong>
111+
<strong>$ bazel test --cxxopt=-std=c++14 --test_output=all //:hello_test</strong>
119112
INFO: Analyzed target //:hello_test (26 packages loaded, 362 targets configured).
120113
INFO: Found 1 test target...
121114
INFO: From Testing //:hello_test:

0 commit comments

Comments
 (0)