Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 27 additions & 76 deletions developer_guides/unit_tests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,86 +6,42 @@ Unit Tests
Prerequisites
*************

You must use `cmocka <https://cmocka.org/>`_ to run unit tests.
This guide assumes that you have proper setup and that you know how to build firmware.
If that's not true please follow :doc:`../getting_started/build-guide/build-from-scratch` first.

Use the package manager such as apt to build native libraries:
`Cmocka <https://cmocka.org/>`_ is fetched and built automatically.
For successfull compilation it needs toolchain thats supports C stdlib.

.. code-block:: bash
Configuring for unit tests
**************************

sudo apt install libcmocka-dev
In order to build and run unit tests, just pass additional flag to
CMake **-DBUILD_UNIT_TESTS=ON**.

If you want to use the `custom version of cmocka <Preparing cmocka package_>`_
(for example for cross-compilation), provide the path to cmocka
built for the chosen architecture using the **--with-cmocka-prefix** option.
Unit tests need valid config for used toolchain, so before building them you can
use default config like:

Enabling unit tests
*******************
.. code-block:: bash

In order to build and run unit tests, provide the path to cmocka using the
**--with-cmocka-prefix** option of the configure script.
make <platform>_defconfig

After the configuration is complete, build and run all unit tests by entering:
Then build and run all unit tests by entering:

.. code-block:: bash

make check

Reconfiguration is not necessary when the cmocka path is set in the
configuration script. Use **make** for building the normal APL Binary
and **make check** to build and run unit tests.
make -j4 && ctest -j8


Example: Running tests for APL
==============================

In order to build tests for the APL platform, use the `custom version of
cmocka <Preparing cmocka package_>`_. Run the **./configure** script
with the same parameters as when building APL binaries. You must also
add **--with-cmocka-prefix=<path to cmocka>**. For example:

.. code-block:: bash

./autogen.sh
./configure --with-arch=xtensa-smp --with-platform=apollolake \
--with-dsp-core=$XTENSA_CORE --with-root-dir=$CONFIG_PATH/xtensa-elf \
--host=xtensa-bxt-elf --with-meu=$MEU_PATH \
--with-key=$PRIVATE_KEY_PATH CC=xt-xcc OBJCOPY=xt-objcopy \
OBJDUMP=xt-objdump --with-cmocka-prefix=/home/admin/cminstall_apl_2017_8/
make check

Preparing cmocka package
************************

#. Build cmocka with the static library on:

.. code-block:: bash

cmake <cmocka src dir> -DWITH_STATIC_LIB=ON

In order to build cmocka with xt-xcc to link with a DSP binary code,
do the following:

a. add another option -DCMAKE_C_COMPILER=xt-xcc
#. edit the cmocka build scripts to disable building of shared library
mkdir build_ut && cd build_ut
cmake -DTOOLCHAIN=xt -DROOT_DIR=$CONFIG_PATH/xtensa-elf -DBUILD_UNIT_TESTS=ON ..
make apollolake_defconfig
make -j4 && ctest -j8

#. Create a directory for the package to be referenced by the main
**sof** build script and copy the required files there:

.. code-block:: bash

mkdir /home/<you>/cminstall
mkdir /home/<you>/cminstall/include
mkdir /home/<you>/cminstall/lib

cp cmocka.h /home/<you>/cminstall/include
cp libcmocka.a /home/<you>/cminstall/lib

#. Use the target location for the cmocka files when invoking the
**configure** script:

.. code-block:: bash

./configure --with-cmocka-prefix=/home/<you>/cminstall ...

Wrapping objects for unit tests
*******************************
Expand Down Expand Up @@ -113,12 +69,14 @@ CFLAGS, however they should be passed in separate variables in the makefile.

Example:

.. code-block:: bash
.. code-block:: cmake

# some tests before ...
check_PROGRAMS += pipeline_connect_upstream
pipeline_connect_upstream_SOURCES = ../../src/audio/pipeline.c src/audio/pipeline/ pipeline_mocks.c src/audio/pipeline/pipeline_connect_upstream.c src/audio/pipeline/pipeline_mocks_rzalloc.c
pipeline_connect_upstream_CFLAGS = -Wl, --wrap=symbol_name
cmocka_test(pipeline_connect_upstream
pipeline_connect_upstream.c
...
)
target_link_libraries(pipeline_connect_upstream PRIVATE "-Wl,--wrap=symbol_name")

Full information about wrapping can be found here:

Expand All @@ -127,16 +85,9 @@ https://lwn.net/Articles/558106/
Notes
*****

#. Use the **make check -j** option while running tests that use xt-run
#. Use the **ctest -j** option while running tests that use xt-run
(to speed up tests significantly) by running multiple instances of the
xt-run simulator (it also speeds up build if you have many unit tests).

#. When you switch platforms, such as from native to APL, use **make
clean**; otherwise, **make** will not build binaries for the new
platform and your tests will fail.

#. To speed up development of new unit tests, run specific tests such as:

.. code-block:: bash

make check check_PROGRAMS="testname1 testname2"
#. **ctest** only runs unit tests, to rebuild them you have to explicitly
run **make**.