classification
Title: unittest's assertAlmostEqual improved error message
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, giampaolo.rodola, michael.foord, rbcollins, rhettinger, serhiy.storchaka
Priority: normal Keywords:

Created on 2017-04-27 20:12 by giampaolo.rodola, last changed 2017-07-25 21:06 by giampaolo.rodola. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 1331 merged giampaolo.rodola, 2017-04-27 20:52
PR 1534 merged giampaolo.rodola, 2017-05-10 13:24
Messages (14)
msg292477 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2017-04-27 20:12
When comparing 2 numbers as "self.assertAlmostEqual(a, b, delta=1000)" the error message looks like this:

    AssertionError: 27332885 != 27391120 within 1000 delta

Especially when a and b are big numbers or differ a lot, it would be useful to see the absolute difference between the 2 numbers as in:

    AssertionError: 27332885 != 27391120 within 1000 delta (58235 difference)
msg292482 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2017-04-27 20:53
PR: https://github.com/python/cpython/pull/1331/
msg292510 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-04-28 05:02
Is it worth to output a difference when the delta argument is not passed?

Is it worth to output a signed difference rather than an absolute value?
msg292537 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2017-04-28 16:17
Thanks for chiming in.

> Is it worth to output a difference when the delta argument is not passed?

Yes, probably. I will change that.

> Is it worth to output a signed difference rather than an absolute value?

Mmmm probably not IMHO. Personally I never care about the position of left and right arguments. I just want to check whether a and b are almost equal.
msg292540 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2017-04-28 17:05
Done.
msg292548 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-04-28 20:26
The patch looks good to me.
msg292668 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2017-05-01 16:18
New changeset 5d7a8d0c13737fd531b722ad76c505ef47aac96a by Giampaolo Rodola in branch 'master':
bpo-30190: improved error msg for assertAlmostEqual(delta=...) (#1331)
https://github.com/python/cpython/commit/5d7a8d0c13737fd531b722ad76c505ef47aac96a
msg293350 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-05-09 21:30
New test produce deprecation warnings:

/home/serhiy/py/cpython/Lib/unittest/test/test_assertions.py:228: DeprecationWarning: invalid escape sequence \(
  ["^1 != 2 within 7 places \(1 difference\)$", "^oops$",
/home/serhiy/py/cpython/Lib/unittest/test/test_assertions.py:229: DeprecationWarning: invalid escape sequence \(
  "^1 != 2 within 7 places \(1 difference\)$",
/home/serhiy/py/cpython/Lib/unittest/test/test_assertions.py:230: DeprecationWarning: invalid escape sequence \(
  "^1 != 2 within 7 places \(1 difference\) : oops$"])
msg293415 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2017-05-10 13:02
Mmm I can't see them. Am I supposed to enable this kind of warning somehow?
msg293416 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-05-10 13:07
$ ./python -Wa Lib/unittest/test/test_assertions.py
Lib/unittest/test/test_assertions.py:228: DeprecationWarning: invalid escape sequence \(
  ["^1 != 2 within 7 places \(1 difference\)$", "^oops$",
Lib/unittest/test/test_assertions.py:229: DeprecationWarning: invalid escape sequence \(
  "^1 != 2 within 7 places \(1 difference\)$",
Lib/unittest/test/test_assertions.py:230: DeprecationWarning: invalid escape sequence \(
  "^1 != 2 within 7 places \(1 difference\) : oops$"])
...................................
----------------------------------------------------------------------
Ran 35 tests in 0.052s

OK
msg293417 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-05-10 13:09
Warnings are not shown when the source is already compiled to .pyc file.

$ find -name '*.py[co]' -exec rm '{}' +
$ ./python -Wa -m test test_unittest
Run tests sequentially
0:00:00 load avg: 0.54 [1/1] test_unittest
/home/serhiy/py/cpython/Lib/unittest/test/test_assertions.py:228: DeprecationWarning: invalid escape sequence \(
  ["^1 != 2 within 7 places \(1 difference\)$", "^oops$",
/home/serhiy/py/cpython/Lib/unittest/test/test_assertions.py:229: DeprecationWarning: invalid escape sequence \(
  "^1 != 2 within 7 places \(1 difference\)$",
/home/serhiy/py/cpython/Lib/unittest/test/test_assertions.py:230: DeprecationWarning: invalid escape sequence \(
  "^1 != 2 within 7 places \(1 difference\) : oops$"])
1 test OK.

Total duration: 5 sec
Tests result: SUCCESS
msg293418 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2017-05-10 13:10
OK thanks. Fix coming.
msg299138 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2017-07-25 21:05
Reopening as it needs backports for 2.7, 3.3, 3.4, 3.5 and 3.6.
msg299140 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2017-07-25 21:06
Sorry, I accidentally picked up the wrong thread.
History
Date User Action Args
2017-07-25 21:06:29giampaolo.rodolasetstatus: pending -> closed
versions: - Python 2.7, Python 3.3, Python 3.4, Python 3.5, Python 3.6
messages: + msg299140

resolution: fixed
stage: backport needed -> resolved
2017-07-25 21:05:30giampaolo.rodolasetstatus: closed -> pending
versions: + Python 2.7, Python 3.3, Python 3.4, Python 3.5, Python 3.6
messages: + msg299138

resolution: fixed -> (no value)
stage: resolved -> backport needed
2017-05-10 18:13:40giampaolo.rodolasetstatus: open -> closed
2017-05-10 13:24:27giampaolo.rodolasetpull_requests: + pull_request1633
2017-05-10 13:10:48giampaolo.rodolasetmessages: + msg293418
2017-05-10 13:09:58serhiy.storchakasetmessages: + msg293417
2017-05-10 13:07:19serhiy.storchakasetmessages: + msg293416
2017-05-10 13:02:56giampaolo.rodolasetmessages: + msg293415
2017-05-09 21:30:45serhiy.storchakasetstatus: closed -> open

messages: + msg293350
2017-05-05 07:31:14berker.peksagsetstatus: open -> closed
type: enhancement
resolution: fixed
components: + Library (Lib)
stage: patch review -> resolved
2017-05-01 16:18:58giampaolo.rodolasetmessages: + msg292668
2017-04-28 20:26:01rhettingersetnosy: + rhettinger
messages: + msg292548
2017-04-28 17:05:26giampaolo.rodolasetmessages: + msg292540
2017-04-28 16:17:48giampaolo.rodolasetmessages: + msg292537
2017-04-28 05:02:48serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg292510
2017-04-27 20:56:33giampaolo.rodolasettitle: unittest's assertAlmostEqual should show the difference -> unittest's assertAlmostEqual improved error message
2017-04-27 20:53:36giampaolo.rodolasetmessages: + msg292482
stage: needs patch -> patch review
2017-04-27 20:52:25giampaolo.rodolasetpull_requests: + pull_request1439
2017-04-27 20:21:22giampaolo.rodolasetnosy: + rbcollins, ezio.melotti, michael.foord
2017-04-27 20:12:42giampaolo.rodolacreate