Hi guys,
we are using the nRF52833 and have a running application. Now I want to introduce unit tests and stumbled upon ztest. The idea is to add a folder where all the test are in one place. Started with this tree in the root project:
test
├── CMakeLists.txt
├── README.md
└── sample_test
├── CMakeLists.txt
├── prj.conf
├── testcase.yaml
└── test_sample.c
test/CMakeLists.txt:
add_subdirectory(sample_test)
test/sample_test/CMakeLists.txt:
cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(sample_test) # Use the existing application source tree target_sources(app PRIVATE test_sample.c) # Automatically inherit includes from the root project target_include_directories(app PRIVATE ${CMAKE_SOURCE_DIR}/src)
test/sample_test/prj.conf:
CONFIG_ZTEST=y CONFIG_ZTEST_ASSERT_VERBOSE=2
test/sample_test/testcase.yaml:
tests: sample_test.my_suite: tags: unit platform_allow: native_posix extra_args: "APP_DIR=../.."
test/sample_test/test_sample.c:
#include <zephyr/ztest.h> ZTEST(sample_group, test_missing_information) { type_defined_somewhere_in_application mode = function_defined_somewhere_in_application(); zassert_equal(mode, type_defined_somewhere_in_application, "Test failed for missing information!"); } ZTEST_SUITE(sample_group, NULL, NULL, NULL, NULL, NULL);
I would like to call functions defined somewhere in the app and test them here without having to provide all inc/src/etc in e.g. the CMakeLists.txt in the tests as this is already done in the root.
Currently if I run
rm -rf twister-out* && west twister -T test/sample_test/
I get errors like
test/sample_test/test_sample.c:9:8: error: unknown type name ‘type_defined_somewhere_in_application’
Is my assumption correct? Can I inherit the applications build information and "only" add the unit tests without having to provide this info again?
Thanks in advance!