简体   繁体   中英

How to integrate googletest to stm32 cmake project

I made my project, which I created with STM32 cubeIDE, compilable with Cmake, but I had problems with googletest integration.

The file structure of the entire project is as follows;

在此处输入图像描述

Project Link

I created the project and test folders as two different cmake projects. I don't have any problems compiling the project, but I could not establish a healthy connection between the test and the project. Because when compiling the test I get an error.

When I add the source and header files related to the Project to the CmakeList.txt in the test, I get an error in the project's variables and type definitions.

The cmake commands I used in the test file directory are as follows;

$cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_TOOLCHAIN_FILE=<path_to_Project/cmake/gcc-arm-none-eabi.cmake> -S<path_to_Test> -B<path_to_Test/build/Debug> -G Ninja

$cmake --build <path_to_Test/build/Debug> -j 8

Errors;

错误

Do you have any advice to integrate googletest into existing cmake project?

Unless you plan to run the unit tests on the target device (MCU), you cannot and should not include any microcontroller-specific code, such as HAL.

Here are the some approaches for unit testing embedded projects:

  • Separate testable code into libraries. This code should not rely on any hardware-specific stuff. Then you can unit-test the library independently.
  • If you need to test code that relies on peripherals, you need to abstract the peripherals and create mocks for use in unit tests. This is quite a lot of work to implement and maintain.

Not every piece of code is meant to be unit tested, do some more research to get a better idea of different test types.

Update

So the goal is to run the tests on the target. In that case, I would recommend choosing a different testing framework, as gtest is probably too big to fit on a MCU, even if you could somehow compile it.

I know someone who used cmocka for this purpose. The flow would be something like this:

  1. Set-up your code to output the test results through, for example, a serial port.
  2. Compile your tests for the MCU.
  3. Upload the test firmware to the MCU.
  4. Run the tests and examine the output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM