Merge pull request #34 from djipko/fix_assertraises_metaclasses

Fix assertRaises when the exception has a metaclass
This commit is contained in:
Jonathan Lange
2013-03-26 07:44:18 -07:00
4 changed files with 19 additions and 1 deletions

View File

@@ -17,6 +17,7 @@ The testtools authors are:
* Gavin Panella
* Martin Pool
* Vincent Ladeuil
* Nikola Đipanov
and are collectively referred to as "testtools developers".

2
NEWS
View File

@@ -13,6 +13,8 @@ experimental and we might need to break it.
Improvements
------------
* ``assertRaises`` works properly for exception classes that have custom
metaclasses
* ``ConcurrentTestSuite`` was silently eating exceptions that propagate from
the test.run(result) method call. Ignoring them is fine in a normal test

View File

@@ -44,7 +44,9 @@ class MatchesException(Matcher):
if istext(value_re):
value_re = AfterPreproccessing(str, MatchesRegex(value_re), False)
self.value_re = value_re
self._is_instance = type(self.expected) not in classtypes() + (tuple,)
expected_type = type(self.expected)
self._is_instance = not any(issubclass(expected_type, class_type)
for class_type in classtypes() + (tuple,))
def match(self, other):
if type(other) != tuple:

View File

@@ -302,6 +302,19 @@ class TestAssertions(TestCase):
# assertRaises asserts that a callable raises a particular exception.
self.assertRaises(RuntimeError, self.raiseError, RuntimeError)
def test_assertRaises_exception_w_metaclass(self):
# assertRaises works when called for exceptions with custom metaclasses
class MyExMeta(type):
def __init__(cls, name, bases, dct):
""" Do some dummy metaclass stuff """
dct.update({'answer': 42})
type.__init__(cls, name, bases, dct)
class MyEx(Exception):
__metaclass__ = MyExMeta
self.assertRaises(MyEx, self.raiseError, MyEx)
def test_assertRaises_fails_when_no_error_raised(self):
# assertRaises raises self.failureException when it's passed a
# callable that raises no error.