How configure launch.json in VSCode to debug an unit ctest(cmake) from the new testing explorer view?
Asked Answered
E

2

6

I have two cmake unit-test to check url and json helper classes, declared in the CMakeLists.txt of my project like following:

########################################
# CMakeLists.txt - Tests               #
########################################
add_executable(urltest test/url.cpp)
add_test(NAME URLTest COMMAND urltest)

add_executable(jsontest test/json.cpp)
add_test(NAME JSONTest COMMAND jsontest )

I get visually the test units in the testing view of VSCode, and I can run perfectly the both in release mode: Execute the unit tests in release mode

Now if I want debugging it with the other button I have to select a launch configuration, what requires to create manually one launch configuration for each unit test: Execute the unit tests in debug mode

My current launch.json file require so one configuration for each unit test, it's impossible to maintain:

{ // launch.json
    "version": "0.2.0",
    "configurations": [
        {
            "name": "CTest-urltest",
            "type": "lldb",
            "request": "launch",
            "program": "${workspaceFolder}/build/urltest",
            "args": [],
            "initCommands": ["breakpoint set -n main -N entry"],
            "exitCommands": ["breakpoint delete entry"],
            "cwd": "${workspaceFolder}"
        },
        {
            "name": "CTest-jsontest",
            "type": "lldb",
            "request": "launch",
            "program": "${workspaceFolder}/build/jsontest",
            "args": [],
            "initCommands": ["breakpoint set -n main -N entry"],
            "exitCommands": ["breakpoint delete entry"],
            "cwd": "${workspaceFolder}"
        }
    ]
}

I guess the problem is only the program attribute, How can we map the program clicked on the testing view of VSCode to an unique launch configuration? something like following for example:

{ // launch.json
    "version": "0.2.0",
    "configurations": [
        {
            "name": "CTest",
            "type": "lldb",
            "request": "launch",
            "program": "${workspaceFolder}/build/${command: testing.currentPath}",
            "args": [],
            "initCommands": ["breakpoint set -n main -N entry"],
            "exitCommands": ["breakpoint delete entry"],
            "cwd": "${workspaceFolder}"
        }
    ]
}
Ergonomics answered 2/5, 2023 at 22:8 Comment(2)
Weren't all these fancy GUI IDEs and editors, like VSCode, with their pretty dialogs and buttons, supposed to make things easier, to avoid all these yucky Makefiles and shell commands? You mean that I must concoct some magic incantation in the form of a convoluted JSON configuration file, to get basic functionality right? That's a darn shame, isn't it? After reading makes man page, as lengthy as it is, with plenty of examples, it's always pretty straightofrward for me to simply push F5 in emacs, and get everything built.Ritch
Yes. By this way the debug part with F5 works as a charm. The issue describing concerns the new TestExplorer view from VSCode: Click on "Debug Test" on an individual unit test asks for a configuration-launch.. yes should be pretty smooth and automatic, maybe an issue :-(Ergonomics
A
12

This is now documented in Debugging Tests (permalink).

As suggested, prefer ${cmake.testProgram} over ${command:cmake.launchTargetPath} as the program, since this allows you to have a different launch target while being able to debug the test(s).

Include "args": [ "${cmake.testArgs}" ] to be able to run a specific test. Without this, all tests will be debugged even though you only selected one.

GDB

{
    "name": "(ctest) Launch",
    "type": "cppdbg",
    "cwd": "${workspaceFolder}",
    "request": "launch",
    "program": "${cmake.testProgram}",
    "args": [ "${cmake.testArgs}" ],
    // other options...
}

MSVC

{
    "name": "(ctest) Launch",
    "type": "cppvsdbg",
    "request": "launch",
    "program": "${cmake.testProgram}",
    "args": [ "${cmake.testArgs}" ],
    // other options...
}
Argali answered 10/6, 2023 at 16:7 Comment(3)
Using ${cmake.testProgram} I get 'program' is missing or empty. Using ${command:cmake.launchTargetPath} it starts debugging but the one I am interested in.Foliage
Really thanks @neris, it works as a charm on my side in using ${cmake.testProgram} exactly like you propose (with lldb) => <code>{ "name": "CTest", "type": "lldb", "request": "launch", "program": "${cmake.testProgram}", "args": [ "${cmake.testArgs}" ], "initCommands": ["breakpoint set -n main -N entry"], "exitCommands": ["breakpoint delete entry"], "cwd": "${workspaceFolder}" } </code>Ergonomics
That link was a big help!Caterer
E
2

How can we map the program clicked on the testing view of VSCode to an unique launch configuration?

This seems to be documented in https://github.com/microsoft/vscode-cmake-tools/blob/main/docs/debug-launch.md.

In your launch.json, you can have a debug config with this value in program

// Resolved by CMake Tools:
"program": "${command:cmake.launchTargetPath}",
Envious answered 6/6, 2023 at 11:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.