Using Cpputest with ESP-IDF

gotramaval
Posts: 1
Joined: Sun Feb 13, 2022 7:52 pm

Using Cpputest with ESP-IDF

Postby gotramaval » Sun Feb 13, 2022 8:25 pm

Hello,

I'm trying to set a testing rig for my application using Cpputest and I'd need some assistance for its integration with IDF build system. I'm aware that ESP-IDF comes with Unity test framework integrated, but as far as I can see, is focused for in target tests, while I want to run the tests on my computer.

What previously worked for me on other non IDF projects, was to create two different targets, and depending of the target, use either the compiler for the target architecture or a compiler for the desktop PC where the tests will run. (see for example CMake files at https://github.com/raulgotor/ams_as5600 ).

But on IDF, I run on several issues when using the approach above:
- If I write the CMake file to completely exclude the IDF build system when doing the tests build, (for example, wrapping all the idf related calls such as include($ENV{IDF_PATH}/tools/cmake/project.cmake), idf_component_register, etc... in if target=production / else if target=tests blocks, I'll run on issues as my testing code and production code are depending on some IDF components that already have CMakeLists files based on IDF build system, so this doesn't seems a way to go.

- My second approach was to try to go with the IDF build system, but having an additional test app target. For instance, I expanded the root level CMakeLists to the following:

Code: Select all

cmake_minimum_required(VERSION 3.5)
set(CMAKE_C_STANDARD 99)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
set(ENV{PATH} "$ENV{XPATH}:$ENV{PATH}")

set(PRODUCTION_DIR ${CMAKE_CURRENT_SOURCE_DIR}/main)

SET(TARGET_GROUP test)

message("Building ${TARGET_GROUP}")
if(TARGET_GROUP STREQUAL production)

    message("Building Production")

elseif(TARGET_GROUP STREQUAL test)

    set(TESTS_DIR ${CMAKE_SOURCE_DIR}/tests)
    add_subdirectory(${TESTS_DIR})
    add_subdirectory(${PRODUCTION_DIR})

else()

    message(FATAL_ERROR "I don't know the TARGET_GROUP you gave me!")

endif()

project(reflow_oven_controller LANGUAGES C CXX)
But then I run into some issues:
-First, I need to compile the test app with a compiler for my desktop PC, such as gcc, clang, etc. Would this be possible under IDF build system?

-Second, my tests relay on PRODUCTION_DIR. When I add this subdirectory with add_subdirectory(${PRODUCTION_DIR}), CMake complains with the following error:

Code: Select all

CMake Error at /Users/raul.gotor/esp/esp-idf/tools/cmake/component.cmake:431 (message):
  Called idf_component_register from a non-component directory.
For reference, the project is organised as follows:

Code: Select all

- CMakeLists.txt
- components/
- main/
...- CMakeLists.txt
...- source files
- tests/
...- CMakeLists.txt
...- test_source files
...- mocks/
and the CMakeLists file at /tests is the following:

Code: Select all

project(tests_main)

set(SRC_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR})
set(MOCKS_DIR ${TESTS_DIR}/mocks)

message("Tests source dirs: " ${SRC_DIRECTORIES})
message("Mocks dirs:        " ${MOCKS_DIR})

if(DEFINED ENV{CPPUTEST_HOME})
    message(STATUS "Using CppUTest home: $ENV{CPPUTEST_HOME}")
    set(CPPUTEST_INCLUDE_DIRS $ENV{CPPUTEST_HOME}/include)
    set(CPPUTEST_LIBRARIES $ENV{CPPUTEST_HOME}/lib)
    set(CPPUTEST_LDFLAGS CppUTest CppUTestExt)
else()
    find_package(PkgConfig REQUIRED)
    #find_package(CPPUTEST REQUIRED cpputest>=3.8)
    pkg_search_module(CPPUTEST REQUIRED cpputest>=3.8)
    message(STATUS "Found CppUTest version ${CPPUTEST_VERSION}")
endif()

add_subdirectory(${MOCKS_DIR})

file(GLOB SOURCES
        "${SRC_DIRECTORIES}/*.h"
        "${SRC_DIRECTORIES}/*.cpp"
        "${SRC_DIRECTORIES}/*.c"
        "${MOCKS_DIR}"
        "${PRODUCTION_DIR}"
        )

include_directories(
        ${CPPUTEST_INCLUDE_DIRS}
        ${MOCKS_DIR}
        ${PRODUCTION_DIR}
)

link_directories(${CPPUTEST_LIBRARIES})

add_executable(reflow_oven_controller_tests heater_tests.cpp ${SOURCES})

target_link_libraries(reflow_oven_controller_tests
        main
        mocks
        ${CPPUTEST_LDFLAGS}
        )
     
How would you organise the CMake files in order to be able to compile my test app using IDF build system?

Thanks in advance

kammutierspule
Posts: 6
Joined: Sat Apr 11, 2020 9:25 am

Re: Using Cpputest with ESP-IDF

Postby kammutierspule » Sat Sep 28, 2024 1:52 pm

How did you implemented?

Two years later, still not much info on internet about it..

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot], MicroController and 63 guests