13

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?

Oliver Leung
  • 185
  • 1
  • 6
  • Unfortunately, I could not reproduce your problem. After I installed pytest and copied your config to `launch.json`, everything works fine and I can successfully pause on breakpoints during doctests run. Might it be caused by temporary bug in VSC/extension/pytest? – Artalus Sep 29 '18 at 15:03
  • 1
    Thank you so much @Oliver Leung!!! I was looking for Python Doctests VSCode `launch.json` for 2 days :) It works for me, though had to remove couple of non-valid parameters like `pythonPath` and `debugOptions`. I am using VSCode 1.52.1 on WSL 1 – Edgar Manukyan Dec 20 '20 at 15:27

1 Answers1

9

VSCode 35.x (at least) seems to have built-in support for PyTest, and does not need a launch.json section:

These are the relevant .vscode/settings.json

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

Of course, I have pytest installed in the specified virtual-environment.

If you need to configure PyTest, you may configure it, as usual, in one of the

pyproject.toml, setup.cfg, pytest.ini, tox.ini, setup.cfg files:

[tool:pytest]
addopts          = --doctest-modules --doctest-report ndiff
doctest_optionflags= NORMALIZE_WHITESPACE ELLIPSIS
Tsvi M
  • 334
  • 1
  • 12
ankostis
  • 8,579
  • 3
  • 47
  • 61
  • Little beginner's remark: I tried to follow the advice of setting the configuration `python.testing.pyTestArgs` and it changed nothing. Turned out, that I had configured it in the global settings, but overwritten it in the project local `projectroot/.vscode/settings.json` file by accident. – kdb Feb 20 '22 at 10:52
  • Where do I see/run my specific doctests then? They are not listed in the testing tab and I don't see a play button next to the doc strings. – stefanbschneider May 20 '22 at 11:53