Merge pull request #3373 from backbord/master

Fix issue #3372
This commit is contained in:
Bruno Oliveira 2018-04-06 22:10:57 -03:00 committed by GitHub
commit e012dbe346
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 1 deletions

View File

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

View File

@ -2,6 +2,7 @@ import math
import sys
import py
from six import binary_type, text_type
from six.moves import zip, filterfalse
from more_itertools.more import always_iterable
@ -584,7 +585,8 @@ def raises(expected_exception, *args, **kwargs):
"""
__tracebackhide__ = True
for exc in filterfalse(isclass, always_iterable(expected_exception)):
base_type = (type, text_type, binary_type)
for exc in filterfalse(isclass, always_iterable(expected_exception, base_type)):
msg = ("exceptions must be old-style classes or"
" derived from BaseException, not %s")
raise TypeError(msg % type(exc))

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 sys
@ -147,3 +148,20 @@ class TestRaises(object):
with pytest.raises(ValueError):
with pytest.raises(IndexError, match='nomatch'):
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)