Skip to content

Add assertRaises context manager to testutils #526

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

Merged
merged 1 commit into from
Feb 23, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 5 additions & 21 deletions tests/snippets/set.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from testutils import assert_raises
from testutils import assert_raises, assertRaises

assert set([1,2]) == set([1,2])
assert not set([1,2,3]) == set([1,2])
Expand Down Expand Up @@ -109,12 +109,8 @@ def __hash__(self):
a = set([1,2,3])
a |= set([3,4,5])
assert a == set([1,2,3,4,5])
try:
with assertRaises(TypeError):
a |= 1
except TypeError:
pass
else:
assert False, "TypeError not raised"

a = set([1,2,3])
a.intersection_update([2,3,4,5])
Expand All @@ -124,12 +120,8 @@ def __hash__(self):
a = set([1,2,3])
a &= set([2,3,4,5])
assert a == set([2,3])
try:
with assertRaises(TypeError):
a &= 1
except TypeError:
pass
else:
assert False, "TypeError not raised"

a = set([1,2,3])
a.difference_update([3,4,5])
Expand All @@ -139,12 +131,8 @@ def __hash__(self):
a = set([1,2,3])
a -= set([3,4,5])
assert a == set([1,2])
try:
with assertRaises(TypeError):
a -= 1
except TypeError:
pass
else:
assert False, "TypeError not raised"

a = set([1,2,3])
a.symmetric_difference_update([3,4,5])
Expand All @@ -154,9 +142,5 @@ def __hash__(self):
a = set([1,2,3])
a ^= set([3,4,5])
assert a == set([1,2,4,5])
try:
with assertRaises(TypeError):
a ^= 1
except TypeError:
pass
else:
assert False, "TypeError not raised"
16 changes: 16 additions & 0 deletions tests/snippets/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,19 @@ def assert_raises(exc_type, expr, msg=None):
if msg is not None:
failmsg += ': {!s}'.format(msg)
assert False, failmsg


class assertRaises:
def __init__(self, expected):
self.expected = expected

def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
if exc_type is None:
failmsg = '{!s} was not raised'.format(self.expected.__name_)
assert False, failmsg
if not issubclass(exc_type, self.expected):
return False
return True