2

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?

LaMontagne
  • 55
  • 4

1 Answers1

2

Maybe it's not the correct way to do it, but I solved it as follows:

  1. Create a directory structure as you did:
PROJECT_ROOT
|-src
|-test
  1. 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);
}
  1. When testing, add following line to prj.conf:
CONFIG_ZTEST=y
  1. 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)

...
  1. Build your code.
  2. 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.

El tornillo
  • 437
  • 3
  • 16