Expected build-failure tests in CMake

前端 未结 2 1088
囚心锁ツ
囚心锁ツ 2021-01-31 02:44

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         


        
2条回答
  •  星月不相逢
    2021-01-31 03:30

    @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.

提交回复
热议问题