【问题标题】:How to debug the current python test file with pytest in VS Code如何在 VS Code 中使用 pytest 调试当前的 python 测试文件
【发布时间】:2021-12-07 11:46:14
【问题描述】:

我知道如何配置VS Code调试器的launch.json来调试当前的python文件:

{ 
    "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}"
            }
        },
}

但是如何配置launch.jsonpytest 调试当前的python 测试文件

【问题讨论】:

  • 抱歉,您要配置什么? Test Explorer上的Debug Test按钮不能满足你的要求,在这种情况下,你只需要Python File的基本配置。
  • 我想如果可以在python file.py 之类的东西上运行调试器,那么它也应该可以在pytest test.py 上运行
  • 恐怕不可能,如果要调试,需要启动调试器服务器。
  • 那么我将如何使用 pytest 调试单个 python 测试?这一定是可能的,对吧?
  • 你能找到Test Explorer吗,Debug Test按钮旁边有Test Explorer按钮。

标签: python debugging visual-studio-code pytest


【解决方案1】:

这在 Django 环境中对我有用:

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

【讨论】:

    【解决方案2】:
    { 
        "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"]            
                
            }]
    }
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2020-12-16
    • 1970-01-01
    • 1970-01-01
    • 2019-06-20
    • 2021-05-12
    • 2019-10-10
    • 2017-10-02
    • 2019-12-29
    相关资源
    最近更新 更多