Maybe it's not the correct way to do it, but I solved it as follows:
- Create a directory structure as you did:
PROJECT_ROOT
|-src
|-test
- Add a source file with the test code to test/ directory.
main.cpp:
#include <ztest.h>
/* Make private functions public for access.
I think this is not a clean way to do it, but for my purposes it was just fine. */
#define private public
#include "../src/Classyouwanttest.h"
void test_function(void)
{
Classyouwanttest classyouwanttest;
/* your zassert instructions here: */
zassert_equal(classyouwanttest.funcXY(...) , ..., ...);
}
void test_main(void)
{
ztest_test_suite(common,
ztest_unit_test(test_function)
);
ztest_run_test_suite(common);
}
- When testing, add following line to prj.conf:
CONFIG_ZTEST=y
- Make your CMakeLists.txt switchable between production and testing code:
...
option(TESTS "Run tests in test/" ON)
# option(TESTS "Run tests in test/" OFF)
if(TESTS)
target_sources(app PRIVATE test/main.cpp)
# add here all class files for productive code
target_sources(app PRIVATE ...
else()
target_sources(app PRIVATE src/main.cpp)
# add here all class files for productive code
target_sources(app PRIVATE ...
endif()
unset(TESTS CACHE)
...
- Build your code.
- Connect your device and see the test result in a terminal. E.g. with minicom:
$ minicom -b 115200 -D /dev/ttyACM0
Documentation about Zephyr Testing you find here:
https://docs.zephyrproject.org/latest/guides/test/index.html?highlight=test
Besides. If you want to use the test runner. They changed the name from "sanitycheck" to "twister" (Zephyr version 2.5). You find it in the same scripts folder.