[Python-Dev] Optional delta argument for assertAlmostEqual

geremy condra debatem1 at gmail.com
Sun Mar 28 20:09:39 CEST 2010


On Fri, Mar 26, 2010 at 8:59 PM, Michael Foord
<fuzzyman at voidspace.org.uk> wrote:
> Hello all,
>
> A user has suggested an optional argument to
> unittest.TestCase.assertAlmostEqual for specifying a maximum difference
> between the expected and actual values, instead of using rounding.
>
> This sounds great to me as the default implementation of assertAlmostEqual
> has *never* been useful to me (YMMV). In fact one of the first things I do
> on setting up a test suite is provide a TestCase that overrides
> assertAlmostEqual with an implementation that uses a delta rather than
> rounding.
>
> The implementation would be effectively:
>
> assert abs(actual - expected) < delta
>
> This has the advantage that it allows things like:
>
> self.assertAlmostEqual(timeStamp, expected,
> delta=datetime.timedelta(seconds=5))
>
> The issue is this would make the signature of assertAlmostEqual (and its
> negative counterpart):
>
> def assertAlmostEqual(self, first, second, places=7, msg=None, delta=None)
>
> Note that delta comes after msg, which is different to other assert methods.
> To put delta before msg would be backwards incompatible with existing uses
> passing arguments positionally. In Python 3.2 we can make delta a keyword
> argument. Passing both places and delta would be an error (TypeError).
>
> Anyway, unless there are strenuous objections I intend to do this.
>
> All the best,
>
> Michael

+1, this would be very helpful to me.

Geremy Condra


More information about the Python-Dev mailing list