]> granicus.if.org Git - python/commitdiff
Objects that compare equal automatically pass or fail assertAlmostEqual and assertNot...
authorMichael Foord <fuzzyman@voidspace.org.uk>
Sun, 13 Sep 2009 16:40:02 +0000 (16:40 +0000)
committerMichael Foord <fuzzyman@voidspace.org.uk>
Sun, 13 Sep 2009 16:40:02 +0000 (16:40 +0000)
Doc/library/unittest.rst
Doc/whatsnew/2.7.rst
Lib/test/test_unittest.py
Lib/unittest/case.py

index a2d4478a02b37e5d7e74c42d04bdd87a072860d5..61c4308b0af09916ad3ebe28aa6043ce023225a4 100644 (file)
@@ -733,6 +733,9 @@ Test cases
       compare equal, the test will fail with the explanation given by *msg*, or
       :const:`None`.
 
+      .. versionchanged:: 2.7
+         Objects that compare equal are automatically almost equal.
+
       .. deprecated:: 2.7
          :meth:`failUnlessAlmostEqual`.
 
@@ -749,6 +752,9 @@ Test cases
       compare equal, the test will fail with the explanation given by *msg*, or
       :const:`None`.
 
+      .. versionchanged:: 2.7
+         Objects that compare equal automatically fail.
+
       .. deprecated:: 2.7
          :meth:`failIfAlmostEqual`.
 
index 9a3b3e67acc49b52688de321a30b0561ab7e4afe..6089d33f544f8bfaf1e3abf58840bb0dce896330 100644 (file)
@@ -505,6 +505,10 @@ changes, or look through the Subversion logs for all the details.
     differences.  :meth:`assertDictContainsSubset` checks whether
     all of the key/value pairs in *first* are found in *second*.
 
+  * :meth:`assertAlmostEqual` and :meth:`assertNotAlmostEqual` short-circuit
+    (automatically pass or fail without checking decimal places) if the objects
+    are equal.
+
   * A new hook, :meth:`addTypeEqualityFunc` takes a type object and a
     function.  The :meth:`assertEqual` method will use the function
     when both of the objects being compared are of the specified type.
index 5c3921dfd984f58a867af0af3ba4834c57248929..2fab7de9796acd1c9c179ea547becd2090af996e 100644 (file)
@@ -2989,6 +2989,11 @@ class Test_Assertions(TestCase):
         self.assertRaises(self.failureException,
                           self.assertNotAlmostEqual, 0, .1+.1j, places=0)
 
+        self.assertAlmostEqual(float('inf'), float('inf'))
+        self.assertRaises(self.failureException, self.assertNotAlmostEqual,
+                          float('inf'), float('inf'))
+
+
     def test_assertRaises(self):
         def _raise(e):
             raise e
index d52bc8d8505573f7f98bccfc41d9445c0c02c839..cac28421bf7c70a51ec87f419e5eeb314fa9da21 100644 (file)
@@ -457,7 +457,13 @@ class TestCase(object):
 
            Note that decimal places (from zero) are usually not the same
            as significant digits (measured from the most signficant digit).
+
+           If the two objects compare equal then they will automatically
+           compare almost equal.
         """
+        if first == second:
+            # shortcut for ite
+            return
         if round(abs(second-first), places) != 0:
             standardMsg = '%r != %r within %r places' % (first, second, places)
             msg = self._formatMessage(msg, standardMsg)
@@ -470,8 +476,10 @@ class TestCase(object):
 
            Note that decimal places (from zero) are usually not the same
            as significant digits (measured from the most signficant digit).
+
+           Objects that are equal automatically fail.
         """
-        if round(abs(second-first), places) == 0:
+        if (first == second) or round(abs(second-first), places) == 0:
             standardMsg = '%r == %r within %r places' % (first, second, places)
             msg = self._formatMessage(msg, standardMsg)
             raise self.failureException(msg)