-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent pytest.raises from expecting an exception #5165
Comments
Here's a workaround: def test_thing(exception_for_object):
exception, obj = exception_for_object
try:
obj.thing()
except exception:
pass
else:
assert not exception |
Why not: def test_thing(exception_for_object):
exception, obj = exception_for_object
if exception:
with pytest.raises(exception):
obj.thing()
else:
obj.thing() But check out also https://github.com/blueyed/pytest/blob/253415cf397fd3d73d0dac0478d27075bdcea1f6/doc/en/example/parametrize.rst#parametrizing-conditional-raising |
However, I can see that |
@blueyed I try to avoid splitting tests like that in favor of just having 2 tests. That link is pretty cool, though! Hadn't thought of that... |
Im on mobile, bit this has been discussed before, use a different context manager |
Here is an example of a case where it would be really handy to be able to tell
pytest.raises
not to expect an exception:The result, though, is this:
This behavior is not useful because
obj.thing
couldn't raise()
anyway:Not even in Python 2:
Note that
None
doesn't work instead of()
.followed by
Tox dump of
pip list
(running on CentOS 7.6.1810):The text was updated successfully, but these errors were encountered: