[Pyunit-interest] assertEquals with a delta?
Brought to you by:
purcell
From: Luke D. <and...@sh...> - 2001-12-31 21:01:22
|
hi there, well i have taken the plunge and started teaching myself python, along the way i came across pyunit... can i recommend that we change assertEquals to take a delta parameter? the reason for this is so an approximate value will not fail the assert. eg. writing a unittest that tests the area of a circle, you can set up a 'list' of known values consisting of radius and actual area ie. (1, 3.1415927), (2,12.566371).... using the current implementation of assertEquals the provided area's would fail the assertEquals but if you use assertEquals(result, actual, 0.0005) where 0.0005 is the delta and in this case is approximate enough to pass and assertEquals test. give the delta parameter a default value of 0 and AssertEquals will be consistent with its current behaviour if you choose not to use a delta etc. this means we redefine failUnlessEqual inside pyunit.py to def failUnlessEqual(self, first, second, delta=0, msg=None): """Fail if the two objects are unequal as determined by the difference between the actual value and the expected value. a delta can be used to assert equal on approx values. """ """if first != second: raise self.failureException, (msg or '%s != %s' % (first, second)) """ if abs(first - second) > delta: raise self.failureException, (msg or '%s != %s' % (first, second)) make sense, hope so? thanks, luke |