Reputation: 55
How would like to start a new application using Zephyr RTOS, including unit-tests for the application sources. I have seen that Zephyr provides a test-framework z-test and a test runner sanitycheck.
My goal is to separate production code from the testing code using the following directory structure:
PROJECT_ROOT
|-src
|-test
The src
folder shall contain the application sources. The test
folder shall contain the unit tests for the application sources, potentially with multiple separate binaries.
How can I achieve this with the framework (west, sanitycheck) provided by Zephyr? How does the test application know where to look for the application sources?
Upvotes: 2
Views: 3917
Reputation: 457
Maybe it's not the correct way to do it, but I solved it as follows:
PROJECT_ROOT
|-src
|-test
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);
}
CONFIG_ZTEST=y
...
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)
...
$ 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.
Upvotes: 2