Sometimes it\'s good to check that certain things fail to build, e.g.:
// Next line should fail to compile: can\'t convert const iterator to iterator.
my_new_con
@Fraser's answer is a good approach, in particular the WILL_FAIL
property is good advice. There is an alternative to making the failing target part of the main project though. The use case in the question is pretty much what the ctest --build-and-test
mode is meant for. Rather than making the expected-to-fail target part of the main build, you can put it in its own separate mini project which is then built as part of a test. An example of how this might look in the main project goes something like this:
add_test(NAME iter_conversion
COMMAND ${CMAKE_CTEST_COMMAND}
--build-and-test
${CMAKE_CURRENT_LIST_DIR}/test_iter
${CMAKE_CURRENT_BINARY_DIR}/test_iter
--build-generator ${CMAKE_GENERATOR}
--test-command ${CMAKE_CTEST_COMMAND}
)
set_tests_properties(iter_conversion PROPERTIES WILL_FAIL TRUE)
This has the advantage that it will be part of the project's test results and will therefore be more likely to get executed regularly as part of normal testing processes. In the above example, the test_iter
directory is essentially it's own separate project. If you need to pass information to it from the main build, you can do that by adding --build-options
to define cache variables to pass to it's CMake run. Check the latest docs for recently corrected/clarified help on this area.