Unit Tests - USCRPL/mbed-cmake GitHub Wiki
mbed-cmake supports unit tests with a similar structure to mbed-cli's unit testing setup. You write unit tests using the GTest framework, and mbed-cmake will handle building them into an executable and executing them using CMake's built-in ctest
test runner. Mbed OS also includes an incomplete set of stub headers used for unit testing, and mbed-cmake makes these available to your tests.
Unit Testing Mode
Unit testing mode can be enabled using -DMBED_UNITTESTS=TRUE
argument to CMake. This argument disables the mbed-cmake toolchain file and lets CMake find the native compilers on the host. Note that because of this, you must use it on a fresh new build directory or it won't take effect properly. In fact, it might be easiest to have two CMake build dirs, one with unit tests enabled and one without.
A couple of other mbed-cmake features are activated in unit test mode:
- mbed-cmake will automatically download and build the GTest and GMock libraries. These libraries are not available in most package managers, so this should save some effort setting up tests.
- The
add_mbed_executable()
function becomes a no-op (it creates a dummy target that is not built). - The
mbed-os
cmake target is still exported, but now it refers to the mbed dummy headers for unit testing. This should allow some libraries that link to mbed-os to still compile. add_mbed_unit_test()
is available as a convenience function. Ths function is simple shorthand for creating and adding a test program:
function(add_mbed_unit_test EXECUTABLE)
add_executable(${EXECUTABLE} ${ARGN})
target_link_libraries(${EXECUTABLE} GMock::Main)
gtest_discover_tests(${EXECUTABLE})
endfunction(add_mbed_unit_test)