mirror of
				https://github.com/python/cpython.git
				synced 2025-10-30 21:21:22 +00:00 
			
		
		
		
	Objects that compare equal automatically pass or fail assertAlmostEqual and assertNotAlmostEqual tests on unittest.TestCase. Issue 6567.
This commit is contained in:
		
							parent
							
								
									60931a5a58
								
							
						
					
					
						commit
						c3f79373e8
					
				
					 4 changed files with 24 additions and 1 deletions
				
			
		|  | @ -733,6 +733,9 @@ Test cases | ||||||
|       compare equal, the test will fail with the explanation given by *msg*, or |       compare equal, the test will fail with the explanation given by *msg*, or | ||||||
|       :const:`None`. |       :const:`None`. | ||||||
| 
 | 
 | ||||||
|  |       .. versionchanged:: 2.7 | ||||||
|  |          Objects that compare equal are automatically almost equal. | ||||||
|  | 
 | ||||||
|       .. deprecated:: 2.7 |       .. deprecated:: 2.7 | ||||||
|          :meth:`failUnlessAlmostEqual`. |          :meth:`failUnlessAlmostEqual`. | ||||||
| 
 | 
 | ||||||
|  | @ -749,6 +752,9 @@ Test cases | ||||||
|       compare equal, the test will fail with the explanation given by *msg*, or |       compare equal, the test will fail with the explanation given by *msg*, or | ||||||
|       :const:`None`. |       :const:`None`. | ||||||
| 
 | 
 | ||||||
|  |       .. versionchanged:: 2.7 | ||||||
|  |          Objects that compare equal automatically fail. | ||||||
|  | 
 | ||||||
|       .. deprecated:: 2.7 |       .. deprecated:: 2.7 | ||||||
|          :meth:`failIfAlmostEqual`. |          :meth:`failIfAlmostEqual`. | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -505,6 +505,10 @@ changes, or look through the Subversion logs for all the details. | ||||||
|     differences.  :meth:`assertDictContainsSubset` checks whether |     differences.  :meth:`assertDictContainsSubset` checks whether | ||||||
|     all of the key/value pairs in *first* are found in *second*. |     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 |   * A new hook, :meth:`addTypeEqualityFunc` takes a type object and a | ||||||
|     function.  The :meth:`assertEqual` method will use the function |     function.  The :meth:`assertEqual` method will use the function | ||||||
|     when both of the objects being compared are of the specified type. |     when both of the objects being compared are of the specified type. | ||||||
|  |  | ||||||
|  | @ -2989,6 +2989,11 @@ def test_AlmostEqual(self): | ||||||
|         self.assertRaises(self.failureException, |         self.assertRaises(self.failureException, | ||||||
|                           self.assertNotAlmostEqual, 0, .1+.1j, places=0) |                           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 test_assertRaises(self): | ||||||
|         def _raise(e): |         def _raise(e): | ||||||
|             raise e |             raise e | ||||||
|  |  | ||||||
|  | @ -457,7 +457,13 @@ def assertAlmostEqual(self, first, second, places=7, msg=None): | ||||||
| 
 | 
 | ||||||
|            Note that decimal places (from zero) are usually not the same |            Note that decimal places (from zero) are usually not the same | ||||||
|            as significant digits (measured from the most signficant digit). |            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: |         if round(abs(second-first), places) != 0: | ||||||
|             standardMsg = '%r != %r within %r places' % (first, second, places) |             standardMsg = '%r != %r within %r places' % (first, second, places) | ||||||
|             msg = self._formatMessage(msg, standardMsg) |             msg = self._formatMessage(msg, standardMsg) | ||||||
|  | @ -470,8 +476,10 @@ def assertNotAlmostEqual(self, first, second, places=7, msg=None): | ||||||
| 
 | 
 | ||||||
|            Note that decimal places (from zero) are usually not the same |            Note that decimal places (from zero) are usually not the same | ||||||
|            as significant digits (measured from the most signficant digit). |            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) |             standardMsg = '%r == %r within %r places' % (first, second, places) | ||||||
|             msg = self._formatMessage(msg, standardMsg) |             msg = self._formatMessage(msg, standardMsg) | ||||||
|             raise self.failureException(msg) |             raise self.failureException(msg) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Michael Foord
						Michael Foord