|
3 | 3 | Unit Tests with Ztest |
4 | 4 | ###################### |
5 | 5 |
|
| 6 | +Overview |
| 7 | +******** |
| 8 | + |
| 9 | +The SOF project is transitioning from CMocka to Zephyr's native ztest framework for unit testing. This migration aligns SOF's testing infrastructure with the Zephyr RTOS ecosystem and provides better integration with Twister test runner. |
| 10 | + |
| 11 | +The migration from CMocka to ztest is ongoing. Currently, basic unit tests (math functions, library functions) have been migrated, with more complex audio component tests in progress. For the latest migration status, see `GitHub issue #10110 <https://github.com/thesofproject/sof/issues/10110>`_. |
| 12 | + |
| 13 | +For more information about the ztest framework, refer to `Zephyr's ztest documentation <https://docs.zephyrproject.org/latest/develop/test/ztest.html>`_. |
| 14 | + |
| 15 | +For legacy CMocka-based unit tests, refer to :doc:`unit_tests`. |
| 16 | + |
| 17 | +Prerequisites |
| 18 | +************* |
| 19 | + |
| 20 | +This guide assumes that you have a basic SOF development environment set up. If not, follow the instructions at :doc:`../getting_started/build-guide/build-with-zephyr` first. |
| 21 | + |
| 22 | +Required Tools |
| 23 | +============== |
| 24 | + |
| 25 | +Ztest unit tests require the following tools and dependencies: |
| 26 | + |
| 27 | +.. code-block:: bash |
| 28 | +
|
| 29 | + sudo apt-get update |
| 30 | + sudo apt-get install -y clang llvm ninja-build device-tree-compiler \ |
| 31 | + python3-pyelftools gcc-multilib g++-multilib |
| 32 | +
|
| 33 | +West Meta-Tool |
| 34 | +============== |
| 35 | + |
| 36 | +Zephyr uses the ``west`` meta-tool for project management. Install it using pip: |
| 37 | + |
| 38 | +.. code-block:: bash |
| 39 | +
|
| 40 | + pip3 install west |
| 41 | +
|
| 42 | +Python Dependencies |
| 43 | +=================== |
| 44 | + |
| 45 | +Install Zephyr's Python requirements: |
| 46 | + |
| 47 | +.. code-block:: bash |
| 48 | +
|
| 49 | + cd path/to/zephyrproject/zephyr |
| 50 | + pip3 install -r scripts/requirements.txt |
| 51 | +
|
| 52 | +Building and Running Tests |
| 53 | +*************************** |
| 54 | + |
| 55 | +Setup Workspace |
| 56 | +=============== |
| 57 | + |
| 58 | +First, initialize your workspace with west if you haven't already: |
| 59 | + |
| 60 | +.. code-block:: bash |
| 61 | +
|
| 62 | + cd path/to/sof |
| 63 | + west init -l |
| 64 | + west update --narrow --fetch-opt=--filter=tree:0 |
| 65 | +
|
| 66 | +Set Toolchain |
| 67 | +============= |
| 68 | + |
| 69 | +Ztest unit tests use the LLVM/Clang toolchain: |
| 70 | + |
| 71 | +.. code-block:: bash |
| 72 | +
|
| 73 | + export ZEPHYR_TOOLCHAIN_VARIANT=llvm |
| 74 | +
|
| 75 | +Running Unit Tests |
| 76 | +================== |
| 77 | + |
| 78 | +To build and run all unit tests using Twister: |
| 79 | + |
| 80 | +.. code-block:: bash |
| 81 | +
|
| 82 | + west twister --testsuite-root sof/test/ztest/unit/ --platform native_sim \ |
| 83 | + --verbose --inline-logs |
| 84 | +
|
| 85 | +Twister provides detailed test results in the console output. Test results are also saved in ``twister-out/`` directory. |
| 86 | + |
| 87 | +Writing a Unit Test |
| 88 | +******************* |
| 89 | + |
| 90 | +*This section is in progress and will be added in a future update.* |
| 91 | + |
| 92 | +Test Architecture |
| 93 | +***************** |
| 94 | + |
| 95 | +*This section is in progress and will be added in a future update.* |
| 96 | + |
| 97 | +Notes |
| 98 | +***** |
| 99 | + |
| 100 | +*This section is in progress and will be added in a future update.* |
| 101 | + |
0 commit comments