在 Visual Studio Code Debugger 中 Pytesting Doctests

Oli*_*ung 8 python doctest docstring pytest visual-studio-code

假设我有以下代码foo.py

def start():
    """
    >>> start()
    Hello world
    """
    test = 10
    print('Hello world')
Run Code Online (Sandbox Code Playgroud)

通常,我会通过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"
]
Run Code Online (Sandbox Code Playgroud)

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

ank*_*tis 9

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

这些是相关的.vscode/settings.json

{
    "python.testing.pytestArgs": [
        "--doctest-modules", 
        "--doctest-report", "ndiff",
    ],
    "python.testing.pytestEnabled": true
}

Run Code Online (Sandbox Code Playgroud)

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

如果您需要配置 PyTest,您可以像往常一样在其中之一进行配置

pyproject.tomlsetup.cfgpytest.initox.inisetup.cfg文件:

[tool:pytest]
addopts          = --doctest-modules --doctest-report ndiff
doctest_optionflags= NORMALIZE_WHITESPACE ELLIPSIS
Run Code Online (Sandbox Code Playgroud)