You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
OutcomeException should not be a subclass of Exception #580
def blackbox(callback, arg):
# this is assumed to be application code
try:
callback(arg)
except Exception:
pass
def test_callback():
def cb(x):
if not x:
import pytest
pytest.fail('foo')
blackbox(cb, True)
blackbox(cb, False)
The testcase will pass since except Exception will catch the Failed raised by pytest.fail. However, I don't think there is any valid use-case for application code to prevent a testcase failure from propagating to the test runner.
A more practical example where this is a major issue is monkeypatching a library function to fail when called. For example, one might want to monkeypatch smtplib.SMTP.connect to fail when code tries to send an email unexpectedly - but chances are good that the application is wrapping that code in a try...except Exception block to prevent an email sending failure from breaking things (e.g. because the email is not very important). So now the pytest.fail called in the monkeypatched method will never propagate since it's caught by application code.
Just for reference/info: using BaseException is problematic, e.g. when people are trying to have a safe repr (and only handling Exception, and where pytest-django might throw a failure for when DB access is not allowed), or with asyncio which only handles Exception (pytest-dev/pytest-asyncio#123).
Activity
pytestbot commentedon Sep 8, 2014
Original comment by Floris Bruynooghe (BitBucket: flub, GitHub: flub):
When following this route, how can one also protect agains
try: callback(); except: pass, that is a bare exception in user code?(I must admit I've never tried to call
pytest.fail()in callbacks before)pytestbot commentedon Sep 8, 2014
Original comment by BitBucket: thiefmaster, GitHub: thiefmaster:
That's such an unlikely case though...
RonnyPfannschmidt commentedon Sep 13, 2015
@flub
its sensible to use
BaseExceptionto help peoplewe don't need to protect about the diaper antipattern
nicoddemus commentedon Sep 25, 2015
Shouldn't this be in
2.9.0? It's not a bug per-se, but an improvement that might even break current test suites, IMO.The-Compiler commentedon Aug 5, 2016
Changing the milestone for this to 4.0 for now.
3 remaining items
make Test Outcomes inherit from BaseException instead of exception
make Test Outcomes inherit from BaseException instead of exception
make Test Outcomes inherit from BaseException instead of exception
make Test Outcomes inherit from BaseException instead of exception
make Test Outcomes inherit from BaseException instead of exception
Merge pull request #2490 from RonnyPfannschmidt/fix-580
tests: test_invalid_template_variable: use include
tests: test_invalid_template_variable: use include
Change the inherited exception class in tests
Change the inherited exception class in tests
tests: test_invalid_template_variable: use include (#506)
tests: test_invalid_template_variable: use include (pytest-dev#506)
blueyed commentedon May 7, 2019
Just for reference/info: using BaseException is problematic, e.g. when people are trying to have a safe repr (and only handling Exception, and where pytest-django might throw a failure for when DB access is not allowed), or with asyncio which only handles
Exception(pytest-dev/pytest-asyncio#123).tests: test_invalid_template_variable: use include (#506)