Search code examples
cmakectest

Let CMake setup CTtest to print header and footer around the output from single tests


Is there a way, ideally from CMakeLists.txt, to setup ctest as to

  • print a header before running the individual tests,
  • print a footer after running the individual tests,
  • make the footer dependent on whether the tests were all successful or not ?

The footer should appear below the default output

The following tests FAILED:
       76 - MyHardTest
Errors while running CTest

This concretizes and generalizes a somewhat unclear question that is open since more than 2 years (CMakeLists.txt: How to print a message if ctest fails?). Therefore I fear there is no easy solution.

Thence an alternative question: could the desired bevhavior achieved with CDash?


Solution

  • YES, CTest does have macros to achieve exactly this [1]:

    • CTEST_CUSTOM_PRE_TEST Command to execute before any tests are run during Test stage
    • CTEST_CUSTOM_POST_TEST Command to execute after any tests are run during Test stage

    To activate these macros from cmake for use by ctest, they must somehow be placed into the build directory. So it seems, two steps are necessary:

    (1) Have a script scriptdir/CTestCustom.cmake.in somewhere in the source tree, which contains

    set(CTEST_CUSTOM_POST_TEST "echo AFTER_THE_TEST")
    

    or whatever whatever command instead of "echo"

    (2) Let CMakeLists.txt call

    configure_file("scriptdir/CTestCustom.cmake.in" ${CMAKE_BINARY_DIR}/CTestCustom.cmake)
    

    so that during configuration stage a CTest configuration file is placed under the preferred name [2] CTestCustom.cmake in the build directory.

    [1] https://cmake.org/Wiki/CMake/Testing_With_CTest [2] https://blog.kitware.com/ctest-performance-tip-use-ctestcustom-cmake-not-ctest/