Search code examples
cmakegdbctest

How to run CTest test in debugger


Suppose in CMakeLists.txt I have

add_executable(mytarget main.cpp)

enable_testing()
add_test(mytarget_test0 mytarget -option0)

Is there any easy way how can I run mytarget in GDB with all command line options from some particular CTest test? (Other than searching for test in CMakeLists and then copy-pasting add_test parameters to command line manually?)

Real life scenario: I run all tests using ctest, one fails, I want to open it in debugger quickly.

In other build systems there are command line parameters to use gdb, for example in Meson meson test --gdb testname , in bazel bazel --run_under=gdbserver. I did not found anything similar for CTest


Solution

  • It is possible to get test command with arguments:

    ctest -R $regex_matching_test -V -N
    

    As output you will get something like:

    Test project ../cmake-build-debug-gcc7
    Constructing a list of tests
    Done constructing a list of tests
    
    1: Test command: ../cmake-build-debug-gcc7/my_tool  "-v" "test0"
      Test #1: my_tool_test0
    
    Total Tests: 1
    

    Then using regexp it is possible to grab command line args for gdb