Issue 26323: Add assert_called() and assert_called_once() methods for mock objects (original) (raw)

Created on 2016-02-10 00:00 by Amit.Saha, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue26323.patch Amit.Saha,2016-02-11 23:07 Patch review
issue26323.patch Amit.Saha,2016-02-11 23:11 Modified patch review
issue26323.patch Amit.Saha,2016-02-18 06:25 Updated patch review
issue26323.patch Amit.Saha,2016-02-18 12:28 review
issue26323.patch Amit.Saha,2016-02-18 23:14 Updated patch with docs review
issue26323.patch Amit.Saha,2016-02-22 03:37 Updated patch review
Messages (24)
msg259960 - (view) Author: Amit Saha (Amit.Saha) * Date: 2016-02-10 00:00
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.
msg259981 - (view) Author: Florian Bruhin (The Compiler) * Date: 2016-02-10 05:50
I agree this would be useful. "assert themock.called" (or self.assertTrue) works, but always trips me up.
msg260143 - (view) Author: Amit Saha (Amit.Saha) * Date: 2016-02-11 23:07
Please take a look at the attached patch.
msg260144 - (view) Author: Amit Saha (Amit.Saha) * Date: 2016-02-11 23:11
Fixed the test name
msg260409 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-02-17 21:05
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
msg260410 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-02-17 21:06
> 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.
msg260412 - (view) Author: Amit Saha (Amit.Saha) * Date: 2016-02-17 22:47
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.
msg260428 - (view) Author: Kushal Das (kushal.das) * (Python committer) Date: 2016-02-18 05:31
Go ahead and update the patch for assert_called_once and related tests.
msg260432 - (view) Author: Amit Saha (Amit.Saha) * Date: 2016-02-18 06:25
Added assert_call_once()
msg260451 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-02-18 11:56
I reviewed your patch.
msg260453 - (view) Author: Amit Saha (Amit.Saha) * Date: 2016-02-18 12:27
Thanks for the review. Updated patch addressing the comments.
msg260454 - (view) Author: Amit Saha (Amit.Saha) * Date: 2016-02-18 12:28
Updated patch
msg260456 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-02-18 13:00
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".
msg260487 - (view) Author: Amit Saha (Amit.Saha) * Date: 2016-02-18 23:14
Updated patch with docs and addressed review comments.
msg260646 - (view) Author: Amit Saha (Amit Saha) Date: 2016-02-21 22:34
@haypo : Just a reminder request to please view the updated patch.
msg260647 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-02-21 23:18
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?
msg260656 - (view) Author: Amit Saha (Amit.Saha) * Date: 2016-02-22 03:37
Updated patch
msg260722 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2016-02-23 10:06
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.
msg260728 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-02-23 11:24
> 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_ :-)
msg260836 - (view) Author: Amit Saha (Amit.Saha) * Date: 2016-02-25 02:59
Thanks Michael, so looks like we are all set for merging this?
msg261584 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2016-03-11 17:41
Yes, this can go in.
msg261605 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-03-11 21:21
New changeset bd75f8d34f97 by Victor Stinner in branch 'default': Add Mock.assert_called() https://hg.python.org/cpython/rev/bd75f8d34f97
msg261606 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-03-11 21:23
> 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!
msg261623 - (view) Author: Amit Saha (Amit.Saha) * Date: 2016-03-12 00:27
Thank you Victor.
History
Date User Action Args
2022-04-11 14:58:27 admin set github: 70511
2016-03-12 01:08:34 berker.peksag set status: open -> closedresolution: fixedstage: resolved
2016-03-12 00:27:31 Amit.Saha set messages: +
2016-03-11 21:23:27 vstinner set messages: +
2016-03-11 21:21:46 python-dev set nosy: + python-devmessages: +
2016-03-11 17:41:43 michael.foord set messages: +
2016-02-25 02:59:09 Amit.Saha set messages: +
2016-02-23 11:24:50 vstinner set messages: +
2016-02-23 10:06:16 michael.foord set messages: +
2016-02-23 01:50:29 rhettinger set assignee: michael.foord
2016-02-22 03:37:12 Amit.Saha set files: + issue26323.patchmessages: +
2016-02-21 23🔞58 vstinner set messages: +
2016-02-21 22:34:04 Amit Saha set nosy: + Amit Sahamessages: +
2016-02-18 23:14:10 Amit.Saha set files: + issue26323.patchmessages: +
2016-02-18 13:00:28 vstinner set messages: +
2016-02-18 12:29:29 Amit.Saha set title: Add a assert_called() method for mock objects -> Add assert_called() and assert_called_once() methods for mock objects
2016-02-18 12:28:19 Amit.Saha set files: + issue26323.patchmessages: +
2016-02-18 12:27:09 Amit.Saha set messages: +
2016-02-18 11:56:03 vstinner set messages: +
2016-02-18 06:25:04 Amit.Saha set files: + issue26323.patchmessages: +
2016-02-18 05:31:01 kushal.das set nosy: + kushal.dasmessages: +
2016-02-17 22:47:50 Amit.Saha set messages: +
2016-02-17 21:06:53 vstinner set messages: +
2016-02-17 21:05:38 vstinner set nosy: + rbcollins, vstinnermessages: + versions: + Python 3.6
2016-02-11 23:11:08 Amit.Saha set files: + issue26323.patchmessages: +
2016-02-11 23:07:53 Amit.Saha set files: + issue26323.patchkeywords: + patchmessages: +
2016-02-10 05:51:32 The Compiler set nosy: + michael.foord
2016-02-10 05:50:44 The Compiler set nosy: + The Compilermessages: +
2016-02-10 00:04:48 Amit.Saha set type: enhancement
2016-02-10 00:00:45 Amit.Saha create