1 .. _module-pw_unit_test:
6 ``pw_unit_test`` unit testing library with a `Google Test`_-compatible API,
7 built on top of embedded-friendly primitives.
9 .. _Google Test: https://github.com/google/googletest/blob/master/googletest/docs/primer.md
11 ``pw_unit_test`` is a portable library which can run on almost any system from
12 from bare metal to a full-fledged desktop OS. It does this by offloading the
13 responsibility of test reporting and output to the underlying system,
14 communicating its results through a common interface. Unit tests can be written
15 once and run under many different environments, empowering developers to write
16 robust, high quality code.
18 ``pw_unit_test`` is still under development and lacks many features expected in
19 a complete testing framework; nevertheless, it is already used heavily within
24 This documentation is currently incomplete.
29 ``pw_unit_test``'s interface is largely compatible with `Google Test`_. Refer to
30 the Google Test documentation for examples of to define unit test cases.
34 A lot of Google Test's more advanced features are not yet implemented. To
35 request a feature addition, please
36 `let us know <mailto:pigweed@googlegroups.com>`_.
38 Using the test framework
39 ========================
41 The EventHandler interface
42 ^^^^^^^^^^^^^^^^^^^^^^^^^^
43 The ``EventHandler`` class in ``public/pw_unit_test/event_handler.h`` defines
44 the interface through which ``pw_unit_test`` communicates the results of its
45 test runs. A platform using ``pw_unit_test`` must register an event handler with
46 the unit testing framework to receive test output.
48 As the framework runs tests, it calls the event handler's callback functions to
49 notify the system of various test events. The system can then choose to perform
50 any necessary handling or aggregation of these events, and report them back to
53 Predefined event handlers
54 -------------------------
55 Pigweed provides some standard event handlers upstream to simplify the process
56 of getting started using ``pw_unit_test``.
58 * ``SimplePrintingEventHandler``: An event handler that writes Google Test-style
59 output to a specified sink.
63 [==========] Running all tests.
64 [ RUN ] Status.Default
66 [ RUN ] Status.ConstructWithStatusCode
67 [ OK ] Status.ConstructWithStatusCode
68 [ RUN ] Status.AssignFromStatusCode
69 [ OK ] Status.AssignFromStatusCode
70 [ RUN ] Status.CompareToStatusCode
71 [ OK ] Status.CompareToStatusCode
72 [ RUN ] Status.Ok_OkIsTrue
73 [ OK ] Status.Ok_OkIsTrue
74 [ RUN ] Status.NotOk_OkIsFalse
75 [ OK ] Status.NotOk_OkIsFalse
76 [ RUN ] Status.KnownString
77 [ OK ] Status.KnownString
78 [ RUN ] Status.UnknownString
79 [ OK ] Status.UnknownString
80 [==========] Done running all tests.
84 * ``LoggingEventHandler``: An event handler which uses the ``pw_log`` module to
85 output test results, to integrate with the system's existing logging setup.
91 To run unit tests, link the tests into a single binary with the unit testing
92 framework, register an event handler, and call the ``RUN_ALL_TESTS`` macro.
96 #include "pw_unit_test/framework.h"
97 #include "pw_unit_test/simple_printing_event_handler.h"
99 void WriteString(const std::string_view& string, bool newline) {
100 printf("%s", string.data());
107 pw::unit_test::SimplePrintingEventHandler handler(WriteString);
108 pw::unit_test::RegisterEventHandler(&handler);
109 return RUN_ALL_TESTS();
112 Build system integration
113 ^^^^^^^^^^^^^^^^^^^^^^^^
114 ``pw_unit_test`` integrates directly into Pigweed's GN build system. To define
115 simple unit tests, set the ``pw_unit_test_MAIN`` build variable to a target
116 which configures the test framework as described in the :ref:`running-tests`
117 section, and use the ``pw_test`` template to register your test code.
121 import("$dir_pw_unit_test/test.gni")
123 pw_test("foo_test") {
124 sources = [ "foo_test.cc" ]
127 The ``pw_unit_test`` module provides a few optional libraries to simplify setup:
129 - ``simple_printing_event_handler```: When running tests, output test results
130 as plain text over ``pw_sys_io``.
131 - ``simple_printing_main``: Implements a ``main()`` function that simply runs
132 tests using the ``simple_printing_event_handler``.
133 - ``logging_event_handler``: When running tests, log test results as
134 plain text using pw_log (ensure your target has set a ``pw_log`` backend).
135 - ``logging_main``: Implements a ``main()`` function that simply runs tests
136 using the ``logging_event_handler``.
141 ``pw_test`` defines a single unit test suite. It creates several sub-targets.
143 * ``<target_name>``: The test suite within a single binary. The test code is
144 linked against the target set in the build arg ``pw_unit_test_MAIN``.
145 * ``<target_name>.run``: If ``pw_unit_test_AUTOMATIC_RUNNER`` is set, this
146 target runs the test as part of the build.
147 * ``<target_name>.lib``: The test sources without ``pw_unit_test_MAIN``.
151 * All GN executable arguments are accepted and forwarded to the underlying
153 * ``enable_if``: Boolean indicating whether the test should be built. If false,
154 replaces the test with an empty target. Default true.
155 * ``test_main``: Target label to add to the tests's dependencies to provide the
156 ``main()`` function. Defaults to ``pw_unit_test_MAIN``. Set to ``""`` if
157 ``main()`` is implemented in the test's ``sources``.
163 import("$dir_pw_unit_test/test.gni")
165 pw_test("large_test") {
166 sources = [ "large_test.cc" ]
167 enable_if = device_has_1m_flash
171 pw_test_group template
172 ----------------------
173 ``pw_test_group`` defines a collection of tests or other test groups. It creates
176 * ``<target_name>``: The test group itself.
177 * ``<target_name>.run``: If ``pw_unit_test_AUTOMATIC_RUNNER`` is set, this
178 target runs all of the tests in the group and all of its group dependencies
180 * ``<target_name>.lib``: The sources of all of the tests in this group and its
182 * ``<target_name>.bundle``: All of the tests in the group and its dependencies
183 bundled into a single binary.
184 * ``<target_name>.bundle.run``: Automatic runner for the test bundle.
188 * ``tests``: List of the ``pw_test`` targets in the group.
189 * ``group_deps``: List of other ``pw_test_group`` targets on which this one
191 * ``enable_if``: Boolean indicating whether the group target should be created.
192 If false, an empty GN group is created instead. Default true.
198 import("$dir_pw_unit_test/test.gni")
200 pw_test_group("tests") {
207 pw_test("foo_test") {
211 pw_test("bar_test") {
217 ``pw_unit_test`` provides an RPC service which runs unit tests on demand and
218 streams the results back to the client. The service is defined in
219 ``pw_unit_test_proto/unit_test.proto``, and implemented by the GN target
220 ``$dir_pw_unit_test:rpc_service``.
222 To set up RPC-based unit tests in your application, instantiate a
223 ``pw::unit_test::UnitTestService`` and register it with your RPC server.
227 #include "pw_rpc/server.h"
228 #include "pw_unit_test/unit_test_service.h"
230 // Server setup; refer to pw_rpc docs for more information.
231 pw::rpc::Channel channels[] = {
232 pw::rpc::Channel::Create<1>(&my_output),
234 pw::rpc::Server server(channels);
236 pw::unit_test::UnitTestService unit_test_service;
238 void RegisterServices() {
239 server.RegisterService(unit_test_services);
242 All tests flashed to an attached device can be run via python by calling
243 ``pw_unit_test.rpc.run_tests()`` with a RPC client services object that has
244 the unit testing RPC service enabled. By default, the results will output via
249 from pw_hdlc.rpc import HdlcRpcClient
250 from pw_unit_test.rpc import run_tests
252 PROTO = Path(os.environ['PW_ROOT'],
253 'pw_unit_test/pw_unit_test_proto/unit_test.proto')
255 client = HdlcRpcClient(serial.Serial(device, baud), PROTO)
256 run_tests(client.rpcs())