skip to Main Content

When debugging unit tests through the GUI, I don’t know how to configure VS Code to step inside third-party code.
Note: I use a workspace.

enter image description here


Edit: Currently as a workaround I can use this configuration from "Run and Debug tab" where I have to specify which test I want to run:

"configurations": [
    {
        "name": "Debug specific test",
        "type": "python",
        "module": "pytest",
        "request": "launch",
        "purpose": ["debug-test"],
        "console": "integratedTerminal",
        "justMyCode": false,
        "args": [
            "explorer/test/test_projects_controller.py::TestProjectsController::test_get_metadata"
        ]
    }
]``

2

Answers


  1. Chosen as BEST ANSWER

    This is a limitation in current VSCode version: VSCode only uses launch.json file to configure pytest debugging options, it ignores the workspace launch section.
    It is planned to be fixed soon: https://github.com/microsoft/vscode-python/issues/21249
    As a workaround, we can duplicate the workspace launch section in a .vscode/launch.json file, ex:

    {
        "configurations": [
            {
                "name": "Python: Debug Tests",
                "type": "python",
                "request": "launch",
                "program": "${file}",
                "purpose": ["debug-test"],
                "console": "integratedTerminal",
                "justMyCode": false,
                "presentation": {
                    "hidden": true, // keep original launch order in 'run and debug' tab
                }
            },
        ],
        "version": "0.2.0"
    }
    

  2. I’d try following the instructions from the VS Code Python docs’ section on debugging tests, which states:

    To customize settings for debugging tests, you can specify "purpose": ["debug-test"] in the launch.json file in the .vscode folder from your workspace. This configuration will be used when you run Test: Debug All Tests, Test: Debug Tests in Current File and Test: Debug Test at Cursor commands.

    For example, the configuration below in the launch.json file disables the justMyCode setting for debugging tests:

    {
      "name": "Python: Debug Tests",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "purpose": ["debug-test"],
      "console": "integratedTerminal",
      "justMyCode": false
    }
    

    If you have more than one configuration entry with "purpose": ["debug-test"], the first definition will be used since we currently don’t support multiple definitions for this request type.

    Note: I’ve also seen older configs floating around that use "request": "test" instead of "purpose": ["debug-test"] (Ex. this), so you can try that if "purpose": ["debug-test"] doesn’t work for you.

    There also seems to be a "debugStdLib": true property you can use if you want to step into standard library things (source).

    There’s an open issue ticket where this is apparently not working for version 2023.8.0 of the Python extension (#21249), but it will be fixed in later versions.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search