【问题标题】:Pytesting Doctests in Visual Studio Code Debugger在 Visual Studio 代码调试器中进行 Pytesting Doctests
【发布时间】:2018-07-05 13:44:20
【问题描述】:

假设我在foo.py 中有以下代码:

def start():
    """
    >>> start()
    Hello world
    """
    test = 10
    print('Hello world')

通常,我会通过在终端中运行 pytest foo.py --doctest-modules -v 来运行 doctest。相反,我希望能够通过 Visual Studio Code 的内置调试器对其进行测试,以跟踪变量和调用堆栈。

我项目的launch.json中有如下配置:

"name": "PyTest",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "${config:python.pythonPath}",
"module": "pytest",
"args": [
    "${file}",
    "--doctest-modules",
    "-v"
],
"cwd": "${workspaceRoot}",
"env": {},
"envFile": "${workspaceRoot}/.env",
"debugOptions": [
    "RedirectOutput"
]

但是,当我打开文件并在 VSCode 调试器中运行 PyTest 调试配置时,文档测试仅在内置终端中运行 - 调试器面板中没有显示任何内容。我应该如何配置调试器才能使用其变量和调用堆栈?

【问题讨论】:

  • 很遗憾,我无法重现您的问题。在我安装 pytest 并将您的配置复制到 launch.json 之后,一切正常,并且我可以在 doctest 运行期间成功暂停断点。可能是由 VSC/extension/pytest 中的临时错误引起的?
  • 非常感谢@Oliver Leung!!!我正在寻找 Python Doctests VSCode launch.json 2 天 :) 它对我有用,但必须删除几个无效参数,如 pythonPathdebugOptions。我在 WSL 1 上使用 VSCode 1.52.1

标签: python visual-studio-code pytest docstring doctest


【解决方案1】:

VSCode 35.x(至少)似乎内置了对 PyTest 的支持, 并且不需要launch.json 部分:

这些是我的相关@​​987654322@

{
    "python.pythonPath": "venv/bin/python",
    "python.testing.pyTestArgs": [
        "--doctest-modules", 
        "--doctest-report", "ndiff",
    ],
    "python.testing.unittestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "python.testing.pyTestEnabled": true
}

当然,我在指定的虚拟环境中安装了 pytest

如果您需要配置 PyTest,您可以像往常一样将它们设置在其中一个

pyproject.tomlsetupcfgpytest.initox.inisetup.cfg

[tool:pytest]
addopts          = --doctest-modules --doctest-report ndiff
doctest_optionflags= NORMALIZE_WHITESPACE ELLIPSIS

【讨论】:

  • 小初学者的评论:我尝试按照设置配置python.testing.pyTestArgs的建议进行操作,但没有任何改变。原来,我在全局设置中配置了它,但是不小心在项目本地projectroot/.vscode/settings.json文件中覆盖了它。
猜你喜欢
  • 1970-01-01
  • 2020-07-16
  • 1970-01-01
  • 1970-01-01
  • 2015-08-04
  • 2019-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多