c++visual-studio-codecmakelaunchctest

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


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}"
        }
    ]
}

Solution

  • 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": "${cmake.testWorkingDirectory}",
        "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...
    }