Introducing ztest for existing application

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!

  • Hi!

    in CMakeLists.txt try adding:

    FILE(GLOB app_sources src/*.c)
    target_sources(app PRIVATE ${app_sources})
    and add
    #include <your_driver/etc/where_type_is_defined_.h>
  • Hi Sigurd,

    /test/sample_test/CMakeLists.txt is adjusted to

    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)
    
    FILE(GLOB app_sources ../../src/*.c)
    target_sources(app PRIVATE ${app_sources})
    
    # Automatically inherit includes from the root project
    target_include_directories(app PRIVATE ${CMAKE_SOURCE_DIR}/src)
    

    Running

    rm -rf twister-out* && west twister -T test/sample_test/

    now results in

    example_1.c:1:10: fatal error: example_1.h: No such file or directory
        1 | #include "example_1.h"
          |          ^~~~~~~~~~~~~~~~~
          
    example_2.c:1:10: fatal error: example_2.h: No such file or directory
        1 | #include "example_2.h"
          |          ^~~~~~~~~~~~~~~~~
          
    main.c:1:10: fatal error: example_3.h: No such file or directory
        1 | #include "example_3.h"
          |          ^~~~~~~~~~~~~~~~~
          
          
    etc

    So inside the application .c files it complains about not finding application .h files. But of course they are there as the application builds.

    Where to put the

    #include <your_driver/etc/where_type_is_defined_.h>

    If I do /test/sample_test/CMakeLists.txt:

    cmake_minimum_required(VERSION 3.20.0)
    find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
    project(sample_test)
    
    FILE(GLOB app_sources ../../src/*.c)
    target_sources(app PRIVATE ${app_sources})
    
    # Add additional include directories
    target_include_directories(app PRIVATE
        ${CMAKE_SOURCE_DIR}/../../src
        ${CMAKE_SOURCE_DIR}/../../inc
        ${CMAKE_SOURCE_DIR}/../../inc/ble_driver
        ${CMAKE_SOURCE_DIR}/../../inc/uart_driver
        ${CMAKE_SOURCE_DIR}/../../modules/nanopb
    
    )
    
    # Link necessary libraries
    target_link_libraries(app PRIVATE ztest)
    

     I get errors like

    test/sample_test/../../inc/uart_driver/uart_driver.h:8:23: error: ‘CONFIG_BT_NUS_UART_BUFFER_SIZE’ undeclared here (not in a function)
        8 | #define UART_BUF_SIZE CONFIG_BT_NUS_UART_BUFFER_SIZE

    This define is generated by the application build and is based on KConfig. It resides in

    build/app-poc/zephyr/include/generated/zephyr/

    I do not want to manually specify all possible .h files that are generated or in the SDK. I want the ztest system to inherit all. Is this possible?

Related