It looks like the topic is not very popular. The approach I see most often in CMake scripts is that it's configuration, not code.
Most of the projects I found were too opinionated and had too many assumptions, like how to organize test code. So yes, I recently made my own small project to test CMake code ;) It probably doesn't cover all cases, but it works to the extent I need it to.
I was inspired by how CMake code is tested in the CMake project itself https://github.com/Kitware/CMake/tree/master/Tests/CMakeTests.
I am using add_test()
and custom target that call CTest - only two functions and few assertions (no mocks so far). Every test code is stored in separate file and looks like usual unit test. Test is failed if message(SEND_ERROR "...")
or message(FATAL_ERROR "...")
is executed.
include(CMakeUnit) # for assertions
include(set) # module under test
# Act
set_if_defined(NewVariable "New value")
# Assert
EXPECT_UNDEFINED(NewVariable)
For more examples please see https://github.com/tawez/CMakeUnit-example/tree/master/tests