question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Flag to simply not catch exceptions at all

See original GitHub issue

Problem

Using pytest 5.4.2 I don’t see a flag that simply tells pytest to not catch any exceptions. So on the first test that raises and unhandled exception, pytest itself would exit with that exception.

Background

I’m trying to debug tests with vscode’s Python extension’s debugpy debugger. It does not support --pdb or --pdbcls. So there is no obvious way to get it to break on an exception. If pytest would simply not catch any exceptions, then any debugger could break on that unhandled exception. In this case I’m running a single test.

I’m sure it’s been a design goal of pytest that it itself never “crashes” but in this case it would make it easy to debug pyttest tests with any debugger.

References

vscode/python/debug/py is working on a solution: https://github.com/microsoft/debugpy/issues/275

  • a detailed description of the bug or suggestion
  • output of pip list from the virtual environment you are using
  • pytest and operating system versions

Environment

pytest 5.4.2 macOS 10.15.3 conda

Package Version


alabaster 0.7.12
appdirs 1.4.4
appnope 0.1.0
asciitree 0.3.3
astroid 2.4.1
attrs 19.3.0
Babel 2.8.0
backcall 0.1.0
bleach 3.1.5
cachey 0.2.1
certifi 2020.4.5.1
chardet 3.0.4
cycler 0.10.0
dask 2.17.2
decorator 4.4.2
defusedxml 0.6.0
docutils 0.16
entrypoints 0.3
fasteners 0.15
flake8 3.8.2
freetype-py 2.1.0.post1
fsspec 0.7.4
future 0.18.2
graphviz 0.14
HeapDict 1.0.1
idna 2.9
imageio 2.8.0
imagesize 1.2.0
ipykernel 5.3.0
ipython 7.14.0
ipython-genutils 0.2.0
ipywidgets 7.5.1
isort 4.3.21
jedi 0.17.0
Jinja2 2.11.2
jsonschema 3.2.0
jupyter 1.0.0
jupyter-client 6.1.3
jupyter-console 6.1.0
jupyter-core 4.6.3
kiwisolver 1.2.0
lazy-object-proxy 1.4.3
MarkupSafe 1.1.1
matplotlib 3.2.1
mccabe 0.6.1
mistune 0.8.4
monotonic 1.5
more-itertools 8.3.0
mypy 0.780
mypy-extensions 0.4.3
napari 0.3.2
napari-plugin-engine 0.1.5
napari-svg 0.1.3
nbconvert 5.6.1
nbformat 5.0.6
networkx 2.4
notebook 6.0.3
numcodecs 0.6.4
numpy 1.18.4
numpydoc 1.0.0
packaging 20.4
pandas 1.0.4
pandocfilters 1.4.2
parso 0.7.0
pexpect 4.8.0
pickleshare 0.7.5
Pillow 7.1.2
pip 20.0.2
pluggy 0.13.1
pooch 1.1.1
prometheus-client 0.8.0
prompt-toolkit 3.0.5
psutil 5.7.0
ptyprocess 0.6.0
py 1.8.1
pycodestyle 2.6.0
pyflakes 2.2.0
Pygments 2.6.1
pylint 2.5.2
PyOpenGL 3.1.5
pyparsing 2.4.7
PyQt5 5.14.2
PyQt5-sip 12.7.2
pyrsistent 0.16.0
pytest 5.4.2
pytest-faulthandler 2.0.1
pytest-ordering 0.6
pytest-qt 3.3.0
pytest-timeout 1.4.1
python-dateutil 2.8.1
pytz 2020.1
PyWavelets 1.1.1
PyYAML 5.3.1
pyzmq 19.0.1
qtconsole 4.7.4
QtPy 1.9.0
requests 2.23.0
scikit-image 0.17.2
scipy 1.4.1
Send2Trash 1.5.0
setuptools 46.4.0.post20200518 shiboken2 5.15.0
six 1.15.0
snowballstemmer 2.0.0
Sphinx 3.0.4
sphinxcontrib-applehelp 1.0.2
sphinxcontrib-devhelp 1.0.2
sphinxcontrib-htmlhelp 1.0.3
sphinxcontrib-jsmath 1.0.1
sphinxcontrib-qthelp 1.0.3
sphinxcontrib-serializinghtml 1.1.4
terminado 0.8.3
testpath 0.4.4
tifffile 2020.5.25
toml 0.10.1
toolz 0.10.0
torch 1.5.0
torchvision 0.6.0
tornado 6.0.4
traitlets 4.3.3
typed-ast 1.4.1
typing-extensions 3.7.4.2
urllib3 1.25.9
vispy 0.6.4
wcwidth 0.1.9
webencodings 0.5.1
wheel 0.34.2
widgetsnbextension 3.5.1
wrapt 1.12.1
xarray 0.15.1
zarr 2.4.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
pwinstoncommented, Jun 24, 2020

Your two hookimpl work great for me. I’ve wrapped then in a env var. So I can run pytest normally or when debugging I can set that var, and then the debugger breaks in the right spot.

I think there should be a pytest flag that does this. I know --pdb and --pdbcls and sys.breakpointhook are all trying to address this more elegantly. But none of the three seem to work with vscode yet, at least for exceptions. Simply letting pytest crash works with any debugger past, present and future. I can see why pytest would be uncomfortable with this idea, though, but it really hits the spot in this case.

This works 100% for me and maybe Google will send other people here. Thanks for such a quick and helpful response. This was the thread me asking vscode Python people how to make this work: https://github.com/microsoft/debugpy/issues/275#issuecomment-648984822. They had some ideas but no real solution today. This stack overflow has the answer now.

4reactions
bluetechcommented, Jun 24, 2020

Does vscode set sys.breakpointhook?


You can cause pytest to crash with a plugin such as this, but don’t do that 😃

import pytest

@pytest.hookimpl(tryfirst=True)
def pytest_exception_interact(call):
    raise call.excinfo.value

@pytest.hookimpl(tryfirst=True)
def pytest_internalerror(excinfo):
    raise excinfo.value
Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Having a flag to indicate if we should throw errors
The problem with this approach is that while exceptions never get thrown (and thus, the application never crashes due to uncaught ...
Read more >
How can I write a `try`/`except` block that catches all exceptions?
If you're attempting to catch ALL exceptions, then put all your code within the "try:" statement, in place of 'print "Performing an action...
Read more >
Not All Exceptions Are Errors - CodeProject
Modifying all three called methods to pass back a flag the user has cancelled? Or simply throw a typed exception in DoSomethingDeeper and...
Read more >
CA1031: Do not catch general exception types (code analysis)
By default, this rule only flags general exception types being caught, but this is configurable. Rule description. General exceptions should not ...
Read more >
Complete Guide to Exception Handling in Spring Boot
One major difference here is that these handlers will handle exceptions thrown by all the controllers in the application and not just ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found