I have a custom target that runs a test when "built", which is provided to me by a custom function belonging to an external dependency (i.e., it is opaque to me and I have no control over its definition).
How do I invoke building this target as a CTest test case? Is this considered an anti-pattern?
If you're able to configure a cmake project that adds a custom target, you're able to see the cmake sources where this is added. Otherwise those sources wouldn't be available. This would allow you to turn add_custom_target
commands equivalent add_test
commands.
If you're not supposed to look into the cmake logic providing the test cases there may be the option of defining a custom cmake function that can be called to extract the COMMAND
arguments ect. for use with either add_custom_target
or add_test
, so if you're collaborating with the people creating the "test" logic, you may be able to rewrite their cmake logic.
If you have no other option though, you could still create a test that builds a specific target.
function(add_target_build_test TEST_NAME TARGET_NAME)
get_property(IS_MULTI_CONFIG_GENERATOR GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
set(CONFIG_PARAM)
if(IS_MULTI_CONFIG_GENERATOR)
set(CONFIG_PARAM --config $<CONFIG>)
endif()
add_test(NAME ${TEST_NAME} COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} ${CONFIG_PARAM} --target ${TARGET_NAME})
endfunction()
add_target_build_test(my_test extern_target)
I'd avoid doing this though, if possible.