Commit 5d7a8d0c authored by Giampaolo Rodola's avatar Giampaolo Rodola Committed by GitHub

bpo-30190: improved error msg for assertAlmostEqual(delta=...) (#1331)

* #30190 / unittest / assertAlmostEqual(delta=...) / error msg: show the difference between the 2 numbers in case of failure

* safe_repr() diff

* also show difference when passing 'places' argument

* refactoring

* update Misc/NEWS
parent 1374dbb6
...@@ -856,23 +856,28 @@ class TestCase(object): ...@@ -856,23 +856,28 @@ class TestCase(object):
if delta is not None and places is not None: if delta is not None and places is not None:
raise TypeError("specify delta or places not both") raise TypeError("specify delta or places not both")
diff = abs(first - second)
if delta is not None: if delta is not None:
if abs(first - second) <= delta: if diff <= delta:
return return
standardMsg = '%s != %s within %s delta' % (safe_repr(first), standardMsg = '%s != %s within %s delta (%s difference)' % (
safe_repr(second), safe_repr(first),
safe_repr(delta)) safe_repr(second),
safe_repr(delta),
safe_repr(diff))
else: else:
if places is None: if places is None:
places = 7 places = 7
if round(abs(second-first), places) == 0: if round(diff, places) == 0:
return return
standardMsg = '%s != %s within %r places' % (safe_repr(first), standardMsg = '%s != %s within %r places (%s difference)' % (
safe_repr(second), safe_repr(first),
places) safe_repr(second),
places,
safe_repr(diff))
msg = self._formatMessage(msg, standardMsg) msg = self._formatMessage(msg, standardMsg)
raise self.failureException(msg) raise self.failureException(msg)
...@@ -890,16 +895,19 @@ class TestCase(object): ...@@ -890,16 +895,19 @@ class TestCase(object):
""" """
if delta is not None and places is not None: if delta is not None and places is not None:
raise TypeError("specify delta or places not both") raise TypeError("specify delta or places not both")
diff = abs(first - second)
if delta is not None: if delta is not None:
if not (first == second) and abs(first - second) > delta: if not (first == second) and diff > delta:
return return
standardMsg = '%s == %s within %s delta' % (safe_repr(first), standardMsg = '%s == %s within %s delta (%s difference)' % (
safe_repr(second), safe_repr(first),
safe_repr(delta)) safe_repr(second),
safe_repr(delta),
safe_repr(diff))
else: else:
if places is None: if places is None:
places = 7 places = 7
if not (first == second) and round(abs(second-first), places) != 0: if not (first == second) and round(diff, places) != 0:
return return
standardMsg = '%s == %s within %r places' % (safe_repr(first), standardMsg = '%s == %s within %r places' % (safe_repr(first),
safe_repr(second), safe_repr(second),
...@@ -908,7 +916,6 @@ class TestCase(object): ...@@ -908,7 +916,6 @@ class TestCase(object):
msg = self._formatMessage(msg, standardMsg) msg = self._formatMessage(msg, standardMsg)
raise self.failureException(msg) raise self.failureException(msg)
def assertSequenceEqual(self, seq1, seq2, msg=None, seq_type=None): def assertSequenceEqual(self, seq1, seq2, msg=None, seq_type=None):
"""An equality assertion for ordered sequences (like lists and tuples). """An equality assertion for ordered sequences (like lists and tuples).
......
...@@ -223,9 +223,11 @@ class TestLongMessage(unittest.TestCase): ...@@ -223,9 +223,11 @@ class TestLongMessage(unittest.TestCase):
"^1 == 1 : oops$"]) "^1 == 1 : oops$"])
def testAlmostEqual(self): def testAlmostEqual(self):
self.assertMessages('assertAlmostEqual', (1, 2), self.assertMessages(
["^1 != 2 within 7 places$", "^oops$", 'assertAlmostEqual', (1, 2),
"^1 != 2 within 7 places$", "^1 != 2 within 7 places : oops$"]) ["^1 != 2 within 7 places \(1 difference\)$", "^oops$",
"^1 != 2 within 7 places \(1 difference\)$",
"^1 != 2 within 7 places \(1 difference\) : oops$"])
def testNotAlmostEqual(self): def testNotAlmostEqual(self):
self.assertMessages('assertNotAlmostEqual', (1, 1), self.assertMessages('assertNotAlmostEqual', (1, 1),
......
...@@ -10,7 +10,7 @@ What's New in Python 3.7.0 alpha 1? ...@@ -10,7 +10,7 @@ What's New in Python 3.7.0 alpha 1?
Core and Builtins Core and Builtins
----------------- -----------------
- bpo-12414: sys.getsizeof() on a code object now returns the sizes - bpo-12414: sys.getsizeof() on a code object now returns the sizes
which includes the code struct and sizes of objects which it references. which includes the code struct and sizes of objects which it references.
Patch by Dong-hee Na. Patch by Dong-hee Na.
...@@ -317,6 +317,10 @@ Extension Modules ...@@ -317,6 +317,10 @@ Extension Modules
Library Library
------- -------
- bpo-30190: unittest's assertAlmostEqual and assertNotAlmostEqual provide a
better message in case of failure which includes the difference between
left and right arguments. (patch by Giampaolo Rodola')
- bpo-30101: Add support for curses.A_ITALIC. - bpo-30101: Add support for curses.A_ITALIC.
- bpo-29822: inspect.isabstract() now works during __init_subclass__. Patch - bpo-29822: inspect.isabstract() now works during __init_subclass__. Patch
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment