Skip to content

Instantly share code, notes, and snippets.

@xealits
Last active November 16, 2025 18:02
Show Gist options
  • Select an option

  • Save xealits/d78e6625b1a4e3ac5c78da4a328ad35f to your computer and use it in GitHub Desktop.

Select an option

Save xealits/d78e6625b1a4e3ac5c78da4a328ad35f to your computer and use it in GitHub Desktop.
CMake ctest --watch with entr
# in the top project:
cmake_minimum_required(VERSION ...)
project(alib LANGUAGES CXX)
# CTest setup
# not sure if there is a point to try to separate the ctest definitions from CMakeLists.txt
# add_test are a part of the project or the build pipeline?
#. ENABLE_TESTING() # this should already be done in include(CTest)
# even though kitware says it's required
# https://www.kitware.com/ctest-and-cdash-testing-without-a-build-system/
include(CTest)
...
# the BUILD_TESTING=ON after include(CTest)
# set it to OFF: cmake -DBUILD_TESTING=ON ...
# to avoid building too many targets (with FetchContent Catch2 etc)
# it means that a CTest script would have to set it with:
#. ctest_configure(OPTIONS "-DBUILD_TESTING=ON")
if(BUILD_TESTING)
# a subdirectory with actual tests like Catch2
# it adds an executable target "${PROJECT_NAME}_tests"
add_subdirectory(tests)
add_test(NAME test_all COMMAND $<TARGET_FILE:${PROJECT_NAME}_tests>)
message(STATUS "${PROJECT_NAME} > set up CTest")
endif()
BUILD_DIR = "build"
run_tests:
find . -name "*[ch]pp" | entr sh -c "cmake --build $(BUILD_DIR) && ctest --test-dir $(BUILD_DIR) --rerun-failed --output-on-failure"
# run ctest script on file updates:
find . -name "*[ch]pp" | entr ctest -VV -S TestOnly.cmake
# -VV is needed to print the test log
# for some reason thede don't work:
# ctest --output-on-failure -S TestOnly.cmake
# CTEST_OUTPUT_ON_FAILURE=1 ctest -S TestOnly.cmake
# or manually:
find . -name "*[ch]pp" | entr sh -c "cmake --build build && ctest --test-dir build --rerun-failed --output-on-failure"
# a minimal CTest script
# to just rebuild & test an already configured build
# no config step etc
set(CTEST_SOURCE_DIRECTORY "${CTEST_SCRIPT_DIRECTORY}")
set(CTEST_BINARY_DIRECTORY "${CTEST_SCRIPT_DIRECTORY}/build") # the build directory is hardcoded
set(CTEST_CMAKE_GENERATOR "Unix Makefiles")
ctest_start(ContinuousTest) # ctest_start is a must
ctest_build()
ctest_test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment