Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytesting Doctests in Visual Studio Code Debugger

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?

like image 925
Oliver Leung Avatar asked Jul 05 '18 13:07

Oliver Leung


1 Answers

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
like image 118
ankostis Avatar answered Oct 18 '22 15:10

ankostis