Skip to content

Commit e012dbe

Browse files
authored
Merge pull request #3373 from backbord/master
Fix issue #3372
2 parents 5d4fe87 + 5bd8561 commit e012dbe

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ Tareq Alayan
188188
Ted Xiao
189189
Thomas Grainger
190190
Thomas Hisch
191+
Tim Strazny
191192
Tom Dalton
192193
Tom Viner
193194
Trevor Bekolay

_pytest/python_api.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import sys
33

44
import py
5+
from six import binary_type, text_type
56
from six.moves import zip, filterfalse
67
from more_itertools.more import always_iterable
78

@@ -584,7 +585,8 @@ def raises(expected_exception, *args, **kwargs):
584585
585586
"""
586587
__tracebackhide__ = True
587-
for exc in filterfalse(isclass, always_iterable(expected_exception)):
588+
base_type = (type, text_type, binary_type)
589+
for exc in filterfalse(isclass, always_iterable(expected_exception, base_type)):
588590
msg = ("exceptions must be old-style classes or"
589591
" derived from BaseException, not %s")
590592
raise TypeError(msg % type(exc))

changelog/3372.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``pytest.raises`` now works with exception classes that look like iterables.

testing/python/raises.py

+18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from _pytest.outcomes import Failed
12
import pytest
23
import sys
34

@@ -147,3 +148,20 @@ def test_raises_match_wrong_type(self):
147148
with pytest.raises(ValueError):
148149
with pytest.raises(IndexError, match='nomatch'):
149150
int('asdf')
151+
152+
def test_raises_exception_looks_iterable(self):
153+
from six import add_metaclass
154+
155+
class Meta(type(object)):
156+
def __getitem__(self, item):
157+
return 1/0
158+
159+
def __len__(self):
160+
return 1
161+
162+
@add_metaclass(Meta)
163+
class ClassLooksIterableException(Exception):
164+
pass
165+
166+
with pytest.raises(Failed, match="DID NOT RAISE <class 'raises.ClassLooksIterableException'>"):
167+
pytest.raises(ClassLooksIterableException, lambda: None)

0 commit comments

Comments
 (0)