DEV Community

Cover image for CMake on SMT32 | Episode 7: unit tests

CMake on SMT32 | Episode 7: unit tests

Pierre Gradot on February 15, 2024

It's been almost four years, and it's about time to revive this series on CMake and STM32 (or MCUs in general, STM32 just serves as a tangible exam...
Collapse
 
baduit profile image
Lena

being able to compile a significant part of the project for another target demonstrates that the abstraction from the hardware layers are well-designed

It reminds me of a project where it was not the case at all for almost the whole codebase, it was soooo much fun (no)

Collapse
 
pgradot profile image
Pierre Gradot • Edited

Strangely enough, many people in the embedded world believe simulation is useless or even counterproductive. Sometimes, they don't even know this is possible.

Collapse
 
danielsalyi profile image
Dániel Sályi • Edited

Can you elaborate how we get feedback whether the tests have passed or failed from the MCU?

Collapse
 
danielsalyi profile image
Dániel Sályi

also, how to use ctests?

Collapse
 
pgradot profile image
Pierre Gradot

About embedded tests

It's not very easy to elaborate this topic without jumping into a particular case.

For my experience, I can tell that it is possible to use Catch2 (at least the 2.x branch) on a STM32 MCU. You need to have printf() working (probably through a UART connected to your computer with a USB-UART cable). You need to compile a program that is small enough to fit in the ROM of the MCU and provide the compiler with the necessary functions it needs to link the program. When you power-on the board, the tests will run and the output on the UART will be similar to the output you could get while running the tests directly on your computer.

About ctest

The unit tests running on the computer described in this article are of course usable with ctest. You just need to add these 2 lines to the CMakeLists.txt:

enable_testing()
add_test(NAME tests COMMAND tests)
Enter fullscreen mode Exit fullscreen mode

Then you can call ctest from the command-line in the build directory.

See the documentation cmake.org/cmake/help/book/masterin...