Testing Error-Checking

Sometimes one may want to ensure that an exception (or FATAL_ERROR) is thrown, but normally doing so would cause a test to fail. However, there exists an option to invert the test so that it will only pass if an exception or FATAL_ERROR is thrown. This option is called EXPECTFAIL and the test maker must pass this option to the ct_add_test() or ct_add_section() calls that define the test. An example is below:

include(cmake_test/cmake_test)

#[[[
# This test just ensures that throwing a FATAL_ERROR
# in an EXPECTFAIL test will allow the test to succeed.
#]]
ct_add_test(NAME "make_sure_function_fails" EXPECTFAIL)
function("${CMAKETEST_TEST}")

    function(failing_fxn)
        message(FATAL_ERROR "I have erred.")
    endfunction()

    failing_fxn()

endfunction()

When using CMinx to document a test, the EXPECTFAIL option will be listed in the test definition.

Implementation Details

Due to limitations in the CMake language, CMakeTest must run EXPECTFAIL tests in a subprocess. This introduces significant latency in executing such tests. If performance becomes a problem try splitting some tests into different files and use CTest’s -j option to run more processes at once.