CMake: setting an environmental variable for ctest (or otherwise getting failed test output from ctest/make test automatically)

The built-in test target cannot be modified, but you can add a custom check target which invokes ctest with the --output-on-failure switch in the following way:

if (CMAKE_CONFIGURATION_TYPES)
    add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} 
        --force-new-ctest-process --output-on-failure 
        --build-config "$<CONFIGURATION>")
else()
    add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} 
        --force-new-ctest-process --output-on-failure)
endif()

The custom target has to be set up differently for single build type and multi-configuration builds. In the latter case, the active build configuration has to be passed on to the ctest invocation using the --build-config flag. The --force-new-ctest-process is used by the built-in test target by default.