diff options
author | Jeffrey Yasskin <jyasskin@gmail.com> | 2007-09-07 15:00:39 (GMT) |
---|---|---|
committer | Jeffrey Yasskin <jyasskin@gmail.com> | 2007-09-07 15:00:39 (GMT) |
commit | aaaef110dcb58ded6257512997ddf270828dc409 (patch) | |
tree | e530f013a6999837ec749ea6496c6ca639caf8b0 | |
parent | 96593ed348078403eb30fd5d2767fc96c17e913a (diff) | |
download | cpython-aaaef110dcb58ded6257512997ddf270828dc409.zip cpython-aaaef110dcb58ded6257512997ddf270828dc409.tar.gz cpython-aaaef110dcb58ded6257512997ddf270828dc409.tar.bz2 |
Add a test for fail*AlmostEqual, including the new support for complex numbers.
-rw-r--r-- | Lib/test/test_unittest.py | 23 | ||||
-rw-r--r-- | Lib/unittest.py | 4 |
2 files changed, 24 insertions, 3 deletions
diff --git a/Lib/test/test_unittest.py b/Lib/test/test_unittest.py index 944ab22..9b43f45 100644 --- a/Lib/test/test_unittest.py +++ b/Lib/test/test_unittest.py @@ -2292,13 +2292,34 @@ class Test_TestCase(TestCase, TestEquality, TestHashing): expected = ['startTest', 'test', 'stopTest'] self.assertEqual(events, expected) +class Test_Assertions(TestCase): + def test_AlmostEqual(self): + self.failUnlessAlmostEqual(1.00000001, 1.0) + self.failIfAlmostEqual(1.0000001, 1.0) + self.assertRaises(AssertionError, + self.failUnlessAlmostEqual, 1.0000001, 1.0) + self.assertRaises(AssertionError, + self.failIfAlmostEqual, 1.00000001, 1.0) + + self.failUnlessAlmostEqual(1.1, 1.0, places=0) + self.assertRaises(AssertionError, + self.failUnlessAlmostEqual, 1.1, 1.0, places=1) + + self.failUnlessAlmostEqual(0, .1+.1j, places=0) + self.failIfAlmostEqual(0, .1+.1j, places=1) + self.assertRaises(AssertionError, + self.failUnlessAlmostEqual, 0, .1+.1j, places=1) + self.assertRaises(AssertionError, + self.failIfAlmostEqual, 0, .1+.1j, places=0) + ###################################################################### ## Main ###################################################################### def test_main(): test_support.run_unittest(Test_TestCase, Test_TestLoader, - Test_TestSuite, Test_TestResult, Test_FunctionTestCase) + Test_TestSuite, Test_TestResult, Test_FunctionTestCase, + Test_Assertions) if __name__ == "__main__": test_main() diff --git a/Lib/unittest.py b/Lib/unittest.py index a023c67..bd72d90 100644 --- a/Lib/unittest.py +++ b/Lib/unittest.py @@ -331,7 +331,7 @@ class TestCase: if first == second: raise self.failureException(msg or '%r == %r' % (first, second)) - def failUnlessAlmostEqual(self, first, second, places=7, msg=None): + def failUnlessAlmostEqual(self, first, second, *, places=7, msg=None): """Fail if the two objects are unequal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero. @@ -343,7 +343,7 @@ class TestCase: raise self.failureException(msg or '%r != %r within %r places' % (first, second, places)) - def failIfAlmostEqual(self, first, second, places=7, msg=None): + def failIfAlmostEqual(self, first, second, *, places=7, msg=None): """Fail if the two objects are equal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero. |