|
| 1 | +# Unit testing |
| 2 | + |
| 3 | +Since the project is huge, the focus will be only on couple of components. |
| 4 | + |
| 5 | +## Zephyr's twister tool |
| 6 | + |
| 7 | +Twister is a useful tool provided by Zephyr for easier testing and coverage reports generation. Documentation regarding Twister can be found [here](https://docs.zephyrproject.org/3.1.0/develop/test/twister.html). |
| 8 | + |
| 9 | +This directory contains folders containins .html coverage reports in lcov format for a couple of different test runs. In order to get output binaries and logs, twister has to be run again. These files can be large, so they are excluded from the project's repo. |
| 10 | + |
| 11 | +Commands that were used for generation of those reports are given below: |
| 12 | + |
| 13 | +- Execute unit tests related to math_extras and store reports at `/test_coverage/unit_run_math_extras` and `/test_coverage/unit_run_math_extras_builtin`. In order to run these tests, a `CMakeLists.txt` had to be modified before running the commands. There was also a naming bug in the `portable.c` file: |
| 14 | + |
| 15 | +``` |
| 16 | +// portable.c |
| 17 | +
|
| 18 | +... |
| 19 | +
|
| 20 | +#define VNAME(N) test_portable_##N |
| 21 | +// Changed to |
| 22 | +#define VNAME(N) run_portable_##N |
| 23 | +
|
| 24 | +... |
| 25 | +
|
| 26 | +ZTEST(math_extras_portable, test_portable_u32_add) { |
| 27 | + run_portable_u32_add(); |
| 28 | +} |
| 29 | +
|
| 30 | +... |
| 31 | +
|
| 32 | +``` |
| 33 | + |
| 34 | +Twister command for running the tests: |
| 35 | + |
| 36 | +`west twister --coverage -p unit_testing -T tests/unit/math_extras/ -v --report-dir ~/2023_Analysis_zephyr/twister_reports/test_coverage/unit_run_math_extras --outdir ~/2023_Analysis_zephyr/twister_reports/test_coverage/math_extras/unit_run_math_extras` |
| 37 | + |
| 38 | +*NOTE*: As mentioned, `CMakeLists.txt` was modified between two runs. This was done to test math_extras functions with and without builtin support. |
| 39 | + |
| 40 | +`CMakeLists.txt` **with** builtin support: |
| 41 | + |
| 42 | +``` |
| 43 | +# SPDX-License-Identifier: Apache-2.0 |
| 44 | +
|
| 45 | +cmake_minimum_required(VERSION 3.20.0) |
| 46 | +
|
| 47 | +project(math_extras) |
| 48 | +find_package(Zephyr COMPONENTS unittest REQUIRED HINTS $ENV{ZEPHYR_BASE}) |
| 49 | +target_sources(testbinary PRIVATE main.c) |
| 50 | +``` |
| 51 | + |
| 52 | +`CMakeLists.txt` **without** builtin support: |
| 53 | + |
| 54 | +``` |
| 55 | +# SPDX-License-Identifier: Apache-2.0 |
| 56 | +
|
| 57 | +cmake_minimum_required(VERSION 3.20.0) |
| 58 | +
|
| 59 | +project(math_extras) |
| 60 | +find_package(Zephyr COMPONENTS unittest REQUIRED HINTS $ENV{ZEPHYR_BASE}) |
| 61 | +target_sources(testbinary PRIVATE portable.c) |
| 62 | +``` |
| 63 | + |
| 64 | +The only difference is the target source file. |
| 65 | + |
| 66 | +### Ztest framework |
| 67 | + |
| 68 | +In the section above, tests were run without any modification or explanation. Zephyr project has developed a testing framework, `Ztest` that alows writing and executing various tests. Comprehensive documentation for Ztest is given [here](https://docs.zephyrproject.org/3.1.0/develop/test/ztest.html). Source code for math_extras tests is in `tests/unit/math_extras/main.c`, `tests/unit/math_extras/portable.c` and `tests/unit/math_extras/tests.inc`. |
| 69 | + |
| 70 | +`main.c` and `portable.c` contain the main **test** logic. In other words, test cases are defined in those files. Helper functions that call functions under test are defined in `tests.inc` in the form of `run_<function_name>` / `run_portable_<function_name>` based on the macro `VNAME(x)` defined in both `main.c` and `portable.c`. Twister will build and execute the tests based on the source files listed in `CMakeLists.txt` (`target_sources` line). |
| 71 | + |
| 72 | +Each test case is defined using `ZTEST` macro with arguments defining test suite name and test case name. These will be visible during test run. |
| 73 | + |
| 74 | +For example, test case defined as: |
| 75 | +``` |
| 76 | +ZTEST(math_extras, test_u32_add) { |
| 77 | + run_u32_add(); |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +means that Twister will call `math_extras.test_u32_add` test case during execution. The body of the `ZTEST`-defined test case contains all of the test logic (setting up the test, executing helper functions, functions under test, deinitialization etc.) |
| 82 | + |
| 83 | +### lcov report |
| 84 | + |
| 85 | +lcov is a tool that creates graphical representation of the code coverage in .html format. Reports for math_extras functions are given below: |
| 86 | + |
| 87 | +. |
| 88 | + |
| 89 | + |
| 90 | +As the reports show, there is a 100% coverage of the `math_extras` functions both with builtin functions and with portable implementation. |
0 commit comments