How to debug the current python test file with pytest in VS Code

Viewed 1327

I know how to configure the VS Code debugger's launch.json to debug the current python file:

{ 
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "cwd": "${fileDirname}",
            "env": {
                "PYTHONPATH": "${workspaceFolder}${pathSeparator}${env:PYTHONPATH}"
            }
        },
}

But How to configure launch.json to debug the current python test file with pytest?

3 Answers

This is what works for me in the context of Django and Pytest:

{
  "name": "Python: Debug test file",
  "type": "python",
  "request": "launch",
  "module": "pytest",
  "args": [
    "${file}"
  ],
  "django": true
}

Add below configuration in your launch.json file, it should work.

 "configurations": [
    {
      "name": "Python: Pytest",
      "type": "python",
      "request": "launch",
      "module": "pytest",
      "args": [
        "${file}"
      ],
        },
  ]
{ 
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "module": "pytest",
            "console": "integratedTerminal",
            "args" : ["-s", "-q", "-m <id inpytest.ini>", "-myArgu1=xxx"]            
            
        }]
}
Related