Running unit tests
Both Zephyr and the nRF Connect SDK support running unit tests using the following methods:
Twister - A test runner tool that is part of Zephyr, used for automating the execution of test cases. It can be used for both continuous integration and local testing, and it supports running tests on multiple platforms, including actual hardware devices, emulated platforms, and simulated environments. It can also be used to generate code coverage reports.
West (Zephyr’s meta-tool) - Which lets you build and run one specific unit test. This feature of west uses Twister under the hood.
In either case, to run the unit test, you must navigate to the test directory that includes the testcase.yaml
file.
This file includes information about available tests, their parameters, hardware requirements or dependencies, platform subsets, among others.
For more information about these files, read the Test Cases section in Zephyr’s documentation about Twister.
Because Twister is part of Zephyr, you need to provide the <Zephyr_path>
to Zephyr SDK repository that was included in your nRF Connect SDK installation (in the step Get the nRF Connect SDK code).
To generate the test project and run the unit tests, locate the directory with the testcase.yaml
file and run the following command:
<Zephyr_path>/scripts/twister -T .
This command will generate the test project and run it for Native simulator - native_sim and qemu_cortex_m3
boards.
If you want to specify a board target, use the -p
parameter and specify the board_target.
For example, to run the unit test on qemu_cortex_m3
, use the following command:
<Zephyr_path>/scripts/twister -T . -p qemu_cortex_m3
<Zephyr_path>/scripts/twister -T .
This command will generate the test project and run it for Native simulator - native_sim and qemu_cortex_m3
boards.
If you want to specify a board target, use the -p
parameter and specify the board_target.
For example, to run the unit test on qemu_cortex_m3
, use the following command:
<Zephyr_path>/scripts/twister -T . -p qemu_cortex_m3
west build -b board_target -t run
The -t run
parameter tells west to run the default test target.
For example, to run the unit test on Native simulator - native_sim board, use the following command:
west build -b native_sim -t run
Example: Running the unit tests on the nRF9160 DK
The Asset Tracker v2 application provides Unit tests for several of its modules. To run the unit test for the Debug module, complete the following steps:
Connect the kit to the computer using a USB cable. The kit is assigned a COM port (Windows) or ttyACM device (Linux), which is visible in the Device Manager. Take note of the serial port where you receive logs from the DK (this will be
serial_port
in the following command).Navigate to
asset_tracker_v2/tests/debug_module
, where thetestcase.yaml
is located. If you check this file, it includesnrf9160dk/nrf9160/ns
in theplatform_allow:
entry.Enter the following command to execute the unit tests on nRF9160 DK:
<Zephyr_path>/scripts/twister -T . -p nrf9160dk/nrf9160/ns --device-testing --device-serial <serial_port>
<Zephyr_path>/scripts/twister -T . -p nrf9160dk/nrf9160/ns --device-testing --device-serial <serial_port>
west build -b nrf9160dk/nrf9160/ns -t run