-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
Comments
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. |
I agree this would be useful. "assert themock.called" (or self.assertTrue) works, but always trips me up. |
Please take a look at the attached patch. |
Fixed the test name |
If we go into this direction, I suggest to add *two* methods:
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 |
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. |
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. |
Go ahead and update the patch for assert_called_once and related tests. |
Added assert_call_once() |
I reviewed your patch. |
Thanks for the review. Updated patch addressing the comments. |
Updated patch |
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". |
Updated patch with docs and addressed review comments. |
@Haypo : Just a reminder request to please view the updated patch. |
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? |
Updated patch |
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. |
Yeah, I understand that. IMHO having to check if a function was called is a common need. Mock I like the detection of spelling typos for methods starting with assert_ :-) |
Thanks Michael, so looks like we are all set for merging this? |
Yes, this can go in. |
New changeset bd75f8d34f97 by Victor Stinner in branch 'default': |
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! |
Thank you Victor. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: