quickstart-bazel.md (googletest-release-1.11.0) | : | quickstart-bazel.md (googletest-release-1.12.0) | ||
---|---|---|---|---|
skipping to change at line 20 | skipping to change at line 20 | |||
* 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++11. | * A compatible C++ compiler that supports at least C++11. | |||
* [Bazel](https://bazel.build/), the preferred build system used by the | * [Bazel](https://bazel.build/), the preferred build system used by the | |||
GoogleTest team. | 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. | |||
If you don't already have Bazel installed, see the | If you don't already have Bazel installed, see the | |||
[Bazel installation guide](https://docs.bazel.build/versions/master/install.html ). | [Bazel installation guide](https://docs.bazel.build/versions/main/install.html). | |||
{: .callout .note} | {: .callout .note} | |||
Note: The terminal commands in this tutorial show a Unix shell prompt, but the | Note: The terminal commands in this tutorial show a Unix shell prompt, but the | |||
commands work on the Windows command line as well. | commands work on the Windows command line as well. | |||
## Set up a Bazel workspace | ## Set up a Bazel workspace | |||
A | A | |||
[Bazel workspace](https://docs.bazel.build/versions/master/build-ref.html#worksp ace) | [Bazel workspace](https://docs.bazel.build/versions/main/build-ref.html#workspac e) | |||
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 | `WORKSPACE` 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: | |||
``` | ``` | |||
$ 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 `WORKSPACE` file to specify dependencies. A common and | |||
recommended way to depend on GoogleTest is to use a | recommended way to depend on GoogleTest is to use a | |||
[Bazel external dependency](https://docs.bazel.build/versions/master/external.ht ml) | [Bazel external dependency](https://docs.bazel.build/versions/main/external.html ) | |||
via the | via the | |||
[`http_archive` rule](https://docs.bazel.build/versions/master/repo/http.html#ht tp_archive). | [`http_archive` rule](https://docs.bazel.build/versions/main/repo/http.html#http _archive). | |||
To do this, in the root directory of your workspace (`my_workspace/`), create a | To do this, in the root directory of your workspace (`my_workspace/`), create a | |||
file named `WORKSPACE` with the following contents: | file named `WORKSPACE` with the following contents: | |||
``` | ``` | |||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") | load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") | |||
http_archive( | http_archive( | |||
name = "com_google_googletest", | name = "com_google_googletest", | |||
urls = ["https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82 e1ff6c30cc3591e5.zip"], | urls = ["https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82 e1ff6c30cc3591e5.zip"], | |||
strip_prefix = "googletest-609281088cfefc76f9d0ce82e1ff6c30cc3591e5", | strip_prefix = "googletest-609281088cfefc76f9d0ce82e1ff6c30cc3591e5", | |||
) | ) | |||
``` | ``` | |||
The above configuration declares a dependency on GoogleTest which is downloaded | The above configuration declares a dependency on GoogleTest which is downloaded | |||
as a ZIP archive from GitHub. In the above example, | as a ZIP archive from GitHub. In the above example, | |||
`609281088cfefc76f9d0ce82e1ff6c30cc3591e5` is the Git commit hash of the | `609281088cfefc76f9d0ce82e1ff6c30cc3591e5` is the Git commit hash of the | |||
GoogleTest version to use; we recommend updating the hash often to point to the | GoogleTest version to use; we recommend updating the hash often to point to the | |||
latest version. | latest version. | |||
Bazel also needs a dependency on the | ||||
[`rules_cc` repository](https://github.com/bazelbuild/rules_cc) to build C++ | ||||
code, so add the following to the `WORKSPACE` file: | ||||
``` | ||||
http_archive( | ||||
name = "rules_cc", | ||||
urls = ["https://github.com/bazelbuild/rules_cc/archive/40548a2974f1aea0621527 | ||||
2d9c2b47a14a24e556.zip"], | ||||
strip_prefix = "rules_cc-40548a2974f1aea06215272d9c2b47a14a24e556", | ||||
) | ||||
``` | ||||
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 | |||
With your Bazel workspace set up, you can now use GoogleTest code within your | With your Bazel workspace set up, you can now use GoogleTest code within your | |||
own project. | own project. | |||
As an example, create a file named `hello_test.cc` in your `my_workspace` | As an example, create a file named `hello_test.cc` in your `my_workspace` | |||
directory with the following contents: | directory with the following contents: | |||
skipping to change at line 107 | skipping to change at line 95 | |||
``` | ``` | |||
GoogleTest provides [assertions](primer.md#assertions) that you use to test the | GoogleTest provides [assertions](primer.md#assertions) that you use to test the | |||
behavior of your code. The above sample includes the main GoogleTest header file | behavior of your code. The above sample includes the main GoogleTest header file | |||
and demonstrates some basic assertions. | and demonstrates some basic assertions. | |||
To build the code, create a file named `BUILD` in the same directory with the | To build the code, create a file named `BUILD` in the same directory with the | |||
following contents: | following contents: | |||
``` | ``` | |||
load("@rules_cc//cc:defs.bzl", "cc_test") | ||||
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 = ["@com_google_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` | GoogleTest (`//:gtest_main`) using the prefix you specified in the `WORKSPACE` | |||
file (`@com_google_googletest`). For more information about Bazel `BUILD` files, | file (`@com_google_googletest`). For more information about Bazel `BUILD` files, | |||
see the | see the | |||
[Bazel C++ Tutorial](https://docs.bazel.build/versions/master/tutorial/cpp.html) . | [Bazel C++ Tutorial](https://docs.bazel.build/versions/main/tutorial/cpp.html). | |||
Now you can build and run your test: | Now you can build and run your test: | |||
<pre> | <pre> | |||
<strong>my_workspace$ bazel test --test_output=all //:hello_test</strong> | <strong>my_workspace$ bazel test --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: | |||
==================== Test output for //:hello_test: | ==================== Test output for //:hello_test: | |||
Running main() from gmock_main.cc | Running main() from gmock_main.cc | |||
End of changes. 7 change blocks. | ||||
20 lines changed or deleted | 5 lines changed or added |