• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. _module-pw_third_party_googletest:
2
3==========
4GoogleTest
5==========
6The ``$dir_pw_third_party/googletest/`` module provides various helpers to
7optionally use full upstream `GoogleTest/GoogleMock`__ with
8:ref:`module-pw_unit_test`.
9
10.. __: https://github.com/google/googletest
11
12.. _module-pw_third_party_googletest-using_upstream:
13
14----------------------------------------
15Using upstream GoogleTest and GoogleMock
16----------------------------------------
17If you want to use the full upstream GoogleTest/GoogleMock, you must do the
18following:
19
20Add GoogleTest to your workspace with the following command.
21
22.. code-block:: console
23
24   $ git submodule add https://github.com/google/googletest third_party/googletest
25
26Configure ``pw_unit_test`` to use upstream GoogleTest/GoogleMock.
27
28.. tab-set::
29
30   .. tab-item:: GN
31
32      * Set one of the following GN variables:
33
34        * Set ``pw_third_party_googletest_ALIAS`` to point to
35          a GN target label that provides a static library or
36          source set combining GoogleTest/GoogleMock.
37
38          This is required on platforms such as Fuchsia where
39          extra dependencies are required for the library to compile
40          and run properly.
41
42        * Or set ``dir_pw_third_party_googletest`` to point
43          to the location of the googletest submodule instead.
44          This will trigger the generation of a pw_source_set()
45          target wrapping the GoogleTest sources.
46
47      * Set the GN var ``pw_unit_test_MAIN`` to
48        ``dir_pigweed + "/third_party/googletest:gmock_main"``.
49      * Set the GN var ``pw_unit_test_BACKEND`` to
50        ``"//pw_unit_test:googletest"``.
51
52      Pigweed unit tests that do not work with upstream GoogleTest can be
53      disabled by setting ``enable_if`` to
54      ``pw_unit_test_BACKEND == "$dir_pw_unit_test:light"``.
55
56   .. tab-item:: CMake
57
58      * Set the ``dir_pw_third_party_googletest`` to the location of the
59        GoogleTest source.
60      * Set the var
61        ``pw_unit_test_MAIN`` to ``pw_third_party.googletest.gmock_main``.
62      * Set the var ``pw_unit_test_BACKEND`` to
63        ``pw_unit_test.googletest``.
64
65   .. tab-item:: Bazel
66
67      Set the following :ref:`label flags <docs-build_system-bazel_flags>`,
68      either in your
69      :ref:`target config <docs-build_system-bazel_configuration>` or on
70      the command line:
71
72      * ``//pw_unit_test:backend`` to
73        ``@pigweed//pw_unit_test:googletest``.
74      * ``//pw_unit_test:main`` to ``@com_google_googletest//:gtest_main``.
75
76      For example:
77
78      .. code-block:: console
79
80         $ bazel test //... \
81         > --@pigweed//pw_unit_test:backend=@pigweed//pw_unit_test:googletest \
82         > --@pigweed//pw_unit_test:main=@com_google_googletest//:gtest_main
83
84.. note::
85
86  Not all unit tests build properly with upstream GoogleTest yet. This is a
87  work in progress.
88