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

Fix default mock_name in unittest.mock.assert_called error message #82009

Closed
tirkarthi opened this issue Aug 12, 2019 · 4 comments
Closed

Fix default mock_name in unittest.mock.assert_called error message #82009

tirkarthi opened this issue Aug 12, 2019 · 4 comments
Labels
3.8 only security fixes 3.9 only security fixes easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@tirkarthi
Copy link
Member

BPO 37828
Nosy @cjw296, @voidspace, @mariocj89, @pablogsal, @miss-islington, @tirkarthi
PRs
  • bpo-37828: Fix default mock_name in unittest.mock.assert_called error #16166
  • [3.8] bpo-37828: Fix default mock_name in unittest.mock.assert_called error (GH-16166) #16229
  • 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 2019-09-17.13:16:12.718>
    created_at = <Date 2019-08-12.07:12:33.765>
    labels = ['easy', '3.8', 'type-bug', 'library', '3.9']
    title = 'Fix default mock_name in unittest.mock.assert_called error message'
    updated_at = <Date 2019-09-17.13:16:12.717>
    user = 'https://github.com/tirkarthi'

    bugs.python.org fields:

    activity = <Date 2019-09-17.13:16:12.717>
    actor = 'xtreak'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-09-17.13:16:12.718>
    closer = 'xtreak'
    components = ['Library (Lib)']
    creation = <Date 2019-08-12.07:12:33.765>
    creator = 'xtreak'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 37828
    keywords = ['patch', 'newcomer friendly']
    message_count = 4.0
    messages = ['349444', '352629', '352631', '352635']
    nosy_count = 6.0
    nosy_names = ['cjw296', 'michael.foord', 'mariocj89', 'pablogsal', 'miss-islington', 'xtreak']
    pr_nums = ['16166', '16229']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue37828'
    versions = ['Python 3.8', 'Python 3.9']

    @tirkarthi
    Copy link
    Member Author

    In the format string for assert_called the evaluation order is incorrect and hence for mock's without name 'None' is printed whereas it should be 'mock' like for other messages. The error message is ("Expected '%s' to have been called." % self._mock_name or 'mock') . Here self._mock_name which is None is applied to form the string and then used with the string 'mock' in or combination. The fix would be to have the evaluation order correct like other error messages. Marking this as newcomer-friendly. Please leave this to new contributors as their 1st PR.

    ./python.exe
    Python 3.9.0a0 (heads/master:f03b4c8a48, Aug 12 2019, 10:04:10)
    [Clang 7.0.2 (clang-700.1.81)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from unittest.mock import Mock
    >>> m = Mock()
    >>> m.assert_called_once()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/unittest/mock.py", line 854, in assert_called_once
        raise AssertionError(msg)
    AssertionError: Expected 'mock' to have been called once. Called 0 times.
    >>> m.assert_called()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/unittest/mock.py", line 844, in assert_called
        raise AssertionError(msg)
    AssertionError: Expected 'None' to have been called.

    Thanks

    @tirkarthi tirkarthi added 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir easy type-bug An unexpected behavior, bug, or error labels Aug 12, 2019
    @pablogsal
    Copy link
    Member

    New changeset 5f5f11f by Pablo Galindo (Abraham Toriz Cruz) in branch 'master':
    bpo-37828: Fix default mock_name in unittest.mock.assert_called error (GH-16166)
    5f5f11f

    @miss-islington
    Copy link
    Contributor

    New changeset f668d2b by Miss Islington (bot) in branch '3.8':
    bpo-37828: Fix default mock_name in unittest.mock.assert_called error (GH-16166)
    f668d2b

    @tirkarthi
    Copy link
    Member Author

    I don't think 3.7 needs a backport of the fix. I am closing this as fixed since all PRs are merged. Please reopen if a backport is needed. Thanks @categulario.

    @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.8 only security fixes 3.9 only security fixes easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants