Skip to content

Consistency in unittest assert methods: order of actual, expected #54782

Closed

Description

@voidspace
BPO 10573
Nosy @birkenfeld, @rhettinger, @ezio-melotti, @bitdancer, @voidspace
Files
  • issue10573.diff
  • issue10573.diff
  • expected-actual.diff
  • 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 = 'https://github.com/ezio-melotti'
    closed_at = <Date 2011-01-28.19:52:41.307>
    created_at = <Date 2010-11-29.00:55:57.740>
    labels = ['library', 'docs']
    title = 'Consistency in unittest assert methods: order of actual, expected'
    updated_at = <Date 2011-01-28.19:52:41.306>
    user = 'https://github.com/voidspace'

    bugs.python.org fields:

    activity = <Date 2011-01-28.19:52:41.306>
    actor = 'michael.foord'
    assignee = 'ezio.melotti'
    closed = True
    closed_date = <Date 2011-01-28.19:52:41.307>
    closer = 'michael.foord'
    components = ['Documentation', 'Library (Lib)']
    creation = <Date 2010-11-29.00:55:57.740>
    creator = 'michael.foord'
    dependencies = []
    files = ['20103', '20105', '20580']
    hgrepos = []
    issue_num = 10573
    keywords = ['patch']
    message_count = 13.0
    messages = ['122752', '124115', '124288', '124292', '124303', '124305', '124314', '124704', '126689', '126911', '126912', '127329', '127331']
    nosy_count = 6.0
    nosy_names = ['georg.brandl', 'rhettinger', 'ron_adam', 'ezio.melotti', 'r.david.murray', 'michael.foord']
    pr_nums = []
    priority = 'high'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue10573'
    versions = ['Python 3.2']

    Activity

    1. voidspace commented on Nov 29, 2010

      @voidspace
      ContributorAuthor

      The unittest documentation, argument names and implementation need to be consistent about the order of (actual, expected) for TestCase.assert methods that take two arguments.

      This is particularly relevant for the methods that produce 'diffed' output on failure - as the order determines whether mismatched items are missing from the expected or additional to the expected.

      The documentation, signatures and implementation of all two argument asserts need to be checked to ensure they are the same and implemented correctly.

      Personally I prefer (actual, expected).

    2. self-assigned this
      on Nov 29, 2010
    3. added
      docs
      stdlib
      on Nov 29, 2010
    4. rhettinger commented on Dec 16, 2010

      @rhettinger
      Contributor

      This should get fixed-up before the second beta.

      Try to confirm the most common argument ordering using Google's code search to ascertain actual practice in real code.

      FWIW, I also tend to use actual/expected and find the reverse to be a form Yoda-speak, "assert 5 == x", "perhaps misread the prophecy was", etc.

    5. ezio-melotti commented on Dec 18, 2010

      @ezio-melotti
      Member

      The attached patch changes the following things

      • assertDictContainsSubset(expected, actual, msg=None)
        + assertDictContainsSubset(subset, dictionary, msg=None)
        This doesn't change the order of the args, even though the name of the method might suggest the opposite. In the output message I left expected and actual because imho it makes sense there (e.g. "key, expected: 3, actual: 5"). This could be changed to expected/got or something else.

      • assertCountEqual(expected, actual, msg=None)
        + assertCountEqual(actual, expected, msg=None)
        This changes the order of the args and as a consequence the list of 'missing' and 'unexpected' elements will be swapped. The tests don't check the error message so they all pass without modification. It should be noted that this method didn't exist in 3.1. However 2.7 has the equivalent assertItemsEqual with the args swapped, so maybe it should be documented somewhere that while porting from 2.7 to 3.2 the order of the args should be changed accordingly.

      • assertDictEqual(expected, actual, msg=None)
        + assertDictEqual(dict1, dict2, msg=None)
        This is just a doc change, the actual method already uses d1 and d2 as args and their order doesn't seem to matter.

      The other methods (mainly the assert*Equal) are just documented with generic first/second or foo1/foo2. The doc for these methods could be changed to use actual/expected as a preferred order.

    6. birkenfeld commented on Dec 18, 2010

      @birkenfeld
      Member

      If Michael and Raymond reach a consensus, this can go in before beta2.

    7. rhettinger commented on Dec 18, 2010

      @rhettinger
      Contributor

      These three changes look fine.

      The argument order for assertDictContainsSubset is problematic (doesn't match its own name and doesn't match the order for other __contains__ methods elsewhere in Python), but it's too late to change it now :-(

    8. voidspace commented on Dec 18, 2010

      @voidspace
      ContributorAuthor

      The argument order doesn't match the name (which isn't a huge deal I don't think) - but subset in dict does match the element in container order of assertIn.

    9. ezio-melotti commented on Dec 18, 2010

      @ezio-melotti
      Member

      Fixed in r87389. This can be backported to 3.1/2.7 where applicable.

    10. ronadam commented on Dec 27, 2010

      ronadammannequin
      Mannequin

      The bpo-10573.diff file with the time stamp 20:03 has a lot of document changes that don't have corresponding code changes?

    11. ezio-melotti commented on Jan 21, 2011

      @ezio-melotti
      Member

      Not all the arg names in the doc match with the actual name of the args (probably because the methods are expected to be used with positional args only), that's why the changes affects only the doc.

    12. voidspace commented on Jan 24, 2011

      @voidspace
      ContributorAuthor

      There was a BDFL ruling on python-dev mailing list that assert argument names should be (first, second).

      See:

      http://mail.python.org/pipermail/python-dev/2010-December/106954.html

      I'm basically reverting the patch to go back to (first, second) in the docs and in assertCountEqual.

    13. voidspace commented on Jan 24, 2011

      @voidspace
      ContributorAuthor

      Note that I looked at making the sequence comparison code symmetric - but it generates nice diffs for even nested containers by using prettyprint and difflib. Switching to a symmetric output ("in first, not in second" etc) would be very difficult without losing functionality.

      Generating these diffs can be very slow though, so it may be an issue that needs revisiting.

    14. voidspace commented on Jan 28, 2011

      @voidspace
      ContributorAuthor

      Patch to docs and minor change to assertCountEqual to not use actual / expected internally.

    15. bitdancer commented on Jan 28, 2011

      @bitdancer
      Member

      Code patch looks good to me. Unittest tests pass.

    16. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    docsstdlib

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions