How to run CTest test in debugger
Asked Answered
C

2

18

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

Cribble answered 27/10, 2017 at 18:14 Comment(2)
That's not what CTest is meant for. And you can easily copy the command from the CMake file.Joappa
@Joappa What should I use instead of CTest ? I want to automate running target with various command line argumentsCribble
C
17

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

Cribble answered 23/11, 2017 at 1:20 Comment(2)
Could you give an example as to what $regex_matching_test might look like? I'm new to C and regex and don't know where to start.Erythrocytometer
@Erythrocytometer That's just your TestSuit.TestCase. For example "MyTestClass.myMethod*"Thieve
D
-1

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.

Dov answered 28/10, 2017 at 14:43 Comment(1)
make test will not launch test in debugger. Actually if you run gdb --args make test you will be debugging make, not your test. Btw, you can also use ctest command to run tests, it will work even if you build with ninja or msvcCribble

© 2022 - 2024 — McMap. All rights reserved.