[Python-checkins] r74780 - in python/trunk: Doc/library/unittest.rst Doc/whatsnew/2.7.rst Lib/test/test_unittest.py Lib/unittest/case.py

michael.foord python-checkins at python.org
Sun Sep 13 18:40:03 CEST 2009


Author: michael.foord
Date: Sun Sep 13 18:40:02 2009
New Revision: 74780

Log:
Objects that compare equal automatically pass or fail assertAlmostEqual and assertNotAlmostEqual tests on unittest.TestCase. Issue 6567.

Modified:
   python/trunk/Doc/library/unittest.rst
   python/trunk/Doc/whatsnew/2.7.rst
   python/trunk/Lib/test/test_unittest.py
   python/trunk/Lib/unittest/case.py

Modified: python/trunk/Doc/library/unittest.rst
==============================================================================
--- python/trunk/Doc/library/unittest.rst	(original)
+++ python/trunk/Doc/library/unittest.rst	Sun Sep 13 18:40:02 2009
@@ -733,6 +733,9 @@
       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 @@
       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`.
 

Modified: python/trunk/Doc/whatsnew/2.7.rst
==============================================================================
--- python/trunk/Doc/whatsnew/2.7.rst	(original)
+++ python/trunk/Doc/whatsnew/2.7.rst	Sun Sep 13 18:40:02 2009
@@ -505,6 +505,10 @@
     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.

Modified: python/trunk/Lib/test/test_unittest.py
==============================================================================
--- python/trunk/Lib/test/test_unittest.py	(original)
+++ python/trunk/Lib/test/test_unittest.py	Sun Sep 13 18:40:02 2009
@@ -2989,6 +2989,11 @@
         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

Modified: python/trunk/Lib/unittest/case.py
==============================================================================
--- python/trunk/Lib/unittest/case.py	(original)
+++ python/trunk/Lib/unittest/case.py	Sun Sep 13 18:40:02 2009
@@ -457,7 +457,13 @@
 
            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 @@
 
            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)


More information about the Python-checkins mailing list