也许这不是正确的方法,但我解决了如下:
- 像你一样创建一个目录结构:
PROJECT_ROOT
|-src
|-test
- 将带有测试代码的源文件添加到 test/ 目录。
主.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);
}
- 测试时,在 prj.conf 中添加以下行:
CONFIG_ZTEST=y
- 让您的 CMakeLists.txt 在生产和测试代码之间切换:
...
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:
$ minicom -b 115200 -D /dev/ttyACM0
您可以在此处找到有关 Zephyr 测试的文档:
https ://docs.zephyrproject.org/latest/guides/test/index.html?highlight=test
除了。如果你想使用测试运行器。他们将名称从“sanitycheck”更改为“twister”(Zephyr 版本 2.5)。您可以在同一个脚本文件夹中找到它。