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

Add assert_called() and assert_called_once() methods for mock objects #70511

Closed
AmitSaha mannequin opened this issue Feb 10, 2016 · 24 comments
Closed

Add assert_called() and assert_called_once() methods for mock objects #70511

AmitSaha mannequin opened this issue Feb 10, 2016 · 24 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@AmitSaha
Copy link
Mannequin

AmitSaha mannequin commented Feb 10, 2016

BPO 26323
Nosy @vstinner, @rbtcollins, @voidspace, @kushaldas, @The-Compiler
Files
  • issue26323.patch: Patch
  • issue26323.patch: Modified patch
  • issue26323.patch: Updated patch
  • issue26323.patch
  • issue26323.patch: Updated patch with docs
  • issue26323.patch: Updated patch
  • 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/voidspace'
    closed_at = <Date 2016-03-12.01:08:34.151>
    created_at = <Date 2016-02-10.00:00:45.206>
    labels = ['type-feature', 'library']
    title = 'Add assert_called() and assert_called_once() methods for mock objects'
    updated_at = <Date 2016-03-12.01:08:34.151>
    user = 'https://bugs.python.org/AmitSaha'

    bugs.python.org fields:

    activity = <Date 2016-03-12.01:08:34.151>
    actor = 'berker.peksag'
    assignee = 'michael.foord'
    closed = True
    closed_date = <Date 2016-03-12.01:08:34.151>
    closer = 'berker.peksag'
    components = ['Library (Lib)']
    creation = <Date 2016-02-10.00:00:45.206>
    creator = 'Amit.Saha'
    dependencies = []
    files = ['41903', '41904', '41946', '41952', '41961', '41997']
    hgrepos = []
    issue_num = 26323
    keywords = ['patch']
    message_count = 24.0
    messages = ['259960', '259981', '260143', '260144', '260409', '260410', '260412', '260428', '260432', '260451', '260453', '260454', '260456', '260487', '260646', '260647', '260656', '260722', '260728', '260836', '261584', '261605', '261606', '261623']
    nosy_count = 8.0
    nosy_names = ['vstinner', 'rbcollins', 'michael.foord', 'python-dev', 'kushal.das', 'Amit.Saha', 'The Compiler', 'Amit Saha']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue26323'
    versions = ['Python 3.6']

    @AmitSaha
    Copy link
    Mannequin Author

    AmitSaha mannequin commented Feb 10, 2016

    Would a patch for adding a assert_called() method to mocked objects be welcome for inclusion? We do have a assert_not_called() method, so I think this may be a good idea. Please let me know and I will work on it.

    @AmitSaha AmitSaha mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Feb 10, 2016
    @The-Compiler
    Copy link
    Mannequin

    The-Compiler mannequin commented Feb 10, 2016

    I agree this would be useful. "assert themock.called" (or self.assertTrue) works, but always trips me up.

    @AmitSaha
    Copy link
    Mannequin Author

    AmitSaha mannequin commented Feb 11, 2016

    Please take a look at the attached patch.

    @AmitSaha
    Copy link
    Mannequin Author

    AmitSaha mannequin commented Feb 11, 2016

    Fixed the test name

    @vstinner
    Copy link
    Member

    If we go into this direction, I suggest to add *two* methods:

    • assert_called(): called at least once
    • assert_called_once(): called exactly once

    I would be more consitent with the existing API:

    https://docs.python.org/dev/library/unittest.mock.html#unittest.mock.Mock.assert_not_called

    https://docs.python.org/dev/library/unittest.mock.html#unittest.mock.Mock.assert_called_with

    https://docs.python.org/dev/library/unittest.mock.html#unittest.mock.Mock.assert_called_once_with

    @vstinner
    Copy link
    Member

    I would be more consitent with the existing API:

    Hum, I'm not sure that I was clear.

    If we only add assert_called() (without assert_called_once()), users may use assert_called() to check if a function is called exactly once which would be a mistake.

    @AmitSaha
    Copy link
    Mannequin Author

    AmitSaha mannequin commented Feb 17, 2016

    Thanks for your comments. I am +1 to those additions and would be happy to update the patch. Let me know if I should go ahead.

    @kushaldas
    Copy link
    Member

    Go ahead and update the patch for assert_called_once and related tests.

    @AmitSaha
    Copy link
    Mannequin Author

    AmitSaha mannequin commented Feb 18, 2016

    Added assert_call_once()

    @vstinner
    Copy link
    Member

    I reviewed your patch.

    @AmitSaha
    Copy link
    Mannequin Author

    AmitSaha mannequin commented Feb 18, 2016

    Thanks for the review. Updated patch addressing the comments.

    @AmitSaha
    Copy link
    Mannequin Author

    AmitSaha mannequin commented Feb 18, 2016

    Updated patch

    @AmitSaha AmitSaha mannequin changed the title Add a assert_called() method for mock objects Add assert_called() and assert_called_once() methods for mock objects Feb 18, 2016
    @vstinner
    Copy link
    Member

    Can you please also document your changes in Doc/library/unittest.mock.rst? You will have to use ".. versionadded:: 3.6" in the doc of new functions.

    You may also document directly the news functions in Doc/whatsnew/3.6.rst. It looks like you have to add a new "unittest" section in "Improved Modules".

    @AmitSaha
    Copy link
    Mannequin Author

    AmitSaha mannequin commented Feb 18, 2016

    Updated patch with docs and addressed review comments.

    @AmitSaha
    Copy link
    Mannequin Author

    AmitSaha mannequin commented Feb 21, 2016

    @Haypo : Just a reminder request to please view the updated patch.

    @vstinner
    Copy link
    Member

    The latest patch looks good to me, but I would prefer to have a review of the maintainer of the unittest.mock module (Michael Ford, voidspace?), or at least a second core developer.

    @robert: Would you mind reviewing the latest patch please?

    @AmitSaha
    Copy link
    Mannequin Author

    AmitSaha mannequin commented Feb 22, 2016

    Updated patch

    @voidspace
    Copy link
    Contributor

    There have been persistent calls for an "assert_called" method over the lifetime of mock. In the past I've rejected them because you can easily get it by asserting the call count is non-zero and I want to avoid the proliferation of a thousand assert methods. The demand is certainly consistent, so it would seem that people feel there is a use case.

    @vstinner
    Copy link
    Member

    In the past I've rejected them because you can easily get it by asserting the call count is non-zero and I want to avoid the proliferation of a thousand assert methods

    Yeah, I understand that.

    IMHO having to check if a function was called is a common need. Mock
    has already assert_not_called() for example.

    I like the detection of spelling typos for methods starting with assert_ :-)

    @AmitSaha
    Copy link
    Mannequin Author

    AmitSaha mannequin commented Feb 25, 2016

    Thanks Michael, so looks like we are all set for merging this?

    @voidspace
    Copy link
    Contributor

    Yes, this can go in.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Mar 11, 2016

    New changeset bd75f8d34f97 by Victor Stinner in branch 'default':
    Add Mock.assert_called()
    https://hg.python.org/cpython/rev/bd75f8d34f97

    @vstinner
    Copy link
    Member

    Yes, this can go in.

    Cool. I pushed the latest patch of Amit Saha. I just modified assert_call() doc to add "at least once".

    Thanks Amit for your contribution!

    @AmitSaha
    Copy link
    Mannequin Author

    AmitSaha mannequin commented Mar 12, 2016

    Thank you Victor.

    @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
    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