diff options
author | Raymond Hettinger <python@rcn.com> | 2002-12-29 17:59:24 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2002-12-29 17:59:24 (GMT) |
commit | c7b076928c8d3316f6426a49c9d33ce14c0eaaee (patch) | |
tree | cc48c7819d78b3952db20fe0cf805f773a4b8602 /Lib | |
parent | 3c736f1a415fd87fece57756a836f5e06ff2af73 (diff) | |
download | cpython-c7b076928c8d3316f6426a49c9d33ce14c0eaaee.zip cpython-c7b076928c8d3316f6426a49c9d33ce14c0eaaee.tar.gz cpython-c7b076928c8d3316f6426a49c9d33ce14c0eaaee.tar.bz2 |
Incorporate Skip's suggestion to use SciPy's validation test near
equality. Note, there is another flavor that compares to a given
number of significant digits rather than decimal places. If there
is a demand, that could be added at a later date.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/unittest.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/Lib/unittest.py b/Lib/unittest.py index 9100a78..31d9cb4 100644 --- a/Lib/unittest.py +++ b/Lib/unittest.py @@ -300,10 +300,38 @@ class TestCase: raise self.failureException, \ (msg or '%s == %s' % (`first`, `second`)) + 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. + + Note that decimal places (from zero) is usually not the same + as significant digits (measured from the most signficant digit). + """ + if round(second-first, places) != 0: + raise self.failureException, \ + (msg or '%s != %s within %s places' % (`first`, `second`, `places` )) + + 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. + + Note that decimal places (from zero) is usually not the same + as significant digits (measured from the most signficant digit). + """ + if round(second-first, places) == 0: + raise self.failureException, \ + (msg or '%s == %s within %s places' % (`first`, `second`, `places`)) + assertEqual = assertEquals = failUnlessEqual assertNotEqual = assertNotEquals = failIfEqual + assertAlmostEqual = assertAlmostEquals = failUnlessAlmostEqual + + assertNotAlmostEqual = assertNotAlmostEquals = failIfAlmostEqual + assertRaises = failUnlessRaises assert_ = failUnless |