]> granicus.if.org Git - python/commitdiff
Adding assertIs and assertIsNot methods to unittest.TestCase
authorMichael Foord <fuzzyman@voidspace.org.uk>
Sun, 5 Apr 2009 19:19:28 +0000 (19:19 +0000)
committerMichael Foord <fuzzyman@voidspace.org.uk>
Sun, 5 Apr 2009 19:19:28 +0000 (19:19 +0000)
Issue #2578

Doc/library/unittest.rst
Lib/test/test_unittest.py
Lib/unittest.py
Misc/NEWS

index 38e4239a2c360f432f01ee9da7ac9429595bf542..4f4459cf411d5821e2df28ed2ac81ffb6777c4c3 100644 (file)
@@ -859,6 +859,23 @@ Test cases
       .. 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:: assertIsNot(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 38c4f8f41d0d70ca8d696e5ff2b88e1d9a3a59d4..f28b1af988b569c7a58e9a68ee62a3b69a8a2209 100644 (file)
@@ -2301,6 +2301,16 @@ class Test_TestCase(TestCase, TestEquality, TestHashing):
         # 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 @@ class Test_TestCase(TestCase, TestEquality, TestHashing):
 
         # 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]])
@@ -3016,6 +3027,18 @@ class TestLongMessage(TestCase):
                              "^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$"])
+
 
 ######################################################################
 ## Main
index 83790fce703bdc923ef6fd6248cbcce3ce7d725e..f99f958d913263254dc7d966e4d6074ae4f53cea 100644 (file)
@@ -806,6 +806,18 @@ class TestCase(object):
             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 2cfd2922b771c79cbfabebd7f19b31d4b58dd834..fb5351a95aedfc3146b9da51fdad96bda3b1544c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -212,6 +212,8 @@ Core and Builtins
 Library
 -------
 
+- Issue 5693: TestSuite.__iter__ can now be consistently overridden in subclasses.
+
 - Issue 5694: removed spurious test output in Distutils (test_clean).
 
 - Issue 5471: Fix os.path.expanduser() for $HOME set to '/'.