You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
5.5 KiB

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