xref: /aosp_15_r20/external/pigweed/docs/showcases/sense/tutorial/device_tests.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1.. _showcase-sense-tutorial-devicetests:
2
3======================
48. Run on-device tests
5======================
6.. note::
7
8   This step requires the :ref:`full hardware
9   setup <showcase-sense-tutorial-full>` and must be
10   run over a terminal.
11
12Pigweed is all about helping big teams scale, so of course we built out
13a robust and automated solution for running tests on-device! Try it now:
14
15.. _showcase-sense-tutorial-devicetests-setup:
16
17--------------------
18Set up your hardware
19--------------------
20#. Set up your hardware as described in :ref:`showcase-sense-tutorial-full`.
21   On-device tests don't work with the basic setup. You can skip ahead to
22   :ref:`showcase-sense-tutorial-pico-rpc` if you don't have the full setup
23   hardware.
24
25.. _showcase-sense-tutorial-devicetests-run:
26
27-------------------
28Run on-device tests
29-------------------
30.. _Command Palette: https://code.visualstudio.com/docs/getstarted/userinterface#_command-palette
31.. _terminal: https://code.visualstudio.com/docs/terminal/basics
32
33.. tab-set::
34
35   .. tab-item:: VS Code
36      :sync: vsc
37
38      #. Open the `Command Palette`_.
39
40      #. Run the ``Pigweed: Set Bazelisk Path`` command and select
41         **Use the version built into the Pigweed extension**.
42
43      #. Open the Command Palette again and run the
44         ``Pigweed: Activate Bazelisk In Terminal`` command.
45
46      #. Open a VS Code `terminal`_.
47
48         .. note::
49
50            This section requires the VS Code terminal because there's currently
51            no way to run wildcard tests from the **Bazel Build Targets** UI.
52            Wildcard tests will be explained more in a moment.
53
54      #. Start the test runner:
55
56         .. code-block:: console
57
58            $ bazelisk run \
59                @pigweed//targets/rp2040/py:unit_test_server \
60                -- --chip RP2040 --debug-probe-only
61            INFO: Analyzed target @@pigweed~//targets/rp2040/py:unit_test_server (134 packages loaded, 13872 targets configured).
62            INFO: Found 1 target...
63            Target @@pigweed~//targets/rp2040/py:unit_test_server up-to-date:
64              bazel-bin/external/pigweed~/targets/rp2040/py/unit_test_server
65            INFO: Elapsed time: 32.497s, Critical Path: 18.71s
66            INFO: 177 processes: 12 internal, 165 linux-sandbox.
67            INFO: Build completed successfully, 177 total actions
68            INFO: Running command line: bazel-bin/external/pigweed~/targets/rp2040/py/unit_test_server <args omitted>
69            20240806 18:22:29 OUT [370633] 2024/08/06 18:22:29 Parsed server configuration from /tmp/tmparhr7i8o
70            20240806 18:22:29 OUT [370633] 2024/08/06 18:22:29 Registered ExecDeviceRunner /home/kayce/.cache/bazel/_bazel_kayce/12747149b267f61f52f2c26162a31942/execroot/_main/bazel-out/k8-fastbuild/bin/external/pigweed~/targets/rp2040/py/rpc_unit_test_runner with args [--usb-bus 3 --usb-port 6]
71            20240806 18:22:29 OUT [370633] 2024/08/06 18:22:29 Starting gRPC server on [::]:34172
72            20240806 18:22:29 OUT [370633] [ServerWorkerPool] 2024/08/06 18:22:29 Starting 1 workers
73            20240806 18:22:29 OUT [370633] [ExecDeviceRunner 0] 2024/08/06 18:22:29 Starting worker
74
75         The test runner essentially orchestrates how all the unit
76         tests should be run.
77
78         .. note::
79
80            You don't need to manually install ``bazelisk``. The Pigweed extension
81            put its bundled version of ``bazelisk`` on your ``$PATH`` when
82            you ran the ``Pigweed: Set Bazelisk Path`` command.
83
84      #. Open another terminal and run the tests:
85
86         .. code-block:: console
87
88            $ bazelisk test --config=rp2040 //...
89            # ...
90            INFO: Found 134 targets and 10 test targets...
91            INFO: Elapsed time: 131.231s, Critical Path: 60.93s
92            INFO: 2368 processes: 423 internal, 1945 linux-sandbox.
93            INFO: Build completed successfully, 2368 total actions
94            //modules/pubsub:service_test                                  SKIPPED
95            //modules/air_sensor:air_sensor_test                           PASSED in 30.2s
96            //modules/blinky:blinky_test                                   PASSED in 14.3s
97            //modules/buttons:manager_test                                 PASSED in 41.0s
98            //modules/edge_detector:hysteresis_edge_detector_test          PASSED in 7.3s
99            //modules/lerp:lerp_test                                       PASSED in 26.0s
100            //modules/morse_code:encoder_test                              PASSED in 35.6s
101            //modules/pubsub:pubsub_events_test                            PASSED in 18.2s
102            //modules/pubsub:pubsub_test                                   PASSED in 22.1s
103            //modules/state_manager:state_manager_test                     PASSED in 38.5s
104
105            Executed 9 out of 10 tests: 9 tests pass and 1 was skipped.
106
107      #. Go to the terminal that the test runner is running in and press
108         :kbd:`Control+C` to close it.
109
110   .. tab-item:: CLI
111      :sync: cli
112
113      #. Open a terminal and start a test runner:
114
115         .. code-block:: console
116
117            $ bazelisk run \
118                @pigweed//targets/rp2040/py:unit_test_server \
119                -- --debug-probe-only
120            INFO: Analyzed target @@pigweed~//targets/rp2040/py:unit_test_server (134 packages loaded, 13872 targets configured).
121            INFO: Found 1 target...
122            Target @@pigweed~//targets/rp2040/py:unit_test_server up-to-date:
123              bazel-bin/external/pigweed~/targets/rp2040/py/unit_test_server
124            INFO: Elapsed time: 32.497s, Critical Path: 18.71s
125            INFO: 177 processes: 12 internal, 165 linux-sandbox.
126            INFO: Build completed successfully, 177 total actions
127            INFO: Running command line: bazel-bin/external/pigweed~/targets/rp2040/py/unit_test_server <args omitted>
128            20240806 18:22:29 OUT [370633] 2024/08/06 18:22:29 Parsed server configuration from /tmp/tmparhr7i8o
129            20240806 18:22:29 OUT [370633] 2024/08/06 18:22:29 Registered ExecDeviceRunner /home/kayce/.cache/bazel/_bazel_kayce/12747149b267f61f52f2c26162a31942/execroot/_main/bazel-out/k8-fastbuild/bin/external/pigweed~/targets/rp2040/py/rpc_unit_test_runner with args [--usb-bus 3 --usb-port 6]
130            20240806 18:22:29 OUT [370633] 2024/08/06 18:22:29 Starting gRPC server on [::]:34172
131            20240806 18:22:29 OUT [370633] [ServerWorkerPool] 2024/08/06 18:22:29 Starting 1 workers
132            20240806 18:22:29 OUT [370633] [ExecDeviceRunner 0] 2024/08/06 18:22:29 Starting worker
133
134         The test runner essentially orchestrates how all the unit
135         tests should be run.
136
137      #. Open another terminal and run the tests:
138
139         .. code-block:: console
140
141            $ bazelisk test --config=rp2040 //...
142            # ...
143            INFO: Found 134 targets and 10 test targets...
144            INFO: Elapsed time: 131.231s, Critical Path: 60.93s
145            INFO: 2368 processes: 423 internal, 1945 linux-sandbox.
146            INFO: Build completed successfully, 2368 total actions
147            //modules/pubsub:service_test                                  SKIPPED
148            //modules/air_sensor:air_sensor_test                           PASSED in 30.2s
149            //modules/blinky:blinky_test                                   PASSED in 14.3s
150            //modules/buttons:manager_test                                 PASSED in 41.0s
151            //modules/edge_detector:hysteresis_edge_detector_test          PASSED in 7.3s
152            //modules/lerp:lerp_test                                       PASSED in 26.0s
153            //modules/morse_code:encoder_test                              PASSED in 35.6s
154            //modules/pubsub:pubsub_events_test                            PASSED in 18.2s
155            //modules/pubsub:pubsub_test                                   PASSED in 22.1s
156            //modules/state_manager:state_manager_test                     PASSED in 38.5s
157
158            Executed 9 out of 10 tests: 9 tests pass and 1 was skipped.
159
160      #. Go to the terminal that the test runner is running in and press
161         :kbd:`Control+C` to close it.
162
163.. note::
164
165   The ``//...`` in this command is what makes this a wildcard
166   test. ``//...`` means "run all of the on-device tests defined
167   thoughout the project".
168
169.. _showcase-sense-tutorial-devicetests-flash:
170
171-----------------------------------------
172Flash the blinky app onto your Pico again
173-----------------------------------------
174You're done with the on-device tests. Flash the ``blinky`` app back onto
175your Pico again:
176
177.. tab-set::
178
179   .. tab-item:: VS Code
180      :sync: vsc
181
182      In **Bazel Build Targets** expand **//apps/blinky**, then right-click
183      **:flash (alias)**, then select **Run target**.
184
185   .. tab-item:: CLI
186      :sync: cli
187
188      .. code-block:: console
189
190         $ bazelisk run //apps/blinky:flash
191
192.. _showcase-sense-tutorial-devicetests-summary:
193
194-------
195Summary
196-------
197:ref:`Host tests <showcase-sense-tutorial-hosttests>` are a great way to
198verify that hardware-agnostic application logic is correct. For any logic
199that's intimately connected to hardware, however, on-device tests are the
200way to go.
201
202Next, head over to :ref:`showcase-sense-tutorial-pico-rpc` to
203explore how to communicate with your Pico from your host.
204