This commit is contained in:
Tim Strazny 2018-04-06 14:16:12 +02:00
parent 5d4fe87b72
commit ec2d8223cf
4 changed files with 25 additions and 4 deletions

View File

@ -188,6 +188,7 @@ Tareq Alayan
Ted Xiao Ted Xiao
Thomas Grainger Thomas Grainger
Thomas Hisch Thomas Hisch
Tim Strazny
Tom Dalton Tom Dalton
Tom Viner Tom Viner
Trevor Bekolay Trevor Bekolay

View File

@ -584,6 +584,7 @@ def raises(expected_exception, *args, **kwargs):
""" """
__tracebackhide__ = True __tracebackhide__ = True
if not issubclass(expected_exception, BaseException):
for exc in filterfalse(isclass, always_iterable(expected_exception)): for exc in filterfalse(isclass, always_iterable(expected_exception)):
msg = ("exceptions must be old-style classes or" msg = ("exceptions must be old-style classes or"
" derived from BaseException, not %s") " derived from BaseException, not %s")

View File

@ -0,0 +1 @@
``pytest.raises`` now works with exception classes that look like iterables.

View File

@ -1,3 +1,4 @@
from _pytest.outcomes import Failed
import pytest import pytest
import sys import sys
@ -147,3 +148,20 @@ class TestRaises(object):
with pytest.raises(ValueError): with pytest.raises(ValueError):
with pytest.raises(IndexError, match='nomatch'): with pytest.raises(IndexError, match='nomatch'):
int('asdf') int('asdf')
def test_raises_exception_looks_iterable(self):
from six import add_metaclass
class Meta(type(object)):
def __getitem__(self, item):
return 1/0
def __len__(self):
return 1
@add_metaclass(Meta)
class ClassLooksIterableException(Exception):
pass
with pytest.raises(Failed, match="DID NOT RAISE <class 'raises.ClassLooksIterableException'>"):
pytest.raises(ClassLooksIterableException, lambda: None)