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
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.