Index: Doc/library/unittest.rst =================================================================== --- Doc/library/unittest.rst (Revision 75173) +++ Doc/library/unittest.rst (Arbeitskopie) @@ -955,6 +955,22 @@ .. versionadded:: 2.7 + .. method:: assertIsInstance(obj, cls[, msg]) + + This signals a test failure if *obj* is not an instance of *cls* (which + can be a class or a tuple of classes, as supported by :func:`isinstance`). + + .. versionadded:: 2.7 + + + .. method:: assertNotIsInstance(obj, cls[, msg]) + + The inverse of the :meth:`assertIsInstance` method. This signals a test + failure if *obj* is an instance of *cls*. + + .. versionadded:: 2.7 + + .. method:: assertFalse(expr[, msg]) failIf(expr[, msg]) Index: Lib/unittest/case.py =================================================================== --- Lib/unittest/case.py (Revision 75175) +++ Lib/unittest/case.py (Arbeitskopie) @@ -817,6 +817,19 @@ standardMsg = 'unexpectedly None' self.fail(self._formatMessage(msg, standardMsg)) + def assertIsInstance(self, obj, cls, msg=None): + """Same as self.assertTrue(isinstance(obj, cls)), with a nicer + default message.""" + if not isinstance(obj, cls): + standardMsg = '%r is not an instance of %r' % (obj, cls) + self.fail(self._formatMessage(msg, standardMsg)) + + def assertNotIsInstance(self, obj, cls, msg=None): + """Included for symmetry with assertIsInstance.""" + if isinstance(obj, cls): + standardMsg = '%r is an instance of %r' % (obj, cls) + self.fail(self._formatMessage(msg, standardMsg)) + def assertRaisesRegexp(self, expected_exception, expected_regexp, callable_obj=None, *args, **kwargs): """Asserts that the message in a raised exception matches a regexp. Index: Lib/test/test_unittest.py =================================================================== --- Lib/test/test_unittest.py (Revision 75173) +++ Lib/test/test_unittest.py (Arbeitskopie) @@ -2500,6 +2500,18 @@ self.assertIsNot(thing, object()) self.assertRaises(self.failureException, self.assertIsNot, thing, thing) + def testAssertIsInstance(self): + thing = [] + self.assertIsInstance(thing, list) + self.assertRaises(self.failureException, self.assertIsInstance, + thing, dict) + + def testAssertNotIsInstance(self): + thing = [] + self.assertNotIsInstance(thing, dict) + self.assertRaises(self.failureException, self.assertNotIsInstance, + thing, list) + def testAssertIn(self): animals = {'monkey': 'banana', 'cow': 'grass', 'seal': 'fish'}