Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unittest's assertAlmostEqual improved error message #74376

Closed
giampaolo opened this issue Apr 27, 2017 · 14 comments
Closed

unittest's assertAlmostEqual improved error message #74376

giampaolo opened this issue Apr 27, 2017 · 14 comments
Labels
3.7 (EOL) end of life stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@giampaolo
Copy link
Contributor

BPO 30190
Nosy @rhettinger, @giampaolo, @rbtcollins, @ezio-melotti, @voidspace, @serhiy-storchaka
PRs
  • bpo-30190: improved error msg for assertAlmostEqual(delta=...) #1331
  • bpo-30190: fix invalid escape sequence warnings #1534
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2017-07-25.21:06:29.071>
    created_at = <Date 2017-04-27.20:12:42.186>
    labels = ['3.7', 'type-feature', 'library']
    title = "unittest's assertAlmostEqual improved error message"
    updated_at = <Date 2017-07-25.21:06:29.069>
    user = 'https://github.com/giampaolo'

    bugs.python.org fields:

    activity = <Date 2017-07-25.21:06:29.069>
    actor = 'giampaolo.rodola'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-07-25.21:06:29.071>
    closer = 'giampaolo.rodola'
    components = ['Library (Lib)']
    creation = <Date 2017-04-27.20:12:42.186>
    creator = 'giampaolo.rodola'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 30190
    keywords = []
    message_count = 14.0
    messages = ['292477', '292482', '292510', '292537', '292540', '292548', '292668', '293350', '293415', '293416', '293417', '293418', '299138', '299140']
    nosy_count = 6.0
    nosy_names = ['rhettinger', 'giampaolo.rodola', 'rbcollins', 'ezio.melotti', 'michael.foord', 'serhiy.storchaka']
    pr_nums = ['1331', '1534']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue30190'
    versions = ['Python 3.7']

    @giampaolo
    Copy link
    Contributor Author

    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)
    

    @giampaolo giampaolo added the 3.7 (EOL) end of life label Apr 27, 2017
    @giampaolo
    Copy link
    Contributor Author

    PR: #1331

    @giampaolo giampaolo changed the title unittest's assertAlmostEqual should show the difference unittest's assertAlmostEqual improved error message Apr 27, 2017
    @serhiy-storchaka
    Copy link
    Member

    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?

    @giampaolo
    Copy link
    Contributor Author

    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.

    @giampaolo
    Copy link
    Contributor Author

    Done.

    @rhettinger
    Copy link
    Contributor

    The patch looks good to me.

    @giampaolo
    Copy link
    Contributor Author

    New changeset 5d7a8d0 by Giampaolo Rodola in branch 'master':
    bpo-30190: improved error msg for assertAlmostEqual(delta=...) (bpo-1331)
    5d7a8d0

    @berkerpeksag berkerpeksag added the stdlib Python modules in the Lib dir label May 5, 2017
    @berkerpeksag berkerpeksag added the type-feature A feature request or enhancement label May 5, 2017
    @serhiy-storchaka
    Copy link
    Member

    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$"])

    @giampaolo
    Copy link
    Contributor Author

    Mmm I can't see them. Am I supposed to enable this kind of warning somehow?

    @serhiy-storchaka
    Copy link
    Member

    $ ./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

    @serhiy-storchaka
    Copy link
    Member

    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

    @giampaolo
    Copy link
    Contributor Author

    OK thanks. Fix coming.

    @giampaolo
    Copy link
    Contributor Author

    Reopening as it needs backports for 2.7, 3.3, 3.4, 3.5 and 3.6.

    @giampaolo giampaolo reopened this Jul 25, 2017
    @giampaolo
    Copy link
    Contributor Author

    Sorry, I accidentally picked up the wrong thread.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants