Index: Doc/library/unittest.rst =================================================================== --- Doc/library/unittest.rst (revision 71118) +++ Doc/library/unittest.rst (working copy) @@ -859,6 +859,23 @@ .. versionadded:: 2.7 + .. method:: assertIs(expr1, expr2[, msg]) + + This signals a test failure if *expr1* and *expr2* don't evaluate to the same + object. + + .. versionadded:: 2.7 + + + .. method:: assertIsNotNone(expr1, expr2[, msg]) + + The inverse of the :meth:`assertIs` method. + This signals a test failure if *expr1* and *expr2* evaluate to the same + object. + + .. versionadded:: 2.7 + + .. method:: assertFalse(expr[, msg]) failIf(expr[, msg]) Index: Lib/unittest.py =================================================================== --- Lib/unittest.py (revision 71118) +++ Lib/unittest.py (working copy) @@ -806,6 +806,18 @@ standardMsg = '%r unexpectedly found in %r' % (member, container) self.fail(self._formatMessage(msg, standardMsg)) + def assertIs(self, expr1, expr2, msg=None): + """Just like self.assertTrue(a is b), but with a nicer default message.""" + if expr1 is not expr2: + standardMsg = '%r is not %r' % (expr1, expr2) + self.fail(self._formatMessage(msg, standardMsg)) + + def assertIsNot(self, expr1, expr2, msg=None): + """Just like self.assertTrue(a is not b), but with a nicer default message.""" + if expr1 is expr2: + standardMsg = 'unexpectedly identical: %r' % (expr1,) + self.fail(self._formatMessage(msg, standardMsg)) + def assertDictEqual(self, d1, d2, msg=None): self.assert_(isinstance(d1, dict), 'First argument is not a dictionary') self.assert_(isinstance(d2, dict), 'Second argument is not a dictionary') Index: Lib/test/test_unittest.py =================================================================== --- Lib/test/test_unittest.py (revision 71118) +++ Lib/test/test_unittest.py (working copy) @@ -2300,7 +2300,17 @@ # No this doesn't clean up and remove the SadSnake equality func # from this TestCase instance but since its a local nothing else # will ever notice that. - + + def testAssertIs(self): + thing = object() + self.assertIs(thing, thing) + self.assertRaises(self.failureException, self.assertIs, thing, object()) + + def testAssertIsNot(self): + thing = object() + self.assertIsNot(thing, object()) + self.assertRaises(self.failureException, self.assertIsNot, thing, thing) + def testAssertIn(self): animals = {'monkey': 'banana', 'cow': 'grass', 'seal': 'fish'} @@ -2444,6 +2454,7 @@ # Test that sequences of unhashable objects can be tested for sameness: self.assertSameElements([[1, 2], [3, 4]], [[3, 4], [1, 2]]) + self.assertSameElements([{'a': 1}, {'b': 2}], [{'b': 2}, {'a': 1}]) self.assertRaises(self.failureException, self.assertSameElements, [[1]], [[2]]) @@ -3015,6 +3026,18 @@ ["^unexpectedly None$", "^oops$", "^unexpectedly None$", "^unexpectedly None : oops$"]) + + def testAssertIs(self): + self.assertMessages('assertIs', (None, 'foo'), + ["^None is not 'foo'$", "^oops$", + "^None is not 'foo'$", + "^None is not 'foo' : oops$"]) + + def testAssertIsNot(self): + self.assertMessages('assertIsNot', (None, None), + ["^unexpectedly identical: None$", "^oops$", + "^unexpectedly identical: None$", + "^unexpectedly identical: None : oops$"]) ######################################################################