mirror of
https://github.com/google/googletest.git
synced 2024-12-27 02:01:25 +08:00
Update GoogleTest Bazel quickstart for Bzlmod
PiperOrigin-RevId: 652824490 Change-Id: I5e6f57004708e7fa62abb454db9bae81fa265c83
This commit is contained in:
parent
b62593aceb
commit
9ff2450a56
@ -10,8 +10,8 @@ To complete this tutorial, you'll need:
|
|||||||
|
|
||||||
* A compatible operating system (e.g. Linux, macOS, Windows).
|
* A compatible operating system (e.g. Linux, macOS, Windows).
|
||||||
* A compatible C++ compiler that supports at least C++14.
|
* A compatible C++ compiler that supports at least C++14.
|
||||||
* [Bazel](https://bazel.build/), the preferred build system used by the
|
* [Bazel](https://bazel.build/) 7.0 or higher, the preferred build system used
|
||||||
GoogleTest team.
|
by the GoogleTest team.
|
||||||
|
|
||||||
See [Supported Platforms](platforms.md) for more information about platforms
|
See [Supported Platforms](platforms.md) for more information about platforms
|
||||||
compatible with GoogleTest.
|
compatible with GoogleTest.
|
||||||
@ -28,7 +28,7 @@ A
|
|||||||
[Bazel workspace](https://docs.bazel.build/versions/main/build-ref.html#workspace)
|
[Bazel workspace](https://docs.bazel.build/versions/main/build-ref.html#workspace)
|
||||||
is a directory on your filesystem that you use to manage source files for the
|
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
|
software you want to build. Each workspace directory has a text file named
|
||||||
`WORKSPACE` which may be empty, or may contain references to external
|
`MODULE.bazel` which may be empty, or may contain references to external
|
||||||
dependencies required to build the outputs.
|
dependencies required to build the outputs.
|
||||||
|
|
||||||
First, create a directory for your workspace:
|
First, create a directory for your workspace:
|
||||||
@ -37,30 +37,20 @@ First, create a directory for your workspace:
|
|||||||
$ mkdir my_workspace && cd my_workspace
|
$ mkdir my_workspace && cd my_workspace
|
||||||
```
|
```
|
||||||
|
|
||||||
Next, you’ll create the `WORKSPACE` file to specify dependencies. A common and
|
Next, you’ll create the `MODULE.bazel` file to specify dependencies. As of Bazel
|
||||||
recommended way to depend on GoogleTest is to use a
|
7.0, the recommended way to consume GoogleTest is through the
|
||||||
[Bazel external dependency](https://docs.bazel.build/versions/main/external.html)
|
[Bazel Central Registry](https://registry.bazel.build/modules/googletest). To do
|
||||||
via the
|
this, create a `MODULE.bazel` file in the root directory of your Bazel workspace
|
||||||
[`http_archive` rule](https://docs.bazel.build/versions/main/repo/http.html#http_archive).
|
with the following content:
|
||||||
To do this, in the root directory of your workspace (`my_workspace/`), create a
|
|
||||||
file named `WORKSPACE` with the following contents:
|
|
||||||
|
|
||||||
```
|
```
|
||||||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
|
# MODULE.bazel
|
||||||
|
|
||||||
http_archive(
|
# Choose the most recent version available at
|
||||||
name = "com_google_googletest",
|
# https://registry.bazel.build/modules/googletest
|
||||||
urls = ["https://github.com/google/googletest/archive/5ab508a01f9eb089207ee87fd547d290da39d015.zip"],
|
bazel_dep(name = "googletest", version = "1.15.0")
|
||||||
strip_prefix = "googletest-5ab508a01f9eb089207ee87fd547d290da39d015",
|
|
||||||
)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
The above configuration declares a dependency on GoogleTest which is downloaded
|
|
||||||
as a ZIP archive from GitHub. In the above example,
|
|
||||||
`5ab508a01f9eb089207ee87fd547d290da39d015` is the Git commit hash of the
|
|
||||||
GoogleTest version to use; we recommend updating the hash often to point to the
|
|
||||||
latest version. Use a recent hash on the `main` branch.
|
|
||||||
|
|
||||||
Now you're ready to build C++ code that uses GoogleTest.
|
Now you're ready to build C++ code that uses GoogleTest.
|
||||||
|
|
||||||
## Create and run a binary
|
## Create and run a binary
|
||||||
@ -92,17 +82,20 @@ following contents:
|
|||||||
|
|
||||||
```
|
```
|
||||||
cc_test(
|
cc_test(
|
||||||
name = "hello_test",
|
name = "hello_test",
|
||||||
size = "small",
|
size = "small",
|
||||||
srcs = ["hello_test.cc"],
|
srcs = ["hello_test.cc"],
|
||||||
deps = ["@com_google_googletest//:gtest_main"],
|
deps = [
|
||||||
|
"@googletest//:gtest",
|
||||||
|
"@googletest//:gtest_main",
|
||||||
|
],
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
This `cc_test` rule declares the C++ test binary you want to build, and links to
|
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`
|
the GoogleTest library (`@googletest//:gtest"`) and the GoogleTest `main()`
|
||||||
file (`@com_google_googletest`). For more information about Bazel `BUILD` files,
|
function (`@googletest//:gtest_main`). For more information about Bazel `BUILD`
|
||||||
see the
|
files, see the
|
||||||
[Bazel C++ Tutorial](https://docs.bazel.build/versions/main/tutorial/cpp.html).
|
[Bazel C++ Tutorial](https://docs.bazel.build/versions/main/tutorial/cpp.html).
|
||||||
|
|
||||||
{: .callout .note}
|
{: .callout .note}
|
||||||
@ -115,7 +108,7 @@ on supported language versions.
|
|||||||
Now you can build and run your test:
|
Now you can build and run your test:
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
<strong>my_workspace$ bazel test --cxxopt=-std=c++14 --test_output=all //:hello_test</strong>
|
<strong>$ bazel test --cxxopt=-std=c++14 --test_output=all //:hello_test</strong>
|
||||||
INFO: Analyzed target //:hello_test (26 packages loaded, 362 targets configured).
|
INFO: Analyzed target //:hello_test (26 packages loaded, 362 targets configured).
|
||||||
INFO: Found 1 test target...
|
INFO: Found 1 test target...
|
||||||
INFO: From Testing //:hello_test:
|
INFO: From Testing //:hello_test:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user