When I run pytest in the vscode debugger with "Uncaught Exceptions" checked, and there are test errors, no uncaught exceptions occur, because pytest catches them d
The behavior is not identical but the test runner stops at the uncaught exception.
Add the file conftest.py
to the directory with your tests.
# conftest.py
def pytest_exception_interact(node, call, report):
print( call.excinfo.traceback[0] )
pass
This adds a hook for any uncaught exceptions.
Place a break point at the line with pass
.
Then Debug
the test.
Look in the Debug console and you see the file and line number where the exception happened.
test_example.py F File 'path\\test_example.py':4 in test_example
assert False
And the debugger stops at the pass
line.
You can make this hook function as fancy as you like.
I raised an issue with pytest and they made a suggestion that works. Add this to your conftest.py
:
import os
import pytest
if os.getenv('_PYTEST_RAISE', "0") != "0":
@pytest.hookimpl(tryfirst=True)
def pytest_exception_interact(call):
raise call.excinfo.value
@pytest.hookimpl(tryfirst=True)
def pytest_internalerror(excinfo):
raise excinfo.value
Then in your "request": "test"
launch.json you can toggle that env var:
{
"name": "Debug Tests",
"type": "python",
"request": "test",
"console": "integratedTerminal",
"justMyCode": true,
"env": {
"_PYTEST_RAISE": "1"
},
}
If _PYTEST_RAISE
is set and you check only the Uncaught Exceptions box you will break right where the uncaught exception was raised not before.
I also opened an issue with debugpy
and they had some ideas, but no solution today. The _PYTEST_RAISE
trick works 100% solves this for me.