1.. _showcase-sense-tutorial-devicetests: 2 3====================== 48. Run on-device tests 5====================== 6.. _mock or stub: https://stackoverflow.com/a/17810004 7 8:ref:`Host tests <showcase-sense-tutorial-hosttests>` are the best way to 9test hardware-independent logic in your codebase because they are much easier to 10scale. Eventually, however, you need to tests parts of your codebase that interact 11with hardware. Sometimes you can `mock or stub`_ the hardware logic and still run 12the tests host-side. Other times you really do need to run the tests on-device. 13Pigweed provides robust and automated solutions for running hardware-in-the-loop tests. 14 15.. note:: 16 17 This step requires the :ref:`full hardware 18 setup <showcase-sense-tutorial-hardware>` and must be 19 run over a terminal. 20 21.. note:: 22 23 This section requires the VS Code terminal because there's currently 24 no way to run wildcard tests from the **Bazel Targets** UI. 25 Wildcard tests will be explained more in a moment. 26 27.. _showcase-sense-tutorial-devicetests-setup: 28 29-------------------- 30Set up your hardware 31-------------------- 32#. Set up your hardware to match the :ref:`full setup <showcase-sense-tutorial-hardware>`. 33 On-device tests only work with the full setup. You can skip ahead to 34 :ref:`showcase-sense-tutorial-pico-rpc` if you don't have the full setup 35 hardware. 36 37.. _showcase-sense-tutorial-devicetests-run: 38 39------------------- 40Run on-device tests 41------------------- 42.. _Command Palette: https://code.visualstudio.com/docs/getstarted/userinterface#_command-palette 43.. _terminal: https://code.visualstudio.com/docs/terminal/basics 44 45#. Open a terminal: 46 47 .. tab-set:: 48 49 .. tab-item:: VS Code 50 :sync: vsc 51 52 #. Open the `Command Palette`_. 53 54 #. Run the ``Pigweed: Set Bazelisk Path`` command and then select 55 **Use the version built in to the Pigweed extension** option. 56 57 #. Open the Command Palette again and run the 58 ``Pigweed: Activate Bazelisk In Terminal`` command. 59 60 You should see a terminal open like this: 61 62 .. code-block:: text 63 64 export PATH="/home/kayce/.vscode/extensions/pigweed.pigweed-1.3.3/node_modules/@bazel/bazelisk:${PATH}" 65 kayce@kayce0:~/tmp/sense$ export PATH="/home/kayce/.vscode/extensions/pigweed.pigweed-1.3.3/node_modules/@bazel/bazelisk:${PATH}" 66 kayce@kayce0:~/tmp/sense$ 67 68 .. tab-item:: CLI 69 :sync: cli 70 71 Just follow your normal workflow for opening a terminal window or tab. 72 73#. Start a test runner. 74 75 .. tab-set:: 76 77 .. tab-item:: Pico 1 & 1W (RP2040) 78 :sync: rp2040 79 80 .. code-block:: console 81 82 bazelisk run \ 83 @pigweed//targets/rp2040/py:unit_test_server \ 84 -- --debug-probe-only --chip RP2040 85 86 .. tab-item:: Pico 2 & 2W (RP2350) 87 :sync: rp2040 88 89 .. code-block:: console 90 91 bazelisk run \ 92 @pigweed//targets/rp2040/py:unit_test_server \ 93 -- --debug-probe-only --chip RP2350 94 95 You should see output like this: 96 97 .. code-block:: text 98 99 INFO: Analyzed target @@pigweed~//targets/rp2040/py:unit_test_server (134 packages loaded, 13872 targets configured). 100 INFO: Found 1 target... 101 Target @@pigweed~//targets/rp2040/py:unit_test_server up-to-date: 102 bazel-bin/external/pigweed~/targets/rp2040/py/unit_test_server 103 INFO: Elapsed time: 32.497s, Critical Path: 18.71s 104 INFO: 177 processes: 12 internal, 165 linux-sandbox. 105 INFO: Build completed successfully, 177 total actions 106 INFO: Running command line: bazel-bin/external/pigweed~/targets/rp2040/py/unit_test_server <args omitted> 107 20240806 18:22:29 OUT [370633] 2024/08/06 18:22:29 Parsed server configuration from /tmp/tmparhr7i8o 108 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] 109 20240806 18:22:29 OUT [370633] 2024/08/06 18:22:29 Starting gRPC server on [::]:34172 110 20240806 18:22:29 OUT [370633] [ServerWorkerPool] 2024/08/06 18:22:29 Starting 1 workers 111 20240806 18:22:29 OUT [370633] [ExecDeviceRunner 0] 2024/08/06 18:22:29 Starting worker 112 113 The test runner essentially orchestrates how all the unit 114 tests should be run. 115 116 Leave this server running and proceed to the next step. 117 118#. Open another terminal tab and run the tests. 119 120 .. tab-set:: 121 122 .. tab-item:: Pico 1 & 1W (RP2040) 123 :sync: rp2040 124 125 .. code-block:: console 126 127 bazelisk test --config=rp2040 //... 128 129 .. tab-item:: Pico 2 & 2W (RP2350) 130 :sync: rp2040 131 132 .. code-block:: console 133 134 bazelisk test --config=rp2350 //... 135 136 .. code-block:: text 137 138 # ... 139 INFO: Found 134 targets and 10 test targets... 140 INFO: Elapsed time: 131.231s, Critical Path: 60.93s 141 INFO: 2368 processes: 423 internal, 1945 linux-sandbox. 142 INFO: Build completed successfully, 2368 total actions 143 //modules/pubsub:service_test SKIPPED 144 //modules/air_sensor:air_sensor_test PASSED in 30.2s 145 //modules/blinky:blinky_test PASSED in 14.3s 146 //modules/buttons:manager_test PASSED in 41.0s 147 //modules/edge_detector:hysteresis_edge_detector_test PASSED in 7.3s 148 //modules/lerp:lerp_test PASSED in 26.0s 149 //modules/morse_code:encoder_test PASSED in 35.6s 150 //modules/pubsub:pubsub_events_test PASSED in 18.2s 151 //modules/pubsub:pubsub_test PASSED in 22.1s 152 //modules/state_manager:state_manager_test PASSED in 38.5s 153 154 Executed 9 out of 10 tests: 9 tests pass and 1 was skipped. 155 156 .. note:: 157 158 The ``//...`` in this command is what makes this a wildcard 159 test. ``//...`` means "run all tests defined thoughout the project". 160 The ``--config`` option specifies what hardware the tests will 161 run on. 162 163#. Go to the terminal that the test runner is running in and press 164 :kbd:`Control+C` to close it. 165 166.. _showcase-sense-tutorial-devicetests-flash: 167 168----------------------------------------- 169Flash the blinky app onto your Pico again 170----------------------------------------- 171You're done with the on-device tests. Flash the ``blinky`` app back onto 172your Pico again: 173 174.. tab-set:: 175 176 .. tab-item:: VS Code 177 :sync: vsc 178 179 .. tab-set:: 180 181 .. tab-item:: Pico 1 & 1W (RP2040) 182 :sync: rp2040 183 184 In **Bazel Targets** expand **//apps/blinky**, then right-click 185 **:flash_rp2040 (native binary)**, then select **Run target**. 186 187 .. tab-item:: Pico 2 & 2W (RP2350) 188 :sync: rp2350 189 190 In **Bazel Targets** expand **//apps/blinky**, then right-click 191 **:flash_rp2350 (native binary)**, then select **Run target**. 192 193 .. tab-item:: CLI 194 :sync: cli 195 196 .. tab-set:: 197 198 .. tab-item:: Pico 1 & 1W (RP2040) 199 :sync: rp2040 200 201 .. code-block:: console 202 203 bazelisk run //apps/blinky:flash_rp2040 204 205 .. tab-item:: Pico 2 & 2W (RP2350) 206 :sync: rp2350 207 208 .. code-block:: console 209 210 bazelisk run //apps/blinky:flash_rp2350 211 212.. _showcase-sense-tutorial-devicetests-summary: 213 214------- 215Summary 216------- 217:ref:`Host tests <showcase-sense-tutorial-hosttests>` are a great way to 218verify that hardware-agnostic application logic is correct. For any logic 219that's intimately connected to hardware, however, on-device tests are 220necessary. Pigweed provides robust support for extensive automation of 221hardware-in-the-loop tests. 222 223Next, head over to :ref:`showcase-sense-tutorial-pico-rpc` to 224explore how to communicate with your Pico from your host. 225