summaryrefslogtreecommitdiffstats
path: root/Lib/unittest/test
diff options
context:
space:
mode:
authorMichael Foord <fuzzyman@voidspace.org.uk>2010-03-27 19:10:11 (GMT)
committerMichael Foord <fuzzyman@voidspace.org.uk>2010-03-27 19:10:11 (GMT)
commita7e08fe3f88d116622fb0508e88f5af9f1ac2d84 (patch)
tree8779446626b6b1fdd8467bd09c42124d2fa7820e /Lib/unittest/test
parentb35ecf47755b23be95657df7d80fe5fcfe261671 (diff)
downloadcpython-a7e08fe3f88d116622fb0508e88f5af9f1ac2d84.zip
cpython-a7e08fe3f88d116622fb0508e88f5af9f1ac2d84.tar.gz
cpython-a7e08fe3f88d116622fb0508e88f5af9f1ac2d84.tar.bz2
Addition of delta keyword argument to unittest.TestCase.assertAlmostEquals and assertNotAlmostEquals
This allows the comparison of objects by specifying a maximum difference; this includes the comparing of non-numeric objects that don't support rounding.
Diffstat (limited to 'Lib/unittest/test')
-rw-r--r--Lib/unittest/test/test_assertions.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/Lib/unittest/test/test_assertions.py b/Lib/unittest/test/test_assertions.py
index 4b4e03a..5d77ce8 100644
--- a/Lib/unittest/test/test_assertions.py
+++ b/Lib/unittest/test/test_assertions.py
@@ -1,3 +1,5 @@
+import datetime
+
import unittest
@@ -25,6 +27,28 @@ class Test_Assertions(unittest.TestCase):
self.assertRaises(self.failureException, self.assertNotAlmostEqual,
float('inf'), float('inf'))
+ def test_AmostEqualWithDelta(self):
+ self.assertAlmostEqual(1.1, 1.0, delta=0.5)
+ self.assertAlmostEqual(1.0, 1.1, delta=0.5)
+ self.assertNotAlmostEqual(1.1, 1.0, delta=0.05)
+ self.assertNotAlmostEqual(1.0, 1.1, delta=0.05)
+
+ self.assertRaises(self.failureException, self.assertAlmostEqual,
+ 1.1, 1.0, delta=0.05)
+ self.assertRaises(self.failureException, self.assertNotAlmostEqual,
+ 1.1, 1.0, delta=0.5)
+
+ self.assertRaises(TypeError, self.assertAlmostEqual,
+ 1.1, 1.0, places=2, delta=2)
+ self.assertRaises(TypeError, self.assertNotAlmostEqual,
+ 1.1, 1.0, places=2, delta=2)
+
+ first = datetime.datetime.now()
+ second = first + datetime.timedelta(seconds=10)
+ self.assertAlmostEqual(first, second,
+ delta=datetime.timedelta(seconds=20))
+ self.assertNotAlmostEqual(first, second,
+ delta=datetime.timedelta(seconds=5))
def test_assertRaises(self):
def _raise(e):