Suppose I have the following code in foo.py
:
def start():
"""
>>> start()
Hello world
"""
test = 10
print('Hello world')
Normally, I would run the doctest by running pytest foo.py --doctest-modules -v
in the terminal. I instead want to be able to test it through Visual Studio Code's built-in debugger to track the variables and call stack.
I have the following configuration in my project's 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"
]
However, when I open the file and run the PyTest debugging configuration in the VSCode debugger, the doctests are only run in the built-in terminal - nothing shows up in the debugger panel. How should I configure the debugger to be able to use its variables and call stack?
VSCode 35.x (atleast) seems to have built-in support for PyTest,
and doe not need a launch.json
section:
These are my relevant .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
}
Ofcourse i have pytest install in the specified virtual-environment.
If you need to configure PyTest, you may set them, as usual, in one of
pyproject.toml
, setupcfg
, pytest.ini
, tox.ini
, setup.cfg
:
[tool:pytest]
addopts = --doctest-modules --doctest-report ndiff
doctest_optionflags= NORMALIZE_WHITESPACE ELLIPSIS
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With