xref: /aosp_15_r20/external/pigweed/pw_unit_test/docs.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker============
4*61c4878aSAndroid Build Coastguard Workerpw_unit_test
5*61c4878aSAndroid Build Coastguard Worker============
6*61c4878aSAndroid Build Coastguard Worker.. pigweed-module::
7*61c4878aSAndroid Build Coastguard Worker   :name: pw_unit_test
8*61c4878aSAndroid Build Coastguard Worker
9*61c4878aSAndroid Build Coastguard Worker.. tab-set::
10*61c4878aSAndroid Build Coastguard Worker
11*61c4878aSAndroid Build Coastguard Worker   .. tab-item:: mylib_test.cpp
12*61c4878aSAndroid Build Coastguard Worker
13*61c4878aSAndroid Build Coastguard Worker      .. code-block:: c++
14*61c4878aSAndroid Build Coastguard Worker
15*61c4878aSAndroid Build Coastguard Worker         #include "mylib.h"
16*61c4878aSAndroid Build Coastguard Worker
17*61c4878aSAndroid Build Coastguard Worker         #include "pw_unit_test/framework.h"
18*61c4878aSAndroid Build Coastguard Worker
19*61c4878aSAndroid Build Coastguard Worker         namespace {
20*61c4878aSAndroid Build Coastguard Worker
21*61c4878aSAndroid Build Coastguard Worker         TEST(MyTestSuite, MyTestCase) {
22*61c4878aSAndroid Build Coastguard Worker           pw::InlineString<32> expected = "(╯°□°)╯︵ ┻━┻";
23*61c4878aSAndroid Build Coastguard Worker           pw::InlineString<32> actual = mylib::flip_table();
24*61c4878aSAndroid Build Coastguard Worker           EXPECT_STREQ(expected.c_str(), actual.c_str());
25*61c4878aSAndroid Build Coastguard Worker         }
26*61c4878aSAndroid Build Coastguard Worker
27*61c4878aSAndroid Build Coastguard Worker         }
28*61c4878aSAndroid Build Coastguard Worker
29*61c4878aSAndroid Build Coastguard Worker   .. tab-item:: BUILD.bazel
30*61c4878aSAndroid Build Coastguard Worker
31*61c4878aSAndroid Build Coastguard Worker      .. code-block:: python
32*61c4878aSAndroid Build Coastguard Worker
33*61c4878aSAndroid Build Coastguard Worker         load("@pigweed//pw_unit_test:pw_cc_test.bzl", "pw_cc_test")
34*61c4878aSAndroid Build Coastguard Worker
35*61c4878aSAndroid Build Coastguard Worker         cc_library(
36*61c4878aSAndroid Build Coastguard Worker             name = "mylib",
37*61c4878aSAndroid Build Coastguard Worker             srcs = ["mylib.cc"],
38*61c4878aSAndroid Build Coastguard Worker             hdrs = ["mylib.h"],
39*61c4878aSAndroid Build Coastguard Worker             includes = ["."],
40*61c4878aSAndroid Build Coastguard Worker             deps = ["@pigweed//pw_string"],
41*61c4878aSAndroid Build Coastguard Worker         )
42*61c4878aSAndroid Build Coastguard Worker
43*61c4878aSAndroid Build Coastguard Worker         pw_cc_test(
44*61c4878aSAndroid Build Coastguard Worker             name = "mylib_test",
45*61c4878aSAndroid Build Coastguard Worker             srcs = ["mylib_test.cc"],
46*61c4878aSAndroid Build Coastguard Worker             deps = [
47*61c4878aSAndroid Build Coastguard Worker                 "@pigweed//pw_unit_test",
48*61c4878aSAndroid Build Coastguard Worker                 ":mylib",
49*61c4878aSAndroid Build Coastguard Worker             ],
50*61c4878aSAndroid Build Coastguard Worker         )
51*61c4878aSAndroid Build Coastguard Worker
52*61c4878aSAndroid Build Coastguard Worker   .. tab-item:: mylib.cc
53*61c4878aSAndroid Build Coastguard Worker
54*61c4878aSAndroid Build Coastguard Worker      .. code-block:: c++
55*61c4878aSAndroid Build Coastguard Worker
56*61c4878aSAndroid Build Coastguard Worker         #include "mylib.h"
57*61c4878aSAndroid Build Coastguard Worker
58*61c4878aSAndroid Build Coastguard Worker         #include "pw_string/string.h"
59*61c4878aSAndroid Build Coastguard Worker
60*61c4878aSAndroid Build Coastguard Worker         namespace mylib {
61*61c4878aSAndroid Build Coastguard Worker
62*61c4878aSAndroid Build Coastguard Worker         pw::InlineString<32> flip_table() {
63*61c4878aSAndroid Build Coastguard Worker           pw::InlineString<32> textmoji = "(╯°□°)╯︵ ┻━┻";
64*61c4878aSAndroid Build Coastguard Worker           return textmoji;
65*61c4878aSAndroid Build Coastguard Worker         }
66*61c4878aSAndroid Build Coastguard Worker
67*61c4878aSAndroid Build Coastguard Worker         }
68*61c4878aSAndroid Build Coastguard Worker
69*61c4878aSAndroid Build Coastguard Worker   .. tab-item:: mylib.h
70*61c4878aSAndroid Build Coastguard Worker
71*61c4878aSAndroid Build Coastguard Worker      .. code-block:: c++
72*61c4878aSAndroid Build Coastguard Worker
73*61c4878aSAndroid Build Coastguard Worker         #include "pw_string/string.h"
74*61c4878aSAndroid Build Coastguard Worker
75*61c4878aSAndroid Build Coastguard Worker         namespace mylib {
76*61c4878aSAndroid Build Coastguard Worker
77*61c4878aSAndroid Build Coastguard Worker         pw::InlineString<32> flip_table();
78*61c4878aSAndroid Build Coastguard Worker
79*61c4878aSAndroid Build Coastguard Worker         }
80*61c4878aSAndroid Build Coastguard Worker
81*61c4878aSAndroid Build Coastguard Worker.. _GoogleTest: https://google.github.io/googletest/
82*61c4878aSAndroid Build Coastguard Worker
83*61c4878aSAndroid Build Coastguard Worker``pw_unit_test`` provides a `GoogleTest`_-compatible unit testing framework for
84*61c4878aSAndroid Build Coastguard WorkerPigweed-based projects. The default backend is a lightweight subset of
85*61c4878aSAndroid Build Coastguard WorkerGoogleTest that uses embedded-friendly primitives.
86*61c4878aSAndroid Build Coastguard Worker
87*61c4878aSAndroid Build Coastguard Worker.. grid:: 1
88*61c4878aSAndroid Build Coastguard Worker
89*61c4878aSAndroid Build Coastguard Worker   .. grid-item-card:: :octicon:`rocket` Quickstart
90*61c4878aSAndroid Build Coastguard Worker      :link: module-pw_unit_test-quickstart
91*61c4878aSAndroid Build Coastguard Worker      :link-type: ref
92*61c4878aSAndroid Build Coastguard Worker      :class-item: sales-pitch-cta-primary
93*61c4878aSAndroid Build Coastguard Worker
94*61c4878aSAndroid Build Coastguard Worker      Set up your project for testing and learn testing basics.
95*61c4878aSAndroid Build Coastguard Worker
96*61c4878aSAndroid Build Coastguard Worker.. grid:: 2
97*61c4878aSAndroid Build Coastguard Worker
98*61c4878aSAndroid Build Coastguard Worker   .. grid-item-card:: :octicon:`list-unordered` Guides
99*61c4878aSAndroid Build Coastguard Worker      :link: module-pw_unit_test-guides
100*61c4878aSAndroid Build Coastguard Worker      :link-type: ref
101*61c4878aSAndroid Build Coastguard Worker      :class-item: sales-pitch-cta-secondary
102*61c4878aSAndroid Build Coastguard Worker
103*61c4878aSAndroid Build Coastguard Worker      Learn how to do common tasks.
104*61c4878aSAndroid Build Coastguard Worker
105*61c4878aSAndroid Build Coastguard Worker   .. grid-item-card:: :octicon:`code-square` C++ API reference
106*61c4878aSAndroid Build Coastguard Worker      :link: module-pw_unit_test-cpp
107*61c4878aSAndroid Build Coastguard Worker      :link-type: ref
108*61c4878aSAndroid Build Coastguard Worker      :class-item: sales-pitch-cta-secondary
109*61c4878aSAndroid Build Coastguard Worker
110*61c4878aSAndroid Build Coastguard Worker      Get detailed C++ API reference information.
111*61c4878aSAndroid Build Coastguard Worker
112*61c4878aSAndroid Build Coastguard Worker.. grid:: 2
113*61c4878aSAndroid Build Coastguard Worker
114*61c4878aSAndroid Build Coastguard Worker   .. grid-item-card:: :octicon:`code-square` Bazel API reference
115*61c4878aSAndroid Build Coastguard Worker      :link: module-pw_unit_test-bazel
116*61c4878aSAndroid Build Coastguard Worker      :link-type: ref
117*61c4878aSAndroid Build Coastguard Worker      :class-item: sales-pitch-cta-secondary
118*61c4878aSAndroid Build Coastguard Worker
119*61c4878aSAndroid Build Coastguard Worker      Get detailed Bazel API reference information.
120*61c4878aSAndroid Build Coastguard Worker
121*61c4878aSAndroid Build Coastguard Worker   .. grid-item-card:: :octicon:`code-square` GN API reference
122*61c4878aSAndroid Build Coastguard Worker      :link: module-pw_unit_test-gn
123*61c4878aSAndroid Build Coastguard Worker      :link-type: ref
124*61c4878aSAndroid Build Coastguard Worker      :class-item: sales-pitch-cta-secondary
125*61c4878aSAndroid Build Coastguard Worker
126*61c4878aSAndroid Build Coastguard Worker      Get detailed GN API reference information.
127*61c4878aSAndroid Build Coastguard Worker
128*61c4878aSAndroid Build Coastguard Worker.. grid:: 2
129*61c4878aSAndroid Build Coastguard Worker
130*61c4878aSAndroid Build Coastguard Worker   .. grid-item-card:: :octicon:`code-square` CMake API reference
131*61c4878aSAndroid Build Coastguard Worker      :link: module-pw_unit_test-cmake
132*61c4878aSAndroid Build Coastguard Worker      :link-type: ref
133*61c4878aSAndroid Build Coastguard Worker      :class-item: sales-pitch-cta-secondary
134*61c4878aSAndroid Build Coastguard Worker
135*61c4878aSAndroid Build Coastguard Worker      Get detailed CMake API reference information.
136*61c4878aSAndroid Build Coastguard Worker
137*61c4878aSAndroid Build Coastguard Worker   .. grid-item-card:: :octicon:`code-square` Python API reference
138*61c4878aSAndroid Build Coastguard Worker      :link: module-pw_unit_test-py
139*61c4878aSAndroid Build Coastguard Worker      :link-type: ref
140*61c4878aSAndroid Build Coastguard Worker      :class-item: sales-pitch-cta-secondary
141*61c4878aSAndroid Build Coastguard Worker
142*61c4878aSAndroid Build Coastguard Worker      Get detailed Python API reference information.
143*61c4878aSAndroid Build Coastguard Worker
144*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-quickstart:
145*61c4878aSAndroid Build Coastguard Worker
146*61c4878aSAndroid Build Coastguard Worker----------
147*61c4878aSAndroid Build Coastguard WorkerQuickstart
148*61c4878aSAndroid Build Coastguard Worker----------
149*61c4878aSAndroid Build Coastguard Worker
150*61c4878aSAndroid Build Coastguard WorkerSet up your build system
151*61c4878aSAndroid Build Coastguard Worker========================
152*61c4878aSAndroid Build Coastguard Worker.. tab-set::
153*61c4878aSAndroid Build Coastguard Worker
154*61c4878aSAndroid Build Coastguard Worker   .. tab-item:: Bazel
155*61c4878aSAndroid Build Coastguard Worker
156*61c4878aSAndroid Build Coastguard Worker      Load the :ref:`module-pw_unit_test-pw_cc_test` rule and create a target
157*61c4878aSAndroid Build Coastguard Worker      that depends on ``@pigweed//pw_unit_test`` as well as the code you want
158*61c4878aSAndroid Build Coastguard Worker      to test:
159*61c4878aSAndroid Build Coastguard Worker
160*61c4878aSAndroid Build Coastguard Worker      .. code-block:: python
161*61c4878aSAndroid Build Coastguard Worker
162*61c4878aSAndroid Build Coastguard Worker         load("@pigweed//pw_unit_test:pw_cc_test.bzl", "pw_cc_test")
163*61c4878aSAndroid Build Coastguard Worker
164*61c4878aSAndroid Build Coastguard Worker         cc_library(
165*61c4878aSAndroid Build Coastguard Worker             name = "mylib",
166*61c4878aSAndroid Build Coastguard Worker             srcs = ["mylib.cc"],
167*61c4878aSAndroid Build Coastguard Worker             hdrs = ["mylib.h"],
168*61c4878aSAndroid Build Coastguard Worker             includes = ["."],
169*61c4878aSAndroid Build Coastguard Worker             deps = ["..."],
170*61c4878aSAndroid Build Coastguard Worker         )
171*61c4878aSAndroid Build Coastguard Worker
172*61c4878aSAndroid Build Coastguard Worker         pw_cc_test(
173*61c4878aSAndroid Build Coastguard Worker             name = "mylib_test",
174*61c4878aSAndroid Build Coastguard Worker             srcs = ["mylib_test.cc"],
175*61c4878aSAndroid Build Coastguard Worker             deps = [
176*61c4878aSAndroid Build Coastguard Worker                 "@pigweed//pw_unit_test",
177*61c4878aSAndroid Build Coastguard Worker                 ":mylib",
178*61c4878aSAndroid Build Coastguard Worker             ],
179*61c4878aSAndroid Build Coastguard Worker         )
180*61c4878aSAndroid Build Coastguard Worker
181*61c4878aSAndroid Build Coastguard Worker      This assumes that your Bazel ``WORKSPACE`` has a `repository
182*61c4878aSAndroid Build Coastguard Worker      <https://bazel.build/concepts/build-ref#repositories>`_ named ``@pigweed``
183*61c4878aSAndroid Build Coastguard Worker      that points to the upstream Pigweed repository.
184*61c4878aSAndroid Build Coastguard Worker
185*61c4878aSAndroid Build Coastguard Worker      See also :ref:`module-pw_unit_test-bazel`.
186*61c4878aSAndroid Build Coastguard Worker
187*61c4878aSAndroid Build Coastguard Worker   .. tab-item:: GN
188*61c4878aSAndroid Build Coastguard Worker
189*61c4878aSAndroid Build Coastguard Worker      Import ``$dir_pw_unit_test/test.gni`` and create a ``pw_test`` rule that
190*61c4878aSAndroid Build Coastguard Worker      depends on the code you want to test:
191*61c4878aSAndroid Build Coastguard Worker
192*61c4878aSAndroid Build Coastguard Worker      .. code-block:: python
193*61c4878aSAndroid Build Coastguard Worker
194*61c4878aSAndroid Build Coastguard Worker         import("$dir_pw_unit_test/test.gni")
195*61c4878aSAndroid Build Coastguard Worker
196*61c4878aSAndroid Build Coastguard Worker         pw_source_set("mylib") {
197*61c4878aSAndroid Build Coastguard Worker           sources = [ "mylib.cc" ]
198*61c4878aSAndroid Build Coastguard Worker         }
199*61c4878aSAndroid Build Coastguard Worker
200*61c4878aSAndroid Build Coastguard Worker         pw_test("mylib_test") {
201*61c4878aSAndroid Build Coastguard Worker           sources = [ "mylib_test.cc" ]
202*61c4878aSAndroid Build Coastguard Worker           deps = [ ":mylib" ]
203*61c4878aSAndroid Build Coastguard Worker         }
204*61c4878aSAndroid Build Coastguard Worker
205*61c4878aSAndroid Build Coastguard Worker      See :ref:`module-pw_unit_test-gn` for more information.
206*61c4878aSAndroid Build Coastguard Worker
207*61c4878aSAndroid Build Coastguard Worker``pw_unit_test`` generates a simple ``main`` function for running tests on
208*61c4878aSAndroid Build Coastguard Worker:ref:`target-host`. See :ref:`module-pw_unit_test-main` to learn how to
209*61c4878aSAndroid Build Coastguard Workercreate your own ``main`` function for running on-device tests.
210*61c4878aSAndroid Build Coastguard Worker
211*61c4878aSAndroid Build Coastguard WorkerWrite tests
212*61c4878aSAndroid Build Coastguard Worker===========
213*61c4878aSAndroid Build Coastguard WorkerCreate test suites and test cases:
214*61c4878aSAndroid Build Coastguard Worker
215*61c4878aSAndroid Build Coastguard Worker.. code-block:: c++
216*61c4878aSAndroid Build Coastguard Worker
217*61c4878aSAndroid Build Coastguard Worker   #include "mylib.h"
218*61c4878aSAndroid Build Coastguard Worker
219*61c4878aSAndroid Build Coastguard Worker   #include "pw_unit_test/framework.h"
220*61c4878aSAndroid Build Coastguard Worker
221*61c4878aSAndroid Build Coastguard Worker   namespace {
222*61c4878aSAndroid Build Coastguard Worker
223*61c4878aSAndroid Build Coastguard Worker   TEST(MyTestSuite, MyTestCase) {
224*61c4878aSAndroid Build Coastguard Worker     pw::InlineString<32> expected = "(╯°□°)╯︵ ┻━┻";
225*61c4878aSAndroid Build Coastguard Worker     pw::InlineString<32> actual = app::flip_table();
226*61c4878aSAndroid Build Coastguard Worker     EXPECT_STREQ(expected.c_str(), actual.c_str());
227*61c4878aSAndroid Build Coastguard Worker   }
228*61c4878aSAndroid Build Coastguard Worker
229*61c4878aSAndroid Build Coastguard Worker   }
230*61c4878aSAndroid Build Coastguard Worker
231*61c4878aSAndroid Build Coastguard Worker``pw_unit_test`` provides a standard set of ``TEST``, ``EXPECT``, ``ASSERT``
232*61c4878aSAndroid Build Coastguard Workerand ``FAIL`` macros. The default backend, ``pw_unit_test:light``, offers an
233*61c4878aSAndroid Build Coastguard Workerembedded-friendly implementation which prioritizes small code size.
234*61c4878aSAndroid Build Coastguard Worker
235*61c4878aSAndroid Build Coastguard WorkerAlternativley, users can opt into a larger set of assertion macros, matchers,
236*61c4878aSAndroid Build Coastguard Workerand more detailed error messages by using the ``pw_unit_test:googletest``
237*61c4878aSAndroid Build Coastguard Workerbackend. See :ref:`module-pw_unit_test-backends`.
238*61c4878aSAndroid Build Coastguard Worker
239*61c4878aSAndroid Build Coastguard WorkerSee `GoogleTest Primer <https://google.github.io/googletest/primer.html>`_ for
240*61c4878aSAndroid Build Coastguard Workerthe basics of using GoogleTest.
241*61c4878aSAndroid Build Coastguard Worker
242*61c4878aSAndroid Build Coastguard WorkerRun tests
243*61c4878aSAndroid Build Coastguard Worker=========
244*61c4878aSAndroid Build Coastguard Worker.. tab-set::
245*61c4878aSAndroid Build Coastguard Worker
246*61c4878aSAndroid Build Coastguard Worker   .. tab-item:: Bazel
247*61c4878aSAndroid Build Coastguard Worker
248*61c4878aSAndroid Build Coastguard Worker      .. code-block:: console
249*61c4878aSAndroid Build Coastguard Worker
250*61c4878aSAndroid Build Coastguard Worker         $ bazel test //src:mylib_test
251*61c4878aSAndroid Build Coastguard Worker
252*61c4878aSAndroid Build Coastguard Worker   .. tab-item:: GN
253*61c4878aSAndroid Build Coastguard Worker
254*61c4878aSAndroid Build Coastguard Worker      Run the generated test binary:
255*61c4878aSAndroid Build Coastguard Worker
256*61c4878aSAndroid Build Coastguard Worker      .. code-block:: console
257*61c4878aSAndroid Build Coastguard Worker
258*61c4878aSAndroid Build Coastguard Worker         $ ./out/.../mylib_test
259*61c4878aSAndroid Build Coastguard Worker
260*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-guides:
261*61c4878aSAndroid Build Coastguard Worker
262*61c4878aSAndroid Build Coastguard Worker------
263*61c4878aSAndroid Build Coastguard WorkerGuides
264*61c4878aSAndroid Build Coastguard Worker------
265*61c4878aSAndroid Build Coastguard Worker
266*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-backends:
267*61c4878aSAndroid Build Coastguard Worker
268*61c4878aSAndroid Build Coastguard WorkerChoose a backend
269*61c4878aSAndroid Build Coastguard Worker================
270*61c4878aSAndroid Build Coastguard WorkerThe default backend, ``pw_unit_test:light``, is a lightweight subset of
271*61c4878aSAndroid Build Coastguard WorkerGoogleTest that uses embedded-friendly primitives. It's also highly portable
272*61c4878aSAndroid Build Coastguard Workerbecause it offloads the responsibility of test reporting and output to the
273*61c4878aSAndroid Build Coastguard Workerunderlying system, communicating its results through a common interface. This
274*61c4878aSAndroid Build Coastguard Workerlets you write unit tests once and run them under many different environments.
275*61c4878aSAndroid Build Coastguard Worker
276*61c4878aSAndroid Build Coastguard WorkerIf the :ref:`subset <module-pw_unit_test-compatibility>` of GoogleTest that
277*61c4878aSAndroid Build Coastguard Worker``pw_unit_test:light`` supports doesn't meet your needs, you can access the
278*61c4878aSAndroid Build Coastguard Workerfull upstream GoogleTest API through ``pw_unit_test:googletest``. See
279*61c4878aSAndroid Build Coastguard Worker:ref:`module-pw_unit_test-upstream`.
280*61c4878aSAndroid Build Coastguard Worker
281*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-main:
282*61c4878aSAndroid Build Coastguard Worker
283*61c4878aSAndroid Build Coastguard WorkerCreate a custom ``main`` function
284*61c4878aSAndroid Build Coastguard Worker=================================
285*61c4878aSAndroid Build Coastguard WorkerFor simple unit tests that run on :ref:`target-host` the workflow outlined in
286*61c4878aSAndroid Build Coastguard Worker:ref:`module-pw_unit_test-quickstart` is all you need. Pigweed's build templates
287*61c4878aSAndroid Build Coastguard Workergenerate a simple ``main`` function to run the tests with.
288*61c4878aSAndroid Build Coastguard Worker
289*61c4878aSAndroid Build Coastguard WorkerTo do more complex testing, such as on-device testing:
290*61c4878aSAndroid Build Coastguard Worker
291*61c4878aSAndroid Build Coastguard Worker1. Create your own ``main`` function:
292*61c4878aSAndroid Build Coastguard Worker
293*61c4878aSAndroid Build Coastguard Worker   .. code-block:: c++
294*61c4878aSAndroid Build Coastguard Worker
295*61c4878aSAndroid Build Coastguard Worker      #include "pw_unit_test/framework.h"
296*61c4878aSAndroid Build Coastguard Worker      // pw_unit_test:light requires an event handler to be configured.
297*61c4878aSAndroid Build Coastguard Worker      #include "pw_unit_test/simple_printing_event_handler.h"
298*61c4878aSAndroid Build Coastguard Worker
299*61c4878aSAndroid Build Coastguard Worker      void WriteString(std::string_view string, bool newline) {
300*61c4878aSAndroid Build Coastguard Worker        printf("%s", string.data());
301*61c4878aSAndroid Build Coastguard Worker        if (newline) {
302*61c4878aSAndroid Build Coastguard Worker          printf("\n");
303*61c4878aSAndroid Build Coastguard Worker        }
304*61c4878aSAndroid Build Coastguard Worker      }
305*61c4878aSAndroid Build Coastguard Worker
306*61c4878aSAndroid Build Coastguard Worker      int main() {
307*61c4878aSAndroid Build Coastguard Worker        // Make the binary compatible with pw_unit_test:googletest. Has no effect
308*61c4878aSAndroid Build Coastguard Worker        // when using pw_unit_test:light.
309*61c4878aSAndroid Build Coastguard Worker        testing::InitGoogleTest();
310*61c4878aSAndroid Build Coastguard Worker        // Set up the event handler for pw_unit_test:light.
311*61c4878aSAndroid Build Coastguard Worker        pw::unit_test::SimplePrintingEventHandler handler(WriteString);
312*61c4878aSAndroid Build Coastguard Worker        pw::unit_test::RegisterEventHandler(&handler);
313*61c4878aSAndroid Build Coastguard Worker        return RUN_ALL_TESTS();
314*61c4878aSAndroid Build Coastguard Worker      }
315*61c4878aSAndroid Build Coastguard Worker
316*61c4878aSAndroid Build Coastguard Worker   See :ref:`module-pw_unit_test-event-handlers` for more information about
317*61c4878aSAndroid Build Coastguard Worker   handling events.
318*61c4878aSAndroid Build Coastguard Worker
319*61c4878aSAndroid Build Coastguard Worker2. Set the build argument that instructs your build system to use your custom
320*61c4878aSAndroid Build Coastguard Worker   ``main`` function:
321*61c4878aSAndroid Build Coastguard Worker
322*61c4878aSAndroid Build Coastguard Worker   * Bazel: :option:`pw_unit_test_main`
323*61c4878aSAndroid Build Coastguard Worker   * GN: :option:`pw_unit_test_MAIN`
324*61c4878aSAndroid Build Coastguard Worker
325*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-event-handlers:
326*61c4878aSAndroid Build Coastguard Worker
327*61c4878aSAndroid Build Coastguard WorkerCreate event handlers
328*61c4878aSAndroid Build Coastguard Worker=====================
329*61c4878aSAndroid Build Coastguard Worker.. _//pw_unit_test/public/pw_unit_test/event_handler.h: https://cs.opensource.google/pigweed/pigweed/+/main:pw_unit_test/public/pw_unit_test/event_handler.h
330*61c4878aSAndroid Build Coastguard Worker
331*61c4878aSAndroid Build Coastguard WorkerThe ``pw::unit_test::EventHandler`` class defines the interface through which
332*61c4878aSAndroid Build Coastguard Worker``pw_unit_test:light`` communicates the results of its test runs. If you're
333*61c4878aSAndroid Build Coastguard Workerusing a :ref:`custom main function <module-pw_unit_test-main>` you need to
334*61c4878aSAndroid Build Coastguard Workerregister an event handler to receive test output from the framework.
335*61c4878aSAndroid Build Coastguard Worker
336*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-predefined-event-handlers:
337*61c4878aSAndroid Build Coastguard Worker
338*61c4878aSAndroid Build Coastguard WorkerPredefined event handlers
339*61c4878aSAndroid Build Coastguard Worker-------------------------
340*61c4878aSAndroid Build Coastguard WorkerPigweed provides some standard event handlers to simplify the process of
341*61c4878aSAndroid Build Coastguard Workergetting started with ``pw_unit_test:light``. All event handlers provide for
342*61c4878aSAndroid Build Coastguard WorkerGoogleTest-style output using the shared
343*61c4878aSAndroid Build Coastguard Worker:cpp:class:`pw::unit_test::GoogleTestStyleEventHandler` base. Example
344*61c4878aSAndroid Build Coastguard Workeroutput:
345*61c4878aSAndroid Build Coastguard Worker
346*61c4878aSAndroid Build Coastguard Worker.. code-block::
347*61c4878aSAndroid Build Coastguard Worker
348*61c4878aSAndroid Build Coastguard Worker   [==========] Running all tests.
349*61c4878aSAndroid Build Coastguard Worker   [ RUN      ] Status.Default
350*61c4878aSAndroid Build Coastguard Worker   [       OK ] Status.Default
351*61c4878aSAndroid Build Coastguard Worker   [ RUN      ] Status.ConstructWithStatusCode
352*61c4878aSAndroid Build Coastguard Worker   [       OK ] Status.ConstructWithStatusCode
353*61c4878aSAndroid Build Coastguard Worker   [ RUN      ] Status.AssignFromStatusCode
354*61c4878aSAndroid Build Coastguard Worker   [       OK ] Status.AssignFromStatusCode
355*61c4878aSAndroid Build Coastguard Worker   [ RUN      ] Status.CompareToStatusCode
356*61c4878aSAndroid Build Coastguard Worker   [       OK ] Status.CompareToStatusCode
357*61c4878aSAndroid Build Coastguard Worker   [ RUN      ] Status.Ok_OkIsTrue
358*61c4878aSAndroid Build Coastguard Worker   [       OK ] Status.Ok_OkIsTrue
359*61c4878aSAndroid Build Coastguard Worker   [ RUN      ] Status.NotOk_OkIsFalse
360*61c4878aSAndroid Build Coastguard Worker   [       OK ] Status.NotOk_OkIsFalse
361*61c4878aSAndroid Build Coastguard Worker   [ RUN      ] Status.KnownString
362*61c4878aSAndroid Build Coastguard Worker   [       OK ] Status.KnownString
363*61c4878aSAndroid Build Coastguard Worker   [ RUN      ] Status.UnknownString
364*61c4878aSAndroid Build Coastguard Worker   [       OK ] Status.UnknownString
365*61c4878aSAndroid Build Coastguard Worker   [==========] Done running all tests.
366*61c4878aSAndroid Build Coastguard Worker   [  PASSED  ] 8 test(s).
367*61c4878aSAndroid Build Coastguard Worker
368*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-subset:
369*61c4878aSAndroid Build Coastguard Worker
370*61c4878aSAndroid Build Coastguard WorkerRun a subset of test suites
371*61c4878aSAndroid Build Coastguard Worker===========================
372*61c4878aSAndroid Build Coastguard Worker.. note:: This feature is only supported in C++17.
373*61c4878aSAndroid Build Coastguard Worker
374*61c4878aSAndroid Build Coastguard Worker.. _//pw_unit_test/light_public_overrides/pw_unit_test/framework_backend.h: https://cs.opensource.google/pigweed/pigweed/+/main:pw_unit_test/light_public_overrides/pw_unit_test/framework_backend.h
375*61c4878aSAndroid Build Coastguard Worker
376*61c4878aSAndroid Build Coastguard WorkerTo run only a subset of registered test suites, use the
377*61c4878aSAndroid Build Coastguard Worker``pw::unit_test::SetTestSuitesToRun`` function. See
378*61c4878aSAndroid Build Coastguard Worker`//pw_unit_test/light_public_overrides/pw_unit_test/framework_backend.h`_.
379*61c4878aSAndroid Build Coastguard Worker
380*61c4878aSAndroid Build Coastguard WorkerThis is useful when you've got a lot of test suites bundled up into a
381*61c4878aSAndroid Build Coastguard Worker:ref:`single test binary <module-pw_unit_test-main>` and you only need
382*61c4878aSAndroid Build Coastguard Workerto run some of them.
383*61c4878aSAndroid Build Coastguard Worker
384*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-skip:
385*61c4878aSAndroid Build Coastguard Worker
386*61c4878aSAndroid Build Coastguard WorkerSkip tests in Bazel
387*61c4878aSAndroid Build Coastguard Worker===================
388*61c4878aSAndroid Build Coastguard WorkerUse ``target_compatible_with`` in Bazel to skip tests. The following test is
389*61c4878aSAndroid Build Coastguard Workerskipped when :ref:`using upstream GoogleTest <module-pw_unit_test-upstream>`:
390*61c4878aSAndroid Build Coastguard Worker
391*61c4878aSAndroid Build Coastguard Worker.. code-block::
392*61c4878aSAndroid Build Coastguard Worker
393*61c4878aSAndroid Build Coastguard Worker   load("//pw_unit_test:pw_cc_test.bzl", "pw_cc_test")
394*61c4878aSAndroid Build Coastguard Worker
395*61c4878aSAndroid Build Coastguard Worker   pw_cc_test(
396*61c4878aSAndroid Build Coastguard Worker       name = "no_upstream_test",
397*61c4878aSAndroid Build Coastguard Worker       srcs = ["no_upstream_test.cc"],
398*61c4878aSAndroid Build Coastguard Worker        target_compatible_with = select({
399*61c4878aSAndroid Build Coastguard Worker            "//pw_unit_test:light_setting": [],
400*61c4878aSAndroid Build Coastguard Worker            "//conditions:default": ["@platforms//:incompatible"],
401*61c4878aSAndroid Build Coastguard Worker        }),
402*61c4878aSAndroid Build Coastguard Worker   }
403*61c4878aSAndroid Build Coastguard Worker
404*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-static:
405*61c4878aSAndroid Build Coastguard Worker
406*61c4878aSAndroid Build Coastguard WorkerRun tests in static libraries
407*61c4878aSAndroid Build Coastguard Worker=============================
408*61c4878aSAndroid Build Coastguard WorkerTo run tests in a static library, use the
409*61c4878aSAndroid Build Coastguard Worker:c:macro:`PW_UNIT_TEST_LINK_FILE_CONTAINING_TEST` macro.
410*61c4878aSAndroid Build Coastguard Worker
411*61c4878aSAndroid Build Coastguard WorkerLinkers usually ignore tests through static libraries (i.e. ``.a`` files)
412*61c4878aSAndroid Build Coastguard Workerbecause test registration relies on the test instance's static constructor
413*61c4878aSAndroid Build Coastguard Workeradding itself to a global list of tests. When linking against a static library,
414*61c4878aSAndroid Build Coastguard Workerstatic constructors in an object file are ignored unless at least one entity
415*61c4878aSAndroid Build Coastguard Workerin that object file is linked.
416*61c4878aSAndroid Build Coastguard Worker
417*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-upstream:
418*61c4878aSAndroid Build Coastguard Worker
419*61c4878aSAndroid Build Coastguard WorkerUse upstream GoogleTest
420*61c4878aSAndroid Build Coastguard Worker=======================
421*61c4878aSAndroid Build Coastguard WorkerTo use the upstream GoogleTest backend (``pw_unit_test:googletest``) instead
422*61c4878aSAndroid Build Coastguard Workerof the default backend:
423*61c4878aSAndroid Build Coastguard Worker
424*61c4878aSAndroid Build Coastguard Worker.. _GoogleTestHandlerAdapter: https://cs.opensource.google/pigweed/pigweed/+/main:pw_unit_test/public/pw_unit_test/googletest_handler_adapter.h
425*61c4878aSAndroid Build Coastguard Worker
426*61c4878aSAndroid Build Coastguard Worker1. Clone the GoogleTest repository into your project. See
427*61c4878aSAndroid Build Coastguard Worker   :ref:`module-pw_third_party_googletest`.
428*61c4878aSAndroid Build Coastguard Worker
429*61c4878aSAndroid Build Coastguard Worker2. :ref:`Create a custom main function <module-pw_unit_test-main>`.
430*61c4878aSAndroid Build Coastguard Worker
431*61c4878aSAndroid Build Coastguard Worker3. Combine `GoogleTestHandlerAdapter`_ with a :ref:`predefined event
432*61c4878aSAndroid Build Coastguard Worker   handler <module-pw_unit_test-predefined-event-handlers>` to enable your
433*61c4878aSAndroid Build Coastguard Worker   ``main`` function to work with upstream GoogleTest without modification.
434*61c4878aSAndroid Build Coastguard Worker
435*61c4878aSAndroid Build Coastguard Worker   .. code-block:: c++
436*61c4878aSAndroid Build Coastguard Worker
437*61c4878aSAndroid Build Coastguard Worker      #include "pw_unit_test/framework.h"
438*61c4878aSAndroid Build Coastguard Worker      #include "pw_unit_test/logging_event_handler.h"
439*61c4878aSAndroid Build Coastguard Worker
440*61c4878aSAndroid Build Coastguard Worker      int main() {
441*61c4878aSAndroid Build Coastguard Worker        testing::InitGoogleTest();
442*61c4878aSAndroid Build Coastguard Worker        pw::unit_test::LoggingEventHandler logger;
443*61c4878aSAndroid Build Coastguard Worker        pw::unit_test::RegisterEventHandler(&logger);
444*61c4878aSAndroid Build Coastguard Worker        return RUN_ALL_TESTS();
445*61c4878aSAndroid Build Coastguard Worker      }
446*61c4878aSAndroid Build Coastguard Worker
447*61c4878aSAndroid Build Coastguard Worker4. If your tests needs GoogleTest functionality not included in the default
448*61c4878aSAndroid Build Coastguard Worker   ``pw_unit_test:light`` backend, include the upstream GoogleTest headers
449*61c4878aSAndroid Build Coastguard Worker   (e.g. ``gtest/gtest.h``) directly and guard your target definition to avoid
450*61c4878aSAndroid Build Coastguard Worker   compiling with ``pw_unit_test:light`` (the default backend).
451*61c4878aSAndroid Build Coastguard Worker
452*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-serial-runner:
453*61c4878aSAndroid Build Coastguard Worker
454*61c4878aSAndroid Build Coastguard WorkerRun tests over serial
455*61c4878aSAndroid Build Coastguard Worker=====================
456*61c4878aSAndroid Build Coastguard WorkerTo accelerate automated unit test bringup for devices with plain-text logging,
457*61c4878aSAndroid Build Coastguard Worker``pw_unit_test`` provides a serial-based test runner in Python that triggers a
458*61c4878aSAndroid Build Coastguard Workerdevice flash and evaluates whether the test passed or failed based on the
459*61c4878aSAndroid Build Coastguard Workerproduced output.
460*61c4878aSAndroid Build Coastguard Worker
461*61c4878aSAndroid Build Coastguard WorkerTo set up a serial test runner in Python:
462*61c4878aSAndroid Build Coastguard Worker
463*61c4878aSAndroid Build Coastguard Worker.. _//pw_unit_test/py/pw_unit_test/serial_test_runner.py: https://cs.opensource.google/pigweed/pigweed/+/main:pw_unit_test/py/pw_unit_test/serial_test_runner.py
464*61c4878aSAndroid Build Coastguard Worker
465*61c4878aSAndroid Build Coastguard Worker1. Implement a ``SerialTestingDevice`` class for your device. See
466*61c4878aSAndroid Build Coastguard Worker   `//pw_unit_test/py/pw_unit_test/serial_test_runner.py`_.
467*61c4878aSAndroid Build Coastguard Worker2. Configure your device code to wait to run unit tests until
468*61c4878aSAndroid Build Coastguard Worker   ``DEFAULT_TEST_START_CHARACTER`` is sent over the serial connection.
469*61c4878aSAndroid Build Coastguard Worker
470*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-rpc:
471*61c4878aSAndroid Build Coastguard Worker
472*61c4878aSAndroid Build Coastguard WorkerRun tests over RPC
473*61c4878aSAndroid Build Coastguard Worker==================
474*61c4878aSAndroid Build Coastguard Worker.. _//pw_unit_test/pw_unit_test_proto/unit_test.proto: https://cs.opensource.google/pigweed/pigweed/+/main:pw_unit_test/pw_unit_test_proto/unit_test.proto
475*61c4878aSAndroid Build Coastguard Worker
476*61c4878aSAndroid Build Coastguard Worker``pw_unit_test`` provides an RPC service which runs unit tests on demand and
477*61c4878aSAndroid Build Coastguard Workerstreams the results back to the client. The service is defined in
478*61c4878aSAndroid Build Coastguard Worker`//pw_unit_test/pw_unit_test_proto/unit_test.proto`_.
479*61c4878aSAndroid Build Coastguard Worker
480*61c4878aSAndroid Build Coastguard WorkerThe RPC service is primarily intended for use with the default
481*61c4878aSAndroid Build Coastguard Worker``pw_unit_test:light`` backend. It has some support for the upstream GoogleTest
482*61c4878aSAndroid Build Coastguard Workerbackend (``pw_unit_test:googletest``), however some features (such as test suite
483*61c4878aSAndroid Build Coastguard Workerfiltering) are missing.
484*61c4878aSAndroid Build Coastguard Worker
485*61c4878aSAndroid Build Coastguard WorkerTo set up RPC-based unit tests in your application:
486*61c4878aSAndroid Build Coastguard Worker
487*61c4878aSAndroid Build Coastguard Worker1. Depend on the relevant target for your build system:
488*61c4878aSAndroid Build Coastguard Worker
489*61c4878aSAndroid Build Coastguard Worker   * Bazel: ``@pigweed//pw_unit_test:rpc_service``
490*61c4878aSAndroid Build Coastguard Worker   * GN: ``$dir_pw_unit_test:rpc_service``
491*61c4878aSAndroid Build Coastguard Worker
492*61c4878aSAndroid Build Coastguard Worker2. Create a ``pw::unit_test::UnitTestService`` instance.
493*61c4878aSAndroid Build Coastguard Worker
494*61c4878aSAndroid Build Coastguard Worker3. Register the instance with your RPC server.
495*61c4878aSAndroid Build Coastguard Worker
496*61c4878aSAndroid Build Coastguard Worker   .. code-block:: c++
497*61c4878aSAndroid Build Coastguard Worker
498*61c4878aSAndroid Build Coastguard Worker      #include "pw_rpc/server.h"
499*61c4878aSAndroid Build Coastguard Worker      #include "pw_unit_test/unit_test_service.h"
500*61c4878aSAndroid Build Coastguard Worker
501*61c4878aSAndroid Build Coastguard Worker      pw::rpc::Channel channels[] = {
502*61c4878aSAndroid Build Coastguard Worker        pw::rpc::Channel::Create<1>(&my_output),
503*61c4878aSAndroid Build Coastguard Worker      };
504*61c4878aSAndroid Build Coastguard Worker      pw::rpc::Server server(channels);
505*61c4878aSAndroid Build Coastguard Worker
506*61c4878aSAndroid Build Coastguard Worker      pw::unit_test::UnitTestService unit_test_service;
507*61c4878aSAndroid Build Coastguard Worker
508*61c4878aSAndroid Build Coastguard Worker      void RegisterServices() {
509*61c4878aSAndroid Build Coastguard Worker        server.RegisterService(unit_test_services);
510*61c4878aSAndroid Build Coastguard Worker      }
511*61c4878aSAndroid Build Coastguard Worker
512*61c4878aSAndroid Build Coastguard Worker   See :ref:`module-pw_rpc` for more guidance around setting up RPC.
513*61c4878aSAndroid Build Coastguard Worker
514*61c4878aSAndroid Build Coastguard Worker4. Run tests that have been flashed to a device by calling
515*61c4878aSAndroid Build Coastguard Worker   ``pw_unit_test.rpc.run_tests()`` in Python. The argument should be an RPC
516*61c4878aSAndroid Build Coastguard Worker   client services object that has the unit testing RPC service enabled. By
517*61c4878aSAndroid Build Coastguard Worker   default, the results output via logging. The return value is a
518*61c4878aSAndroid Build Coastguard Worker   ``TestRecord`` dataclass instance containing the results of the test run.
519*61c4878aSAndroid Build Coastguard Worker
520*61c4878aSAndroid Build Coastguard Worker   .. code-block:: python
521*61c4878aSAndroid Build Coastguard Worker
522*61c4878aSAndroid Build Coastguard Worker      import serial
523*61c4878aSAndroid Build Coastguard Worker
524*61c4878aSAndroid Build Coastguard Worker      from pw_hdlc import rpc
525*61c4878aSAndroid Build Coastguard Worker      from pw_unit_test.rpc import run_tests
526*61c4878aSAndroid Build Coastguard Worker
527*61c4878aSAndroid Build Coastguard Worker      PROTO = Path(
528*61c4878aSAndroid Build Coastguard Worker          os.environ['PW_ROOT'],
529*61c4878aSAndroid Build Coastguard Worker          'pw_unit_test/pw_unit_test_proto/unit_test.proto'
530*61c4878aSAndroid Build Coastguard Worker      )
531*61c4878aSAndroid Build Coastguard Worker      serial_device = serial.Serial(device, baud)
532*61c4878aSAndroid Build Coastguard Worker      with rpc.SerialReader(serial_device) as reader:
533*61c4878aSAndroid Build Coastguard Worker          with rpc.HdlcRpcClient(
534*61c4878aSAndroid Build Coastguard Worker              reader, PROTO, rpc.default_channels(serial_device.write)
535*61c4878aSAndroid Build Coastguard Worker          ) as client:
536*61c4878aSAndroid Build Coastguard Worker              run_tests(client.rpcs())
537*61c4878aSAndroid Build Coastguard Worker
538*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-cpp:
539*61c4878aSAndroid Build Coastguard Worker
540*61c4878aSAndroid Build Coastguard Worker-----------------
541*61c4878aSAndroid Build Coastguard WorkerC++ API reference
542*61c4878aSAndroid Build Coastguard Worker-----------------
543*61c4878aSAndroid Build Coastguard Worker
544*61c4878aSAndroid Build Coastguard Worker``pw_status`` Helpers
545*61c4878aSAndroid Build Coastguard Worker=====================
546*61c4878aSAndroid Build Coastguard WorkerBoth the light and GoogleTest backends of ``pw_unit_test`` expose some matchers
547*61c4878aSAndroid Build Coastguard Workerfor dealing with Pigweed ``pw::Status`` and ``pw::Result`` values. See
548*61c4878aSAndroid Build Coastguard Worker:ref:`module-pw_unit_test-api-expect` and :ref:`module-pw_unit_test-api-assert`
549*61c4878aSAndroid Build Coastguard Workerfor details.
550*61c4878aSAndroid Build Coastguard Worker
551*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-compatibility:
552*61c4878aSAndroid Build Coastguard Worker
553*61c4878aSAndroid Build Coastguard Worker``pw_unit_test:light`` API compatibility
554*61c4878aSAndroid Build Coastguard Worker========================================
555*61c4878aSAndroid Build Coastguard Worker``pw_unit_test:light`` offers a number of primitives for test declaration,
556*61c4878aSAndroid Build Coastguard Workerassertion, event handlers, and configuration.
557*61c4878aSAndroid Build Coastguard Worker
558*61c4878aSAndroid Build Coastguard Worker.. note::
559*61c4878aSAndroid Build Coastguard Worker
560*61c4878aSAndroid Build Coastguard Worker   The ``googletest_test_matchers`` target which provides Pigweed-specific
561*61c4878aSAndroid Build Coastguard Worker   ``StatusIs``, ``IsOkAndHolds`` isn't part of the ``pw_unit_test:light``
562*61c4878aSAndroid Build Coastguard Worker   backend. These matchers are only usable when including the full upstream
563*61c4878aSAndroid Build Coastguard Worker   GoogleTest backend.
564*61c4878aSAndroid Build Coastguard Worker
565*61c4878aSAndroid Build Coastguard WorkerMissing features include:
566*61c4878aSAndroid Build Coastguard Worker
567*61c4878aSAndroid Build Coastguard Worker* GoogleMock and matchers (e.g. :c:macro:`EXPECT_THAT`).
568*61c4878aSAndroid Build Coastguard Worker* Death tests (e.g. :c:macro:`EXPECT_DEATH`). ``EXPECT_DEATH_IF_SUPPORTED``
569*61c4878aSAndroid Build Coastguard Worker  does nothing but silently passes.
570*61c4878aSAndroid Build Coastguard Worker* Value-parameterized tests.
571*61c4878aSAndroid Build Coastguard Worker* Stream messages (e.g. ``EXPECT_TRUE(...) << "My message"``) will compile, but
572*61c4878aSAndroid Build Coastguard Worker  no message will be logged.
573*61c4878aSAndroid Build Coastguard Worker
574*61c4878aSAndroid Build Coastguard WorkerSee :ref:`module-pw_unit_test-upstream` for guidance on using the
575*61c4878aSAndroid Build Coastguard Workerupstream GoogleTest backend (``pw_unit_test:googletest``) instead of
576*61c4878aSAndroid Build Coastguard Worker``pw_unit_test:light``.
577*61c4878aSAndroid Build Coastguard Worker
578*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-declare:
579*61c4878aSAndroid Build Coastguard Worker
580*61c4878aSAndroid Build Coastguard WorkerTest declaration
581*61c4878aSAndroid Build Coastguard Worker================
582*61c4878aSAndroid Build Coastguard WorkerNote that ``TEST_F`` may allocate fixtures separately from the stack.
583*61c4878aSAndroid Build Coastguard WorkerLarge variables should be stored in test fixture fields,
584*61c4878aSAndroid Build Coastguard Workerrather than stack variables. This allows the test framework to statically ensure
585*61c4878aSAndroid Build Coastguard Workerthat enough space is available to store these variables.
586*61c4878aSAndroid Build Coastguard Worker
587*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: TEST
588*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: GTEST_TEST
589*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: TEST_F
590*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: FRIEND_TEST
591*61c4878aSAndroid Build Coastguard Worker
592*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-control:
593*61c4878aSAndroid Build Coastguard Worker
594*61c4878aSAndroid Build Coastguard WorkerTest control
595*61c4878aSAndroid Build Coastguard Worker============
596*61c4878aSAndroid Build Coastguard Worker
597*61c4878aSAndroid Build Coastguard Worker.. doxygenfunction:: RUN_ALL_TESTS
598*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: FAIL
599*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: GTEST_FAIL
600*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: SUCCEED
601*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: GTEST_SUCCEED
602*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: GTEST_SKIP
603*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: ADD_FAILURE
604*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: GTEST_HAS_DEATH_TEST
605*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: EXPECT_DEATH_IF_SUPPORTED
606*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: ASSERT_DEATH_IF_SUPPORTED
607*61c4878aSAndroid Build Coastguard Worker
608*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-api-expect:
609*61c4878aSAndroid Build Coastguard Worker
610*61c4878aSAndroid Build Coastguard WorkerExpectations
611*61c4878aSAndroid Build Coastguard Worker============
612*61c4878aSAndroid Build Coastguard WorkerWhen a test fails an expectation, the framework marks the test as a failure
613*61c4878aSAndroid Build Coastguard Workerand then continues executing the test. They're useful when you want to
614*61c4878aSAndroid Build Coastguard Workerverify multiple dimensions of the same feature and see all the errors at the
615*61c4878aSAndroid Build Coastguard Workersame time.
616*61c4878aSAndroid Build Coastguard Worker
617*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: EXPECT_TRUE
618*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: EXPECT_FALSE
619*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: EXPECT_EQ
620*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: EXPECT_NE
621*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: EXPECT_GT
622*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: EXPECT_GE
623*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: EXPECT_LT
624*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: EXPECT_LE
625*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: EXPECT_NEAR
626*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: EXPECT_FLOAT_EQ
627*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: EXPECT_DOUBLE_EQ
628*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: EXPECT_STREQ
629*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: EXPECT_STRNE
630*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: PW_TEST_EXPECT_OK
631*61c4878aSAndroid Build Coastguard Worker
632*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-api-assert:
633*61c4878aSAndroid Build Coastguard Worker
634*61c4878aSAndroid Build Coastguard WorkerAssertions
635*61c4878aSAndroid Build Coastguard Worker==========
636*61c4878aSAndroid Build Coastguard WorkerAssertions work the same as expectations except they stop the execution of the
637*61c4878aSAndroid Build Coastguard Workertest as soon as a failed condition is met.
638*61c4878aSAndroid Build Coastguard Worker
639*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: ASSERT_TRUE
640*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: ASSERT_FALSE
641*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: ASSERT_EQ
642*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: ASSERT_NE
643*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: ASSERT_GT
644*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: ASSERT_GE
645*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: ASSERT_LT
646*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: ASSERT_LE
647*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: ASSERT_NEAR
648*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: ASSERT_FLOAT_EQ
649*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: ASSERT_DOUBLE_EQ
650*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: ASSERT_STREQ
651*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: ASSERT_STRNE
652*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: PW_TEST_ASSERT_OK
653*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: PW_TEST_ASSERT_OK_AND_ASSIGN
654*61c4878aSAndroid Build Coastguard Worker
655*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-api-event-handlers:
656*61c4878aSAndroid Build Coastguard Worker
657*61c4878aSAndroid Build Coastguard WorkerEvent handlers
658*61c4878aSAndroid Build Coastguard Worker==============
659*61c4878aSAndroid Build Coastguard Worker.. doxygenfunction:: pw::unit_test::RegisterEventHandler(EventHandler* event_handler)
660*61c4878aSAndroid Build Coastguard Worker.. doxygenclass:: pw::unit_test::EventHandler
661*61c4878aSAndroid Build Coastguard Worker   :members:
662*61c4878aSAndroid Build Coastguard Worker.. doxygenclass:: pw::unit_test::GoogleTestHandlerAdapter
663*61c4878aSAndroid Build Coastguard Worker.. doxygenclass:: pw::unit_test::GoogleTestStyleEventHandler
664*61c4878aSAndroid Build Coastguard Worker.. doxygenclass:: pw::unit_test::SimplePrintingEventHandler
665*61c4878aSAndroid Build Coastguard Worker.. doxygenclass:: pw::unit_test::LoggingEventHandler
666*61c4878aSAndroid Build Coastguard Worker.. doxygenclass:: pw::unit_test::PrintfEventHandler
667*61c4878aSAndroid Build Coastguard Worker.. doxygenclass:: pw::unit_test::MultiEventHandler
668*61c4878aSAndroid Build Coastguard Worker.. doxygenclass:: pw::unit_test::TestRecordEventHandler
669*61c4878aSAndroid Build Coastguard Worker
670*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-cpp-config:
671*61c4878aSAndroid Build Coastguard Worker
672*61c4878aSAndroid Build Coastguard WorkerConfiguration
673*61c4878aSAndroid Build Coastguard Worker=============
674*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: PW_UNIT_TEST_CONFIG_EVENT_BUFFER_SIZE
675*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: PW_UNIT_TEST_CONFIG_MEMORY_POOL_SIZE
676*61c4878aSAndroid Build Coastguard Worker
677*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-cpp-helpers:
678*61c4878aSAndroid Build Coastguard Worker
679*61c4878aSAndroid Build Coastguard WorkerHelpers
680*61c4878aSAndroid Build Coastguard Worker=======
681*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: PW_UNIT_TEST_LINK_FILE_CONTAINING_TEST
682*61c4878aSAndroid Build Coastguard Worker
683*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-py:
684*61c4878aSAndroid Build Coastguard Worker
685*61c4878aSAndroid Build Coastguard Worker--------------------
686*61c4878aSAndroid Build Coastguard WorkerPython API reference
687*61c4878aSAndroid Build Coastguard Worker--------------------
688*61c4878aSAndroid Build Coastguard Worker
689*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-py-serial_test_runner:
690*61c4878aSAndroid Build Coastguard Worker
691*61c4878aSAndroid Build Coastguard Worker``pw_unit_test.serial_test_runner``
692*61c4878aSAndroid Build Coastguard Worker===================================
693*61c4878aSAndroid Build Coastguard Worker.. automodule:: pw_unit_test.serial_test_runner
694*61c4878aSAndroid Build Coastguard Worker   :members:
695*61c4878aSAndroid Build Coastguard Worker     DEFAULT_TEST_START_CHARACTER,
696*61c4878aSAndroid Build Coastguard Worker     SerialTestingDevice,
697*61c4878aSAndroid Build Coastguard Worker     run_device_test,
698*61c4878aSAndroid Build Coastguard Worker
699*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-py-rpc:
700*61c4878aSAndroid Build Coastguard Worker
701*61c4878aSAndroid Build Coastguard Worker``pw_unit_test.rpc``
702*61c4878aSAndroid Build Coastguard Worker====================
703*61c4878aSAndroid Build Coastguard Worker.. automodule:: pw_unit_test.rpc
704*61c4878aSAndroid Build Coastguard Worker   :members: EventHandler, run_tests, TestRecord
705*61c4878aSAndroid Build Coastguard Worker
706*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-helpers:
707*61c4878aSAndroid Build Coastguard Worker
708*61c4878aSAndroid Build Coastguard Worker----------------------
709*61c4878aSAndroid Build Coastguard WorkerBuild helper libraries
710*61c4878aSAndroid Build Coastguard Worker----------------------
711*61c4878aSAndroid Build Coastguard WorkerThe following helper libraries can simplify setup and are supported in all
712*61c4878aSAndroid Build Coastguard Workerbuild systems.
713*61c4878aSAndroid Build Coastguard Worker
714*61c4878aSAndroid Build Coastguard Worker.. object:: simple_printing_event_handler
715*61c4878aSAndroid Build Coastguard Worker
716*61c4878aSAndroid Build Coastguard Worker   When running tests, output test results as plain text over ``pw_sys_io``.
717*61c4878aSAndroid Build Coastguard Worker
718*61c4878aSAndroid Build Coastguard Worker.. object:: simple_printing_main
719*61c4878aSAndroid Build Coastguard Worker
720*61c4878aSAndroid Build Coastguard Worker   Implements a ``main()`` function that simply runs tests using the
721*61c4878aSAndroid Build Coastguard Worker   ``simple_printing_event_handler``.
722*61c4878aSAndroid Build Coastguard Worker
723*61c4878aSAndroid Build Coastguard Worker.. object:: logging_event_handler
724*61c4878aSAndroid Build Coastguard Worker
725*61c4878aSAndroid Build Coastguard Worker   When running tests, log test results as plain text using
726*61c4878aSAndroid Build Coastguard Worker   :ref:`module-pw_log`. Make sure your target has set a ``pw_log`` backend.
727*61c4878aSAndroid Build Coastguard Worker
728*61c4878aSAndroid Build Coastguard Worker.. object:: logging_main
729*61c4878aSAndroid Build Coastguard Worker
730*61c4878aSAndroid Build Coastguard Worker   Implements a ``main()`` function that simply runs tests using the
731*61c4878aSAndroid Build Coastguard Worker   ``logging_event_handler``.
732*61c4878aSAndroid Build Coastguard Worker
733*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-bazel:
734*61c4878aSAndroid Build Coastguard Worker
735*61c4878aSAndroid Build Coastguard Worker-------------------
736*61c4878aSAndroid Build Coastguard WorkerBazel API reference
737*61c4878aSAndroid Build Coastguard Worker-------------------
738*61c4878aSAndroid Build Coastguard Worker
739*61c4878aSAndroid Build Coastguard WorkerSee also :ref:`module-pw_unit_test-helpers`.
740*61c4878aSAndroid Build Coastguard Worker
741*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-pw_cc_test:
742*61c4878aSAndroid Build Coastguard Worker
743*61c4878aSAndroid Build Coastguard Worker``pw_cc_test``
744*61c4878aSAndroid Build Coastguard Worker==============
745*61c4878aSAndroid Build Coastguard Worker.. _cc_test: https://bazel.build/reference/be/c-cpp#cc_test
746*61c4878aSAndroid Build Coastguard Worker
747*61c4878aSAndroid Build Coastguard Worker``pw_cc_test`` is a wrapper for `cc_test`_ that provides some defaults, such as
748*61c4878aSAndroid Build Coastguard Workera dependency on ``@pigweed//pw_unit_test:main``. It supports and passes through
749*61c4878aSAndroid Build Coastguard Workerall the arguments recognized by ``cc_test``.
750*61c4878aSAndroid Build Coastguard Worker
751*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-bazel-args:
752*61c4878aSAndroid Build Coastguard Worker
753*61c4878aSAndroid Build Coastguard WorkerBazel build arguments
754*61c4878aSAndroid Build Coastguard Worker=====================
755*61c4878aSAndroid Build Coastguard Worker.. option:: pw_unit_test_backend <target>
756*61c4878aSAndroid Build Coastguard Worker
757*61c4878aSAndroid Build Coastguard Worker   The GoogleTest implementation to use for Pigweed unit tests. This library
758*61c4878aSAndroid Build Coastguard Worker   provides ``gtest/gtest.h`` and related headers. Defaults to
759*61c4878aSAndroid Build Coastguard Worker   ``@pigweed//pw_unit_test:light``, which implements a subset of GoogleTest.
760*61c4878aSAndroid Build Coastguard Worker
761*61c4878aSAndroid Build Coastguard Worker   Type: string (Bazel target label)
762*61c4878aSAndroid Build Coastguard Worker
763*61c4878aSAndroid Build Coastguard Worker   Usage: toolchain-controlled only
764*61c4878aSAndroid Build Coastguard Worker
765*61c4878aSAndroid Build Coastguard Worker.. option:: pw_unit_test_main <target>
766*61c4878aSAndroid Build Coastguard Worker
767*61c4878aSAndroid Build Coastguard Worker   Implementation of a main function for ``pw_cc_test`` unit test binaries.
768*61c4878aSAndroid Build Coastguard Worker
769*61c4878aSAndroid Build Coastguard Worker   Type: string (Bazel target label)
770*61c4878aSAndroid Build Coastguard Worker
771*61c4878aSAndroid Build Coastguard Worker   Usage: toolchain-controlled only
772*61c4878aSAndroid Build Coastguard Worker
773*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-gn:
774*61c4878aSAndroid Build Coastguard Worker
775*61c4878aSAndroid Build Coastguard Worker------------
776*61c4878aSAndroid Build Coastguard WorkerGN reference
777*61c4878aSAndroid Build Coastguard Worker------------
778*61c4878aSAndroid Build Coastguard WorkerSee also :ref:`module-pw_unit_test-helpers`.
779*61c4878aSAndroid Build Coastguard Worker
780*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-pw_test:
781*61c4878aSAndroid Build Coastguard Worker
782*61c4878aSAndroid Build Coastguard Worker``pw_test``
783*61c4878aSAndroid Build Coastguard Worker===========
784*61c4878aSAndroid Build Coastguard Worker``pw_test`` defines a single unit test suite.
785*61c4878aSAndroid Build Coastguard Worker
786*61c4878aSAndroid Build Coastguard WorkerTargets
787*61c4878aSAndroid Build Coastguard Worker-------
788*61c4878aSAndroid Build Coastguard Worker
789*61c4878aSAndroid Build Coastguard Worker.. object:: <target_name>
790*61c4878aSAndroid Build Coastguard Worker
791*61c4878aSAndroid Build Coastguard Worker   The test suite within a single binary. The test code is linked against
792*61c4878aSAndroid Build Coastguard Worker   the target set in the build arg ``pw_unit_test_MAIN``.
793*61c4878aSAndroid Build Coastguard Worker
794*61c4878aSAndroid Build Coastguard Worker.. object:: <target_name>.run
795*61c4878aSAndroid Build Coastguard Worker
796*61c4878aSAndroid Build Coastguard Worker   If ``pw_unit_test_AUTOMATIC_RUNNER`` is set, this target runs the test as
797*61c4878aSAndroid Build Coastguard Worker   part of the build.
798*61c4878aSAndroid Build Coastguard Worker
799*61c4878aSAndroid Build Coastguard Worker.. object:: <target_name>.lib
800*61c4878aSAndroid Build Coastguard Worker
801*61c4878aSAndroid Build Coastguard Worker   The test sources without ``pw_unit_test_MAIN``.
802*61c4878aSAndroid Build Coastguard Worker
803*61c4878aSAndroid Build Coastguard WorkerArguments
804*61c4878aSAndroid Build Coastguard Worker---------
805*61c4878aSAndroid Build Coastguard WorkerAll GN executable arguments are accepted and forwarded to the underlying
806*61c4878aSAndroid Build Coastguard Worker``pw_executable``.
807*61c4878aSAndroid Build Coastguard Worker
808*61c4878aSAndroid Build Coastguard Worker.. option:: enable_if
809*61c4878aSAndroid Build Coastguard Worker
810*61c4878aSAndroid Build Coastguard Worker   Boolean indicating whether the test should be built. If false, replaces the
811*61c4878aSAndroid Build Coastguard Worker   test with an empty target. Default true.
812*61c4878aSAndroid Build Coastguard Worker
813*61c4878aSAndroid Build Coastguard Worker.. _get_target_outputs: https://gn.googlesource.com/gn/+/main/docs/reference.md#func_get_target_outputs
814*61c4878aSAndroid Build Coastguard Worker
815*61c4878aSAndroid Build Coastguard Worker.. option:: source_gen_deps
816*61c4878aSAndroid Build Coastguard Worker
817*61c4878aSAndroid Build Coastguard Worker   List of target labels that generate source files used by this test. The
818*61c4878aSAndroid Build Coastguard Worker   labels must meet the constraints of GN's `get_target_outputs`_, namely they must have been previously defined in the
819*61c4878aSAndroid Build Coastguard Worker   current file. This argument is required if a test uses generated source files
820*61c4878aSAndroid Build Coastguard Worker   and ``enable_if`` can evaluate to false.
821*61c4878aSAndroid Build Coastguard Worker
822*61c4878aSAndroid Build Coastguard Worker.. option:: test_main
823*61c4878aSAndroid Build Coastguard Worker
824*61c4878aSAndroid Build Coastguard Worker   Target label to add to the tests's dependencies to provide the ``main()``
825*61c4878aSAndroid Build Coastguard Worker   function. Defaults to ``pw_unit_test_MAIN``. Set to ``""`` if ``main()``
826*61c4878aSAndroid Build Coastguard Worker   is implemented in the test's ``sources``.
827*61c4878aSAndroid Build Coastguard Worker
828*61c4878aSAndroid Build Coastguard Worker.. option:: test_automatic_runner_args
829*61c4878aSAndroid Build Coastguard Worker
830*61c4878aSAndroid Build Coastguard Worker   Array of args to pass to :ref:`automatic test
831*61c4878aSAndroid Build Coastguard Worker   runner <module-pw_unit_test-serial-runner>`. Defaults to
832*61c4878aSAndroid Build Coastguard Worker   ``pw_unit_test_AUTOMATIC_RUNNER_ARGS``.
833*61c4878aSAndroid Build Coastguard Worker
834*61c4878aSAndroid Build Coastguard Worker.. option:: envvars
835*61c4878aSAndroid Build Coastguard Worker
836*61c4878aSAndroid Build Coastguard Worker   Array of ``key=value`` strings representing environment variables to set
837*61c4878aSAndroid Build Coastguard Worker   when invoking the automatic test runner.
838*61c4878aSAndroid Build Coastguard Worker
839*61c4878aSAndroid Build Coastguard WorkerExample
840*61c4878aSAndroid Build Coastguard Worker-------
841*61c4878aSAndroid Build Coastguard Worker
842*61c4878aSAndroid Build Coastguard Worker.. code-block::
843*61c4878aSAndroid Build Coastguard Worker
844*61c4878aSAndroid Build Coastguard Worker   import("$dir_pw_unit_test/test.gni")
845*61c4878aSAndroid Build Coastguard Worker
846*61c4878aSAndroid Build Coastguard Worker   pw_test("large_test") {
847*61c4878aSAndroid Build Coastguard Worker     sources = [ "large_test.cc" ]
848*61c4878aSAndroid Build Coastguard Worker     enable_if = device_has_1m_flash
849*61c4878aSAndroid Build Coastguard Worker   }
850*61c4878aSAndroid Build Coastguard Worker
851*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-pw_test_group:
852*61c4878aSAndroid Build Coastguard Worker
853*61c4878aSAndroid Build Coastguard Worker``pw_test_group``
854*61c4878aSAndroid Build Coastguard Worker=================
855*61c4878aSAndroid Build Coastguard Worker``pw_test_group`` defines a collection of tests or other test groups.
856*61c4878aSAndroid Build Coastguard Worker
857*61c4878aSAndroid Build Coastguard WorkerTargets
858*61c4878aSAndroid Build Coastguard Worker-------
859*61c4878aSAndroid Build Coastguard Worker.. object:: <target_name>
860*61c4878aSAndroid Build Coastguard Worker
861*61c4878aSAndroid Build Coastguard Worker   The test group itself.
862*61c4878aSAndroid Build Coastguard Worker
863*61c4878aSAndroid Build Coastguard Worker.. object:: <target_name>.run
864*61c4878aSAndroid Build Coastguard Worker
865*61c4878aSAndroid Build Coastguard Worker   If ``pw_unit_test_AUTOMATIC_RUNNER`` is set, this target runs all of the
866*61c4878aSAndroid Build Coastguard Worker   tests in the group and all of its group dependencies individually. See
867*61c4878aSAndroid Build Coastguard Worker   :ref:`module-pw_unit_test-serial-runner`.
868*61c4878aSAndroid Build Coastguard Worker
869*61c4878aSAndroid Build Coastguard Worker.. object:: <target_name>.lib
870*61c4878aSAndroid Build Coastguard Worker
871*61c4878aSAndroid Build Coastguard Worker   The sources of all of the tests in this group and their dependencies.
872*61c4878aSAndroid Build Coastguard Worker
873*61c4878aSAndroid Build Coastguard Worker.. object:: <target_name>.bundle
874*61c4878aSAndroid Build Coastguard Worker
875*61c4878aSAndroid Build Coastguard Worker   All of the tests in the group and its dependencies bundled into a single binary.
876*61c4878aSAndroid Build Coastguard Worker
877*61c4878aSAndroid Build Coastguard Worker.. object:: <target_name>.bundle.run
878*61c4878aSAndroid Build Coastguard Worker
879*61c4878aSAndroid Build Coastguard Worker   Automatic runner for the test bundle.
880*61c4878aSAndroid Build Coastguard Worker
881*61c4878aSAndroid Build Coastguard WorkerArguments
882*61c4878aSAndroid Build Coastguard Worker---------
883*61c4878aSAndroid Build Coastguard Worker.. option:: tests
884*61c4878aSAndroid Build Coastguard Worker
885*61c4878aSAndroid Build Coastguard Worker   List of the ``pw_test`` targets in the group.
886*61c4878aSAndroid Build Coastguard Worker
887*61c4878aSAndroid Build Coastguard Worker.. option:: group_deps
888*61c4878aSAndroid Build Coastguard Worker
889*61c4878aSAndroid Build Coastguard Worker   List of other ``pw_test_group`` targets on which this one depends.
890*61c4878aSAndroid Build Coastguard Worker
891*61c4878aSAndroid Build Coastguard Worker.. option:: enable_if
892*61c4878aSAndroid Build Coastguard Worker
893*61c4878aSAndroid Build Coastguard Worker   Boolean indicating whether the group target should be created. If false, an
894*61c4878aSAndroid Build Coastguard Worker   empty GN group is created instead. Default true.
895*61c4878aSAndroid Build Coastguard Worker
896*61c4878aSAndroid Build Coastguard WorkerExample
897*61c4878aSAndroid Build Coastguard Worker-------
898*61c4878aSAndroid Build Coastguard Worker.. code-block::
899*61c4878aSAndroid Build Coastguard Worker
900*61c4878aSAndroid Build Coastguard Worker   import("$dir_pw_unit_test/test.gni")
901*61c4878aSAndroid Build Coastguard Worker
902*61c4878aSAndroid Build Coastguard Worker   pw_test_group("tests") {
903*61c4878aSAndroid Build Coastguard Worker     tests = [
904*61c4878aSAndroid Build Coastguard Worker       ":bar_test",
905*61c4878aSAndroid Build Coastguard Worker       ":foo_test",
906*61c4878aSAndroid Build Coastguard Worker     ]
907*61c4878aSAndroid Build Coastguard Worker   }
908*61c4878aSAndroid Build Coastguard Worker
909*61c4878aSAndroid Build Coastguard Worker   pw_test("foo_test") {
910*61c4878aSAndroid Build Coastguard Worker     # ...
911*61c4878aSAndroid Build Coastguard Worker   }
912*61c4878aSAndroid Build Coastguard Worker
913*61c4878aSAndroid Build Coastguard Worker   pw_test("bar_test") {
914*61c4878aSAndroid Build Coastguard Worker     # ...
915*61c4878aSAndroid Build Coastguard Worker   }
916*61c4878aSAndroid Build Coastguard Worker
917*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-pw_facade_test:
918*61c4878aSAndroid Build Coastguard Worker
919*61c4878aSAndroid Build Coastguard Worker``pw_facade_test``
920*61c4878aSAndroid Build Coastguard Worker==================
921*61c4878aSAndroid Build Coastguard WorkerPigweed facade test templates allow individual unit tests to build under the
922*61c4878aSAndroid Build Coastguard Workercurrent device target configuration while overriding specific build arguments.
923*61c4878aSAndroid Build Coastguard WorkerThis allows these tests to replace a facade's backend for the purpose of testing
924*61c4878aSAndroid Build Coastguard Workerthe facade layer.
925*61c4878aSAndroid Build Coastguard Worker
926*61c4878aSAndroid Build Coastguard WorkerFacade tests are disabled by default. To build and run facade tests, set the GN
927*61c4878aSAndroid Build Coastguard Workerarg :option:`pw_unit_test_FACADE_TESTS_ENABLED` to ``true``.
928*61c4878aSAndroid Build Coastguard Worker
929*61c4878aSAndroid Build Coastguard Worker.. warning::
930*61c4878aSAndroid Build Coastguard Worker   Facade tests are costly because each facade test will trigger a re-build of
931*61c4878aSAndroid Build Coastguard Worker   every dependency of the test. While this sounds excessive, it's the only
932*61c4878aSAndroid Build Coastguard Worker   technically correct way to handle this type of test.
933*61c4878aSAndroid Build Coastguard Worker
934*61c4878aSAndroid Build Coastguard Worker.. warning::
935*61c4878aSAndroid Build Coastguard Worker   Some facade test configurations may not be compatible with your target. Be
936*61c4878aSAndroid Build Coastguard Worker   careful when running a facade test on a system that heavily depends on the
937*61c4878aSAndroid Build Coastguard Worker   facade being tested.
938*61c4878aSAndroid Build Coastguard Worker
939*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-gn-args:
940*61c4878aSAndroid Build Coastguard Worker
941*61c4878aSAndroid Build Coastguard WorkerGN build arguments
942*61c4878aSAndroid Build Coastguard Worker==================
943*61c4878aSAndroid Build Coastguard Worker.. option:: pw_unit_test_BACKEND <source_set>
944*61c4878aSAndroid Build Coastguard Worker
945*61c4878aSAndroid Build Coastguard Worker   The GoogleTest implementation to use for Pigweed unit tests. This library
946*61c4878aSAndroid Build Coastguard Worker   provides ``gtest/gtest.h`` and related headers. Defaults to
947*61c4878aSAndroid Build Coastguard Worker   ``pw_unit_test:light``, which implements a subset of GoogleTest.
948*61c4878aSAndroid Build Coastguard Worker
949*61c4878aSAndroid Build Coastguard Worker   Type: string (GN path to a source set)
950*61c4878aSAndroid Build Coastguard Worker
951*61c4878aSAndroid Build Coastguard Worker   Usage: toolchain-controlled only
952*61c4878aSAndroid Build Coastguard Worker
953*61c4878aSAndroid Build Coastguard Worker.. option:: pw_unit_test_MAIN <source_set>
954*61c4878aSAndroid Build Coastguard Worker
955*61c4878aSAndroid Build Coastguard Worker   Implementation of a main function for ``pw_test`` unit test binaries.
956*61c4878aSAndroid Build Coastguard Worker   See :ref:`module-pw_unit_test-main`.
957*61c4878aSAndroid Build Coastguard Worker
958*61c4878aSAndroid Build Coastguard Worker   Type: string (GN path to a source set)
959*61c4878aSAndroid Build Coastguard Worker
960*61c4878aSAndroid Build Coastguard Worker   Usage: toolchain-controlled only
961*61c4878aSAndroid Build Coastguard Worker
962*61c4878aSAndroid Build Coastguard Worker.. option:: pw_unit_test_AUTOMATIC_RUNNER <executable>
963*61c4878aSAndroid Build Coastguard Worker
964*61c4878aSAndroid Build Coastguard Worker   Path to a test runner to automatically run unit tests after they are built.
965*61c4878aSAndroid Build Coastguard Worker   See :ref:`module-pw_unit_test-serial-runner`.
966*61c4878aSAndroid Build Coastguard Worker
967*61c4878aSAndroid Build Coastguard Worker   If set, a ``pw_test`` target's ``<target_name>.run`` action invokes the
968*61c4878aSAndroid Build Coastguard Worker   test runner specified by this argument, passing the path to the unit test to
969*61c4878aSAndroid Build Coastguard Worker   run. If this is unset, the ``pw_test`` target's ``<target_name>.run`` step
970*61c4878aSAndroid Build Coastguard Worker   will do nothing.
971*61c4878aSAndroid Build Coastguard Worker
972*61c4878aSAndroid Build Coastguard Worker   Targets that don't support parallelized execution of tests (e.g. an
973*61c4878aSAndroid Build Coastguard Worker   on-device test runner that must flash a device and run the test in serial)
974*61c4878aSAndroid Build Coastguard Worker   should set ``pw_unit_test_POOL_DEPTH`` to ``1``.
975*61c4878aSAndroid Build Coastguard Worker
976*61c4878aSAndroid Build Coastguard Worker   Type: string (name of an executable on ``PATH``, or a path to an executable)
977*61c4878aSAndroid Build Coastguard Worker
978*61c4878aSAndroid Build Coastguard Worker   Usage: toolchain-controlled only
979*61c4878aSAndroid Build Coastguard Worker
980*61c4878aSAndroid Build Coastguard Worker.. option:: pw_unit_test_AUTOMATIC_RUNNER_ARGS <args>
981*61c4878aSAndroid Build Coastguard Worker
982*61c4878aSAndroid Build Coastguard Worker   An optional list of strings to pass as args to the test runner specified by
983*61c4878aSAndroid Build Coastguard Worker   ``pw_unit_test_AUTOMATIC_RUNNER``.
984*61c4878aSAndroid Build Coastguard Worker
985*61c4878aSAndroid Build Coastguard Worker   Type: list of strings (args to pass to ``pw_unit_test_AUTOMATIC_RUNNER``)
986*61c4878aSAndroid Build Coastguard Worker
987*61c4878aSAndroid Build Coastguard Worker   Usage: toolchain-controlled only
988*61c4878aSAndroid Build Coastguard Worker
989*61c4878aSAndroid Build Coastguard Worker.. option:: pw_unit_test_AUTOMATIC_RUNNER_TIMEOUT <timeout_seconds>
990*61c4878aSAndroid Build Coastguard Worker
991*61c4878aSAndroid Build Coastguard Worker   An optional timeout to apply when running the automatic runner. Timeout is
992*61c4878aSAndroid Build Coastguard Worker   in seconds. Defaults to empty which means no timeout.
993*61c4878aSAndroid Build Coastguard Worker
994*61c4878aSAndroid Build Coastguard Worker   Type: string (number of seconds to wait before killing test runner)
995*61c4878aSAndroid Build Coastguard Worker
996*61c4878aSAndroid Build Coastguard Worker   Usage: toolchain-controlled only
997*61c4878aSAndroid Build Coastguard Worker
998*61c4878aSAndroid Build Coastguard Worker.. option:: pw_unit_test_POOL_DEPTH <pool_depth>
999*61c4878aSAndroid Build Coastguard Worker
1000*61c4878aSAndroid Build Coastguard Worker   The maximum number of unit tests that may be run concurrently for the
1001*61c4878aSAndroid Build Coastguard Worker   current toolchain. Setting this to 0 disables usage of a pool, allowing
1002*61c4878aSAndroid Build Coastguard Worker   unlimited parallelization.
1003*61c4878aSAndroid Build Coastguard Worker
1004*61c4878aSAndroid Build Coastguard Worker   Note: A single target with two toolchain configurations (e.g. ``release``
1005*61c4878aSAndroid Build Coastguard Worker   and ``debug``) uses two separate test runner pools by default. Set
1006*61c4878aSAndroid Build Coastguard Worker   ``pw_unit_test_POOL_TOOLCHAIN`` to the same toolchain for both targets to
1007*61c4878aSAndroid Build Coastguard Worker   merge the pools and force serialization.
1008*61c4878aSAndroid Build Coastguard Worker
1009*61c4878aSAndroid Build Coastguard Worker   Type: integer
1010*61c4878aSAndroid Build Coastguard Worker
1011*61c4878aSAndroid Build Coastguard Worker   Usage: toolchain-controlled only
1012*61c4878aSAndroid Build Coastguard Worker
1013*61c4878aSAndroid Build Coastguard Worker.. option:: pw_unit_test_POOL_TOOLCHAIN <toolchain>
1014*61c4878aSAndroid Build Coastguard Worker
1015*61c4878aSAndroid Build Coastguard Worker   The toolchain to use when referring to the ``pw_unit_test`` runner pool.
1016*61c4878aSAndroid Build Coastguard Worker   When this is disabled, the current toolchain is used. This means that every
1017*61c4878aSAndroid Build Coastguard Worker   toolchain will use its own pool definition. If two toolchains should share
1018*61c4878aSAndroid Build Coastguard Worker   the same pool, this argument should be by one of the toolchains to the GN
1019*61c4878aSAndroid Build Coastguard Worker   path of the other toolchain.
1020*61c4878aSAndroid Build Coastguard Worker
1021*61c4878aSAndroid Build Coastguard Worker   Type: string (GN path to a toolchain)
1022*61c4878aSAndroid Build Coastguard Worker
1023*61c4878aSAndroid Build Coastguard Worker   Usage: toolchain-controlled only
1024*61c4878aSAndroid Build Coastguard Worker
1025*61c4878aSAndroid Build Coastguard Worker.. option:: pw_unit_test_EXECUTABLE_TARGET_TYPE <template name>
1026*61c4878aSAndroid Build Coastguard Worker
1027*61c4878aSAndroid Build Coastguard Worker   The name of the GN target type used to build ``pw_unit_test`` executables.
1028*61c4878aSAndroid Build Coastguard Worker
1029*61c4878aSAndroid Build Coastguard Worker   Type: string (name of a GN template)
1030*61c4878aSAndroid Build Coastguard Worker
1031*61c4878aSAndroid Build Coastguard Worker   Usage: toolchain-controlled only
1032*61c4878aSAndroid Build Coastguard Worker
1033*61c4878aSAndroid Build Coastguard Worker.. option:: pw_unit_test_EXECUTABLE_TARGET_TYPE_FILE <gni file path>
1034*61c4878aSAndroid Build Coastguard Worker
1035*61c4878aSAndroid Build Coastguard Worker   The path to the ``.gni`` file that defines
1036*61c4878aSAndroid Build Coastguard Worker   ``pw_unit_test_EXECUTABLE_TARGET_TYPE``.
1037*61c4878aSAndroid Build Coastguard Worker
1038*61c4878aSAndroid Build Coastguard Worker   If ``pw_unit_test_EXECUTABLE_TARGET_TYPE`` is not the default of
1039*61c4878aSAndroid Build Coastguard Worker   ``pw_executable``, this ``.gni`` file is imported to provide the template
1040*61c4878aSAndroid Build Coastguard Worker   definition.
1041*61c4878aSAndroid Build Coastguard Worker
1042*61c4878aSAndroid Build Coastguard Worker   Type: string (path to a .gni file)
1043*61c4878aSAndroid Build Coastguard Worker
1044*61c4878aSAndroid Build Coastguard Worker   Usage: toolchain-controlled only
1045*61c4878aSAndroid Build Coastguard Worker
1046*61c4878aSAndroid Build Coastguard Worker.. option:: pw_unit_test_FACADE_TESTS_ENABLED <boolean>
1047*61c4878aSAndroid Build Coastguard Worker
1048*61c4878aSAndroid Build Coastguard Worker   Controls whether to build and run facade tests. Facade tests add considerably
1049*61c4878aSAndroid Build Coastguard Worker   to build time, so they are disabled by default.
1050*61c4878aSAndroid Build Coastguard Worker
1051*61c4878aSAndroid Build Coastguard Worker.. option:: pw_unit_test_TESTONLY <boolean>
1052*61c4878aSAndroid Build Coastguard Worker
1053*61c4878aSAndroid Build Coastguard Worker   Controls the ``testonly`` variable in ``pw_test``, ``pw_test_group``, and
1054*61c4878aSAndroid Build Coastguard Worker   miscellaneous testing targets. This is useful if your test libraries (e.g.
1055*61c4878aSAndroid Build Coastguard Worker   GoogleTest) used by pw_unit_test have the ``testonly`` flag set. False by
1056*61c4878aSAndroid Build Coastguard Worker   default for backwards compatibility.
1057*61c4878aSAndroid Build Coastguard Worker
1058*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-cmake:
1059*61c4878aSAndroid Build Coastguard Worker
1060*61c4878aSAndroid Build Coastguard Worker---------------
1061*61c4878aSAndroid Build Coastguard WorkerCMake reference
1062*61c4878aSAndroid Build Coastguard Worker---------------
1063*61c4878aSAndroid Build Coastguard WorkerSee also :ref:`module-pw_unit_test-helpers`.
1064*61c4878aSAndroid Build Coastguard Worker
1065*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-pw_add_test:
1066*61c4878aSAndroid Build Coastguard Worker
1067*61c4878aSAndroid Build Coastguard Worker``pw_add_test``
1068*61c4878aSAndroid Build Coastguard Worker===============
1069*61c4878aSAndroid Build Coastguard Worker``pw_add_test`` declares a single unit test suite.
1070*61c4878aSAndroid Build Coastguard Worker
1071*61c4878aSAndroid Build Coastguard Worker.. tip::
1072*61c4878aSAndroid Build Coastguard Worker   Upstream Pigweed tests can be disabled in downstream projects by setting
1073*61c4878aSAndroid Build Coastguard Worker   ``pw_unit_test_ENABLE_PW_ADD_TEST`` to ``OFF`` before adding the ``pigweed``
1074*61c4878aSAndroid Build Coastguard Worker   directory to an existing cmake build.
1075*61c4878aSAndroid Build Coastguard Worker
1076*61c4878aSAndroid Build Coastguard Worker   .. code-block:: cmake
1077*61c4878aSAndroid Build Coastguard Worker
1078*61c4878aSAndroid Build Coastguard Worker      set(pw_unit_test_ENABLE_PW_ADD_TEST OFF)
1079*61c4878aSAndroid Build Coastguard Worker      add_subdirectory(path/to/pigweed pigweed)
1080*61c4878aSAndroid Build Coastguard Worker      set(pw_unit_test_ENABLE_PW_ADD_TEST ON)
1081*61c4878aSAndroid Build Coastguard Worker
1082*61c4878aSAndroid Build Coastguard Worker   See also: :ref:`module-pw_build-existing-cmake-project`.
1083*61c4878aSAndroid Build Coastguard Worker
1084*61c4878aSAndroid Build Coastguard WorkerTargets
1085*61c4878aSAndroid Build Coastguard Worker-------
1086*61c4878aSAndroid Build Coastguard Worker.. object:: {NAME}
1087*61c4878aSAndroid Build Coastguard Worker
1088*61c4878aSAndroid Build Coastguard Worker   Depends on ``${NAME}.run`` if ``pw_unit_test_AUTOMATIC_RUNNER`` is set, else
1089*61c4878aSAndroid Build Coastguard Worker   it depends on ``${NAME}.bin``.
1090*61c4878aSAndroid Build Coastguard Worker
1091*61c4878aSAndroid Build Coastguard Worker.. object:: {NAME}.lib
1092*61c4878aSAndroid Build Coastguard Worker
1093*61c4878aSAndroid Build Coastguard Worker   Contains the provided test sources as a library target, which can then be
1094*61c4878aSAndroid Build Coastguard Worker   linked into a test executable.
1095*61c4878aSAndroid Build Coastguard Worker
1096*61c4878aSAndroid Build Coastguard Worker.. object:: {NAME}.bin
1097*61c4878aSAndroid Build Coastguard Worker
1098*61c4878aSAndroid Build Coastguard Worker   A standalone executable which contains only the test sources specified in the
1099*61c4878aSAndroid Build Coastguard Worker   ``pw_unit_test`` template.
1100*61c4878aSAndroid Build Coastguard Worker
1101*61c4878aSAndroid Build Coastguard Worker.. object:: {NAME}.run
1102*61c4878aSAndroid Build Coastguard Worker
1103*61c4878aSAndroid Build Coastguard Worker   Runs the unit test executable after building it if
1104*61c4878aSAndroid Build Coastguard Worker   ``pw_unit_test_AUTOMATIC_RUNNER`` is set, else it fails to build.
1105*61c4878aSAndroid Build Coastguard Worker
1106*61c4878aSAndroid Build Coastguard WorkerRequired arguments
1107*61c4878aSAndroid Build Coastguard Worker------------------
1108*61c4878aSAndroid Build Coastguard Worker.. option:: NAME
1109*61c4878aSAndroid Build Coastguard Worker
1110*61c4878aSAndroid Build Coastguard Worker   Name to use for the produced test targets specified above.
1111*61c4878aSAndroid Build Coastguard Worker
1112*61c4878aSAndroid Build Coastguard WorkerOptional arguments
1113*61c4878aSAndroid Build Coastguard Worker------------------
1114*61c4878aSAndroid Build Coastguard Worker.. option:: SOURCES
1115*61c4878aSAndroid Build Coastguard Worker
1116*61c4878aSAndroid Build Coastguard Worker   Source files for this library.
1117*61c4878aSAndroid Build Coastguard Worker
1118*61c4878aSAndroid Build Coastguard Worker.. option:: HEADERS
1119*61c4878aSAndroid Build Coastguard Worker
1120*61c4878aSAndroid Build Coastguard Worker   Header files for this library.
1121*61c4878aSAndroid Build Coastguard Worker
1122*61c4878aSAndroid Build Coastguard Worker.. option:: PRIVATE_DEPS
1123*61c4878aSAndroid Build Coastguard Worker
1124*61c4878aSAndroid Build Coastguard Worker   Private ``pw_target_link_targets`` arguments.
1125*61c4878aSAndroid Build Coastguard Worker.. option:: PRIVATE_INCLUDES
1126*61c4878aSAndroid Build Coastguard Worker
1127*61c4878aSAndroid Build Coastguard Worker   Public ``target_include_directories`` argument.
1128*61c4878aSAndroid Build Coastguard Worker
1129*61c4878aSAndroid Build Coastguard Worker.. option:: PRIVATE_DEFINES
1130*61c4878aSAndroid Build Coastguard Worker
1131*61c4878aSAndroid Build Coastguard Worker   Private ``target_compile_definitions`` arguments.
1132*61c4878aSAndroid Build Coastguard Worker
1133*61c4878aSAndroid Build Coastguard Worker.. option:: PRIVATE_COMPILE_OPTIONS
1134*61c4878aSAndroid Build Coastguard Worker
1135*61c4878aSAndroid Build Coastguard Worker   Private ``target_compile_options`` arguments.
1136*61c4878aSAndroid Build Coastguard Worker
1137*61c4878aSAndroid Build Coastguard Worker.. option:: PRIVATE_LINK_OPTIONS
1138*61c4878aSAndroid Build Coastguard Worker
1139*61c4878aSAndroid Build Coastguard Worker   Private ``target_link_options`` arguments.
1140*61c4878aSAndroid Build Coastguard Worker
1141*61c4878aSAndroid Build Coastguard WorkerExample
1142*61c4878aSAndroid Build Coastguard Worker-------
1143*61c4878aSAndroid Build Coastguard Worker
1144*61c4878aSAndroid Build Coastguard Worker.. code-block::
1145*61c4878aSAndroid Build Coastguard Worker
1146*61c4878aSAndroid Build Coastguard Worker   include($ENV{PW_ROOT}/pw_unit_test/test.cmake)
1147*61c4878aSAndroid Build Coastguard Worker
1148*61c4878aSAndroid Build Coastguard Worker   pw_add_test(my_module.foo_test
1149*61c4878aSAndroid Build Coastguard Worker     SOURCES
1150*61c4878aSAndroid Build Coastguard Worker       foo_test.cc
1151*61c4878aSAndroid Build Coastguard Worker     PRIVATE_DEPS
1152*61c4878aSAndroid Build Coastguard Worker       my_module.foo
1153*61c4878aSAndroid Build Coastguard Worker   )
1154*61c4878aSAndroid Build Coastguard Worker
1155*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-pw_add_test_group:
1156*61c4878aSAndroid Build Coastguard Worker
1157*61c4878aSAndroid Build Coastguard Worker``pw_add_test_group``
1158*61c4878aSAndroid Build Coastguard Worker=====================
1159*61c4878aSAndroid Build Coastguard Worker``pw_add_test_group`` defines a collection of tests or other test groups.
1160*61c4878aSAndroid Build Coastguard Worker
1161*61c4878aSAndroid Build Coastguard WorkerTargets
1162*61c4878aSAndroid Build Coastguard Worker-------
1163*61c4878aSAndroid Build Coastguard Worker.. object:: {NAME}
1164*61c4878aSAndroid Build Coastguard Worker
1165*61c4878aSAndroid Build Coastguard Worker   Depends on ``${NAME}.run`` if ``pw_unit_test_AUTOMATIC_RUNNER`` is set, else
1166*61c4878aSAndroid Build Coastguard Worker   it depends on ``${NAME}.bin``.
1167*61c4878aSAndroid Build Coastguard Worker
1168*61c4878aSAndroid Build Coastguard Worker.. object:: {NAME}.bundle
1169*61c4878aSAndroid Build Coastguard Worker
1170*61c4878aSAndroid Build Coastguard Worker   Depends on ``${NAME}.bundle.run`` if ``pw_unit_test_AUTOMATIC_RUNNER`` is
1171*61c4878aSAndroid Build Coastguard Worker   set, else it depends on ``${NAME}.bundle.bin``.
1172*61c4878aSAndroid Build Coastguard Worker
1173*61c4878aSAndroid Build Coastguard Worker.. object:: {NAME}.lib
1174*61c4878aSAndroid Build Coastguard Worker
1175*61c4878aSAndroid Build Coastguard Worker   Depends on ``${NAME}.bundle.lib``.
1176*61c4878aSAndroid Build Coastguard Worker
1177*61c4878aSAndroid Build Coastguard Worker.. object:: {NAME}.bin
1178*61c4878aSAndroid Build Coastguard Worker
1179*61c4878aSAndroid Build Coastguard Worker   Depends on the provided ``TESTS``'s ``<test_dep>.bin`` targets.
1180*61c4878aSAndroid Build Coastguard Worker
1181*61c4878aSAndroid Build Coastguard Worker.. object:: {NAME}.run
1182*61c4878aSAndroid Build Coastguard Worker
1183*61c4878aSAndroid Build Coastguard Worker   Depends on the provided ``TESTS``'s ``<test_dep>.run`` targets if
1184*61c4878aSAndroid Build Coastguard Worker   ``pw_unit_test_AUTOMATIC_RUNNER`` is set, else it fails to build.
1185*61c4878aSAndroid Build Coastguard Worker
1186*61c4878aSAndroid Build Coastguard Worker.. object:: {NAME}.bundle.lib
1187*61c4878aSAndroid Build Coastguard Worker
1188*61c4878aSAndroid Build Coastguard Worker   Contains the provided tests bundled as a library target, which can then be
1189*61c4878aSAndroid Build Coastguard Worker   linked into a test executable.
1190*61c4878aSAndroid Build Coastguard Worker
1191*61c4878aSAndroid Build Coastguard Worker.. object:: {NAME}.bundle.bin
1192*61c4878aSAndroid Build Coastguard Worker
1193*61c4878aSAndroid Build Coastguard Worker   Standalone executable which contains the bundled tests.
1194*61c4878aSAndroid Build Coastguard Worker
1195*61c4878aSAndroid Build Coastguard Worker.. object:: {NAME}.bundle.run
1196*61c4878aSAndroid Build Coastguard Worker
1197*61c4878aSAndroid Build Coastguard Worker   Runs the ``{NAME}.bundle.bin`` test bundle executable after building it if
1198*61c4878aSAndroid Build Coastguard Worker   ``pw_unit_test_AUTOMATIC_RUNNER`` is set, else it fails to build.
1199*61c4878aSAndroid Build Coastguard Worker
1200*61c4878aSAndroid Build Coastguard WorkerRequired arguments
1201*61c4878aSAndroid Build Coastguard Worker------------------
1202*61c4878aSAndroid Build Coastguard Worker.. option:: NAME
1203*61c4878aSAndroid Build Coastguard Worker
1204*61c4878aSAndroid Build Coastguard Worker   The name of the executable target to be created.
1205*61c4878aSAndroid Build Coastguard Worker
1206*61c4878aSAndroid Build Coastguard Worker.. option:: TESTS
1207*61c4878aSAndroid Build Coastguard Worker
1208*61c4878aSAndroid Build Coastguard Worker   ``pw_add_test`` targets and ``pw_add_test_group`` bundles to be
1209*61c4878aSAndroid Build Coastguard Worker   included in this test bundle.
1210*61c4878aSAndroid Build Coastguard Worker
1211*61c4878aSAndroid Build Coastguard WorkerExample
1212*61c4878aSAndroid Build Coastguard Worker-------
1213*61c4878aSAndroid Build Coastguard Worker.. code-block::
1214*61c4878aSAndroid Build Coastguard Worker
1215*61c4878aSAndroid Build Coastguard Worker   include($ENV{PW_ROOT}/pw_unit_test/test.cmake)
1216*61c4878aSAndroid Build Coastguard Worker
1217*61c4878aSAndroid Build Coastguard Worker   pw_add_test_group(tests
1218*61c4878aSAndroid Build Coastguard Worker     TESTS
1219*61c4878aSAndroid Build Coastguard Worker       bar_test
1220*61c4878aSAndroid Build Coastguard Worker       foo_test
1221*61c4878aSAndroid Build Coastguard Worker   )
1222*61c4878aSAndroid Build Coastguard Worker
1223*61c4878aSAndroid Build Coastguard Worker   pw_add_test(foo_test
1224*61c4878aSAndroid Build Coastguard Worker     # ...
1225*61c4878aSAndroid Build Coastguard Worker   )
1226*61c4878aSAndroid Build Coastguard Worker
1227*61c4878aSAndroid Build Coastguard Worker   pw_add_test(bar_test
1228*61c4878aSAndroid Build Coastguard Worker     # ...
1229*61c4878aSAndroid Build Coastguard Worker   )
1230*61c4878aSAndroid Build Coastguard Worker
1231*61c4878aSAndroid Build Coastguard Worker.. _module-pw_unit_test-cmake-args:
1232*61c4878aSAndroid Build Coastguard Worker
1233*61c4878aSAndroid Build Coastguard WorkerCMake build arguments
1234*61c4878aSAndroid Build Coastguard Worker=====================
1235*61c4878aSAndroid Build Coastguard Worker.. option:: pw_unit_test_BACKEND <target>
1236*61c4878aSAndroid Build Coastguard Worker
1237*61c4878aSAndroid Build Coastguard Worker   The GoogleTest implementation to use for Pigweed unit tests. This library
1238*61c4878aSAndroid Build Coastguard Worker   provides ``gtest/gtest.h`` and related headers. Defaults to
1239*61c4878aSAndroid Build Coastguard Worker   ``pw_unit_test.light``, which implements a subset of GoogleTest.
1240*61c4878aSAndroid Build Coastguard Worker
1241*61c4878aSAndroid Build Coastguard Worker   Type: string (CMake target name)
1242*61c4878aSAndroid Build Coastguard Worker
1243*61c4878aSAndroid Build Coastguard Worker   Usage: toolchain-controlled only
1244*61c4878aSAndroid Build Coastguard Worker
1245*61c4878aSAndroid Build Coastguard Worker.. option:: pw_unit_test_AUTOMATIC_RUNNER <executable>
1246*61c4878aSAndroid Build Coastguard Worker
1247*61c4878aSAndroid Build Coastguard Worker   Path to a test runner to automatically run unit tests after they're built.
1248*61c4878aSAndroid Build Coastguard Worker
1249*61c4878aSAndroid Build Coastguard Worker   If set, a ``pw_test`` target's ``${NAME}`` and ``${NAME}.run`` targets will
1250*61c4878aSAndroid Build Coastguard Worker   invoke the test runner specified by this argument, passing the path to the
1251*61c4878aSAndroid Build Coastguard Worker   unit test to run. If this is unset, the ``pw_test`` target's ``${NAME}`` will
1252*61c4878aSAndroid Build Coastguard Worker   only build the unit test(s) and ``${NAME}.run`` will fail to build.
1253*61c4878aSAndroid Build Coastguard Worker
1254*61c4878aSAndroid Build Coastguard Worker   Type: string (name of an executable on the PATH, or path to an executable)
1255*61c4878aSAndroid Build Coastguard Worker
1256*61c4878aSAndroid Build Coastguard Worker   Usage: toolchain-controlled only
1257*61c4878aSAndroid Build Coastguard Worker
1258*61c4878aSAndroid Build Coastguard Worker.. option:: pw_unit_test_AUTOMATIC_RUNNER_ARGS <args>
1259*61c4878aSAndroid Build Coastguard Worker
1260*61c4878aSAndroid Build Coastguard Worker   An optional list of strings to pass as args to the test runner specified
1261*61c4878aSAndroid Build Coastguard Worker   by ``pw_unit_test_AUTOMATIC_RUNNER``.
1262*61c4878aSAndroid Build Coastguard Worker
1263*61c4878aSAndroid Build Coastguard Worker   Type: list of strings (args to pass to pw_unit_test_AUTOMATIC_RUNNER)
1264*61c4878aSAndroid Build Coastguard Worker
1265*61c4878aSAndroid Build Coastguard Worker   Usage: toolchain-controlled only
1266*61c4878aSAndroid Build Coastguard Worker
1267*61c4878aSAndroid Build Coastguard Worker.. option:: pw_unit_test_AUTOMATIC_RUNNER_TIMEOUT_SECONDS <timeout_seconds>
1268*61c4878aSAndroid Build Coastguard Worker
1269*61c4878aSAndroid Build Coastguard Worker   An optional timeout to apply when running the automatic runner. Timeout is
1270*61c4878aSAndroid Build Coastguard Worker   in seconds. Defaults to empty which means no timeout.
1271*61c4878aSAndroid Build Coastguard Worker
1272*61c4878aSAndroid Build Coastguard Worker   Type: string (number of seconds to wait before killing test runner)
1273*61c4878aSAndroid Build Coastguard Worker
1274*61c4878aSAndroid Build Coastguard Worker   Usage: toolchain-controlled only
1275*61c4878aSAndroid Build Coastguard Worker
1276*61c4878aSAndroid Build Coastguard Worker.. option:: pw_unit_test_ADD_EXECUTABLE_FUNCTION <function name>
1277*61c4878aSAndroid Build Coastguard Worker
1278*61c4878aSAndroid Build Coastguard Worker   The name of the CMake function used to build ``pw_unit_test`` executables.
1279*61c4878aSAndroid Build Coastguard Worker   The provided function must take a ``NAME`` and a ``TEST_LIB`` argument which
1280*61c4878aSAndroid Build Coastguard Worker   are the expected name of the executable target and the target which provides
1281*61c4878aSAndroid Build Coastguard Worker   the unit test(s).
1282*61c4878aSAndroid Build Coastguard Worker
1283*61c4878aSAndroid Build Coastguard Worker   Type: string (name of a CMake function)
1284*61c4878aSAndroid Build Coastguard Worker
1285*61c4878aSAndroid Build Coastguard Worker   Usage: toolchain-controlled only
1286*61c4878aSAndroid Build Coastguard Worker
1287*61c4878aSAndroid Build Coastguard Worker.. option:: pw_unit_test_ADD_EXECUTABLE_FUNCTION_FILE <cmake file path>
1288*61c4878aSAndroid Build Coastguard Worker
1289*61c4878aSAndroid Build Coastguard Worker   The path to the ``.cmake`` file that defines
1290*61c4878aSAndroid Build Coastguard Worker   ``pw_unit_test_ADD_EXECUTABLE_FUNCTION``.
1291*61c4878aSAndroid Build Coastguard Worker
1292*61c4878aSAndroid Build Coastguard Worker   Type: string (path to a ``.cmake`` file)
1293*61c4878aSAndroid Build Coastguard Worker
1294*61c4878aSAndroid Build Coastguard Worker   Usage: toolchain-controlled only
1295