如何在zephyr-rtos项目中添加应用程序单元测试

问题描述

如何使用Zephyr RTOS启动新应用程序,包括应用程序源的单元测试。我已经看到Zephyr提供了一个测试框架z-test和一个测试运行器健全性检查。

我的目标是使用以下目录结构将生产代码与测试代码分开:

PROJECT_ROOT
|-src
|-test

src文件夹应包含应用程序源。 test文件夹应包含应用程序源的单元测试,可能带有多个单独的二进制文件。

如何用Zephyr提供的框架(west,健全性检查)实现这一目标?测试应用程序如何知道在哪里寻找应用程序源?

解决方法

也许这不是正确的方法,但我是这样解决的:

  1. 像您一样创建目录结构:
PROJECT_ROOT
|-src
|-test
  1. 将带有测试代码的源文件添加到 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);
}
  1. 测试时,将以下行添加到 prj.conf:
CONFIG_ZTEST=y
  1. 使您的 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)

...
  1. 构建您的代码。
  2. 连接您的设备并在终端中查看测试结果。例如。使用 minicom:
$ minicom -b 115200 -D /dev/ttyACM0

您可以在此处找到有关 Zephyr 测试的文档: https://docs.zephyrproject.org/latest/guides/test/index.html?highlight=test

此外。如果你想使用测试运行器。他们将名称从“sanitycheck”更改为“twister”(Zephyr 2.5 版)。您可以在同一个脚本文件夹中找到它。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...