11
votes

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

2
That's not what CTest is meant for. And you can easily copy the command from the CMake file. - usr1234567
@usr1234567 What should I use instead of CTest ? I want to automate running target with various command line arguments - random

2 Answers

12
votes

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

0
votes

I use the following procedure:

make clean
make # Build target and all unit tests
make test # Run all unit tests: make test

You will get an result list and some output, depending of the unit test frame work. Fix the issue and re-run the failed test within gdb until it succeeds. Restart the whole procedure.

In case you are using an IDE (e.g. QtCreator) it is easy to debug through the failed test case.