Skip to content

Commit 69548c6

Browse files
stemschmidtcfriedt
authored andcommitted
documentation: unit tests: extend description
Add a more detailed description on how to unit test using ztest and twister Signed-off-by: Stefan Schmidt <[email protected]>
1 parent 6693923 commit 69548c6

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

doc/develop/test/ztest.rst

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,16 @@ efforts into the specific module in question. This will speed up testing since
360360
only the module will have to be compiled in, and the tested functions will be
361361
called directly.
362362

363-
Examples of unit tests can be found in the :zephyr_file:`tests/unit/` folder.
363+
To setup unit tests you have to add a CMakeLists.txt, a testcases.yml and a
364+
prj.conf to the directory containing the unit test source files. The resulting
365+
binary from this directory is build with the -DBOARD=unit_testing. When twister
366+
is invoked the script zephyr/scripts/pylib/twister/twisterlib/testplan.py
367+
filters out all testcases.yml in which type: unit is not set. Only unit tests
368+
are executed with a firmware build with BOARD=unit_testing.
369+
370+
CMakeLists.txt
371+
==============
372+
364373
In order to declare the unit tests present in a source folder, you need to add
365374
the relevant source files to the ``testbinary`` target from the CMake
366375
:zephyr_file:`unittest <cmake/modules/unittest.cmake>` component. See a minimal
@@ -383,6 +392,41 @@ and are used to decide whether a test failed or passed by verifying whether an
383392
interaction with an object occurred, and if required, to assert the order of
384393
that interaction.
385394

395+
testcases.yaml
396+
==============
397+
398+
You have to set the value for the key "type" to "unit" in the testcase.yaml
399+
400+
.. code-block:: yaml
401+
402+
tests:
403+
testscenario.testsuite:
404+
tags: your_tag
405+
type: unit
406+
407+
prj.conf
408+
========
409+
410+
For unit tests this contains usually only
411+
412+
.. code-block:: kconfig
413+
414+
CONFIG_ZTEST=y
415+
416+
If your unit tests require additional libraries (e.g. math-lib) you will have to
417+
add them either via the CMakeLists.txt or in the testcase.yaml:
418+
419+
.. code-block:: yaml
420+
421+
tests:
422+
testscenario.testsuite:
423+
tags: your_tag
424+
type: unit
425+
extra_args:
426+
- EXTRA_LDFLAGS="-lm"
427+
428+
Examples of unit tests can be found in the :zephyr_file:`tests/unit/` folder.
429+
386430
Best practices for declaring the test suite
387431
*******************************************
388432

0 commit comments

Comments
 (0)