Skip to content
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

Fix issue #3372 #3373

Merged
merged 4 commits into from
Apr 7, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ Tareq Alayan
Ted Xiao
Thomas Grainger
Thomas Hisch
Tim Strazny
Tom Dalton
Tom Viner
Trevor Bekolay
Expand Down
6 changes: 4 additions & 2 deletions _pytest/python_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys

import py
from six import binary_type, text_type
from six.moves import zip, filterfalse
from more_itertools.more import always_iterable

Expand Down Expand Up @@ -584,9 +585,10 @@ 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")
" derived from BaseException, not %s")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one is the unfortunate dedent mistake that broke the linting job ^^

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh man. thanks for the pointer

raise TypeError(msg % type(exc))

message = "DID NOT RAISE {0}".format(expected_exception)
Expand Down
1 change: 1 addition & 0 deletions changelog/3372.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``pytest.raises`` now works with exception classes that look like iterables.
18 changes: 18 additions & 0 deletions testing/python/raises.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from _pytest.outcomes import Failed
import pytest
import sys

Expand Down Expand Up @@ -147,3 +148,20 @@ def test_raises_match_wrong_type(self):
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)