在Visual Studio代码调试器中Pytesting Doctests

时间:2018-07-05 13:44:21

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

假设我在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调试配置时,doctest仅在内置终端中运行-调试器面板中未显示任何内容。我应该如何配置调试器以使其能够使用其变量并调用堆栈?

1 个答案:

答案 0 :(得分:0)

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

这些是与我有关的.vscode/settings.json

{
    "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]
adopts          = --doctest-modules --doctest-report ndiff
doctest_optionflags= NORMALIZE_WHITESPACE ELLIPSIS