Search code examples
pythonvisual-studio-codepytestvscode-keybinding

VS Code keybinding for starting debugging with a specific debug configuration?


I would like to create a keybind for debugging a specific pytest. I was wondering if it was possible to maybe modify .vscode/launch.json such that whenever I press F5 that it will debug a specific test, instead of having to manually press it every time, as shown in this image. Manually starting debug of a specific test

I have tried setting the Keyboard Shortcut: testing.debugAtCursor, but it debugs all the tests I have in my parametrize, and I would ideally like a solution for running a specific one without having to do pytest.skip.


Solution

  • I couldn't find a command to launch an existing debug configuration in launch.json and gave up looking. I did find debug.startFromConfig, but you have to copy a debug configuration into keybindings.json, so it's not really ideal, but here you go. If you only want it to be defined for a specific workspace, you can probable do that with a when clause using the resourceFilename context key and checking if it starts with the specific workspace folder's path.

    An example from my testing:

    {
        "key": "ctrl+b",
        "command": "debug.startFromConfig",
        "args": {
            "name": "test",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/test.cpp",
            "cwd": "${workspaceFolder}",
        },
    },
    

    See also https://github.com/microsoft/vscode/blob/83b92b5e974ed5b67559d6176848f2b11b5f24de/src/vs/workbench/contrib/debug/browser/debugCommands.ts#L687, https://github.com/microsoft/vscode/blob/main/src/vs/workbench/contrib/debug/common/debug.ts#L817, https://github.com/microsoft/vscode/blob/83b92b5e974ed5b67559d6176848f2b11b5f24de/src/vs/workbench/contrib/debug/browser/debugCommands.ts#L754