Skip to content

Provide assertion functions in unittest.mock #75132

Closed

Description

@OddBloke
mannequin
BPO 30949
Nosy @OddBloke, @berkerpeksag, @pppery
Superseder
  • bpo-24651: Mock.assert* API is in user namespace
  • 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-10-26.08:37:33.151>
    created_at = <Date 2017-07-17.14:36:43.006>
    labels = ['3.7', 'type-feature', 'library']
    title = 'Provide assertion functions in unittest.mock'
    updated_at = <Date 2017-10-26.08:37:33.148>
    user = 'https://github.com/OddBloke'

    bugs.python.org fields:

    activity = <Date 2017-10-26.08:37:33.148>
    actor = 'berker.peksag'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-10-26.08:37:33.151>
    closer = 'berker.peksag'
    components = ['Library (Lib)']
    creation = <Date 2017-07-17.14:36:43.006>
    creator = 'odd_bloke'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 30949
    keywords = []
    message_count = 3.0
    messages = ['298533', '298553', '305036']
    nosy_count = 4.0
    nosy_names = ['odd_bloke', 'berker.peksag', 'ppperry', 'tribaal']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '24651'
    type = 'enhancement'
    url = 'https://bugs.python.org/issue30949'
    versions = ['Python 3.7']

    Activity

    1. OddBloke commented on Jul 17, 2017

      OddBlokemannequin
      MannequinAuthor

      The convenience assertion methods on mock objects can be easily mistyped and if they are mistyped, they will silently pass. This can be quite user-hostile. Consider the following:

      >> example = Mock()
      >> example.assert_called_once()
      >> example.assert_caled_once_with(...)

      This will not raise any exceptions, though the first feels natural and the latter is misspelt. To avoid using the methods, one can type:

      >> example = Mock()
      >> assert example.call_count == 1
      >> assert example.call_args_list == [call(...)]

      but the meaning of that latter statement is particularly non-obvious. Instead, it would be great if I could import the assertions from mock as functions, and call them with mock as the first argument:

      >> from unittest.mock import assert_called_once # This will be an ImportError
      >> example = Mock()
      >> assert_caled_once_with(example, ...) # A NameError
      >> assert_called_once_with(example, ...) # Great success!

    2. added
      tests
      3.7 (EOL)
      on Jul 17, 2017
    3. pppery commented on Jul 17, 2017

      ppperymannequin
      Mannequin

      You can already do this, although it it somewhat of a hack:

      >> assert_called_once=Mock.assert_called_once # This will be an AttributeError
      >> assert_called_once_with=Mock.assert_called_once_with
      >> example = Mock()
      >> assert_caled_once_with(example, ...) # A NameError
      >> assert_called_once_with(example, ...) # Great success!

    4. berkerpeksag commented on Oct 26, 2017

      @berkerpeksag
      Member

      Thank you for the report. This is a duplicate of bpo-24651.

    5. added
      stdlib
      and removed
      tests
      on Oct 26, 2017
    6. added
      type-feature
      on Oct 26, 2017
    7. 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

    No one assigned

      Labels

      3.7 (EOL)stdlibtype-feature

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions