diff -r 4e9eabe3e24f Doc/library/unittest.rst --- a/Doc/library/unittest.rst Tue May 15 20:04:25 2012 +0200 +++ b/Doc/library/unittest.rst Tue May 15 17:12:17 2012 -0400 @@ -859,6 +859,12 @@ | :meth:`assertNotIsInstance(a, b) | ``not isinstance(a, b)`` | 3.2 | | ` | | | +-----------------------------------------+-----------------------------+---------------+ + | :meth:`assertIsSubclass(a, b) | ``issubclass(a, b)`` | 3.3 | + | ` | | | + +-----------------------------------------+-----------------------------+---------------+ + | :meth:`assertNotIsSubclass(a, b) | ``not issubclass(a, b)`` | 3.3 | + | ` | | | + +-----------------------------------------+-----------------------------+---------------+ All the assert methods accept a *msg* argument that, if specified, is used as the error message on failure (see also :data:`longMessage`). @@ -938,6 +944,15 @@ .. versionadded:: 3.2 + .. method:: assertIsSubclass(cls, parent_cls, msg=None) + assertNotIsSubclass(cls, parent_cls, msg=None) + + Test that *cls* is (or is not) a subclass of *parent_cls* (which can be a + class or a tuple of classes, as supported by :func:`issubclass`). + + .. versionadded:: 3.3 + + It is also possible to check that exceptions and warnings are raised using the following methods: diff -r 4e9eabe3e24f Lib/unittest/case.py --- a/Lib/unittest/case.py Tue May 15 20:04:25 2012 +0200 +++ b/Lib/unittest/case.py Tue May 15 17:12:17 2012 -0400 @@ -1069,6 +1069,19 @@ standardMsg = '%s is an instance of %r' % (safe_repr(obj), cls) self.fail(self._formatMessage(msg, standardMsg)) + def assertIsSubclass(self, cls, parent_cls, msg=None): + """Same as self.assertTrue(issubclass(cls, parent_cls)), with a nicer + default message.""" + if not issubclass(cls, parent_cls): + standardMsg = '%r is not a subclass of %r' % (cls, parent_cls) + self.fail(self._formatMessage(msg, standardMsg)) + + def assertNotIsSubclass(self, cls, parent_cls, msg=None): + """Included for symmetry with assertIsSubclass.""" + if issubclass(cls, parent_cls): + standardMsg = '%r is a subclass of %r' % (cls, parent_cls) + self.fail(self._formatMessage(msg, standardMsg)) + def assertRaisesRegex(self, expected_exception, expected_regex, callable_obj=None, *args, **kwargs): """Asserts that the message in a raised exception matches a regex. diff -r 4e9eabe3e24f Lib/unittest/test/test_case.py --- a/Lib/unittest/test/test_case.py Tue May 15 20:04:25 2012 +0200 +++ b/Lib/unittest/test/test_case.py Tue May 15 17:12:17 2012 -0400 @@ -503,6 +503,30 @@ self.assertRaises(self.failureException, self.assertNotIsInstance, thing, list) + def testAssertIsSubclass(self): + class Foo(object): + pass + class Bar(Foo): + pass + class Baz(object): + pass + + self.assertIsSubclass(Bar, Foo) + self.assertRaises(self.failureException, self.assertIsSubclass, + Bar, Baz) + + def testAssertNotIsSubclass(self): + class Foo(object): + pass + class Bar(Foo): + pass + class Baz(object): + pass + + self.assertNotIsSubclass(Bar, Baz) + self.assertRaises(self.failureException, self.assertNotIsSubclass, + Bar, Foo) + def testAssertIn(self): animals = {'monkey': 'banana', 'cow': 'grass', 'seal': 'fish'} diff -r 4e9eabe3e24f Misc/ACKS --- a/Misc/ACKS Tue May 15 20:04:25 2012 +0200 +++ b/Misc/ACKS Tue May 15 17:12:17 2012 -0400 @@ -959,6 +959,7 @@ Eric V. Smith Gregory P. Smith Mark Smith +Patrick Smith Rafal Smotrzyk Eric Snow Dirk Soede