I am trying to use presets to solve a particular testing need in a codebase of mine. Specifically, my C++ code relies on lots of macros that are passed from CMake to conditionally compile code. In order to be able to properly test all those different code branches that depend on the macros, I wanted to use CMake presets to define the different configurations, build my test executable (that uses gtest) in all the relevant configurations, and then execute the test executable for each test configuration on an android device by pushing it via adb. I was hoping i could just use gtest_discover_tests and then apply a filter in my testPresets to only execute the relevant test(s) for a specific configuration (eg. only a test that is actually dependent on cmake configuration that changes macros for the tested code); however, I don’t know how I can specify a ctest run to actually push the test executable to the android device and then only run the test(s) that are filtered for by the test preset. Is there a way to do that? If it is not possible, I could isolate the macro-dependent tests in separate executables and just push and execute them via custom targets but i feel like this would undermine the purpose of ctest and the possibilities it offers in conjunction with gtest_discover_tests…
Here is a minimal example based on my description:
Root CMakeLists.txt:
cmake_minimum_required(VERSION 3.22.1)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
project(MyProject)
add_subdirectory(my_library)
if (PROJECT_IS_TOP_LEVEL)
cmake_minimum_required(VERSION 3.25.0) # support for packagePresets and workflowPresets
enable_testing()
add_subdirectory(test)
endif ()
test/CMakeLists.txt
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/v1.15.0.zip
)
FetchContent_MakeAvailable(googletest)
add_executable(TestBinary main.cpp)
add_subdirectory(...)
target_link_libraries(TestBinary PRIVATE
my_library
gtest
gtest_main
...
)
include(GoogleTest)
set(CMAKE_GTEST_DISCOVER_TESTS_DISCOVERY_MODE PRE_TEST)
gtest_discover_tests(TestBinary)
An example test in test/main.cpp using gtest, which I want to filter for with a test preset like so “filter”: { “include”: { “name”: “MyTests\.ExampleBranchTest” } } :
#include <gtest/gtest.h>
TEST(MyTests, ExampleBranchTest)
{
EXPECT_FALSE(some_function_with_macro_branches());
}
where some_function_with_macro_branches could look like:
bool some_function_with_macro_branches()
{
#if defined(BRANCH_VALUE_MIN) && defined(BRANCH_VALUE_MAX)
const auto result = MinMaxAlgorithm(BRANCH_VALUE_MIN, BRANCH_VALUE_MAX);
#elifdef BRANCH_VALUE
const auto result = BaseAlgorithm(BRANCH_VALUE);
#else
const auto result = FallbackAlgorithm();
#endif
// Further processing of result
...
return xyz;
}
In the end, I want the MyTests.ExampleBranchTest to be executed three times, once for each possible branch, as dictacted by CMake configurations that set the macro values for the branch checks.