[New-bugs-announce] [issue27198] Adding an assertClose() method to unittest.TestCase

Chris Barker report at bugs.python.org
Fri Jun 3 14:48:32 EDT 2016


New submission from Chris Barker:

In py3.5, the math.isclose() function was added to the standard library. It can be used to compare floating point numbers to see if they are close to each other, rather than exactly equal. It's not a lot of code, but there are nuances that not every python user should need to understand.

One of the major use cases for isclose() is test code, so it would be good to make it easily available in unitest.TestCase.

TestCase.assertAlmostEqual is NOT the same thing, and can only be used properly for values near 1.

Enclosed is a patch that adds  assertClose and assertNotClose to unittest, as well as tests and additions to the docs.

Still pending: should this support complex numbers, there is a cmath.isclose(). If so, but switching on type?

Also -- should this be back-ported to 3.5 as well -- math.isclose() was introduced there.

Done in the pyCon sprints.

----------
components: Library (Lib)
files: assertClose.patch
keywords: patch
messages: 267133
nosy: ChrisBarker, ezio.melotti, michael.foord, rbcollins
priority: normal
severity: normal
status: open
title: Adding an assertClose() method to unittest.TestCase
type: enhancement
versions: Python 3.6
Added file: http://bugs.python.org/file43157/assertClose.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue27198>
_______________________________________


More information about the New-bugs-announce mailing list