This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add assert_called() and assert_called_once() methods for mock objects
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: michael.foord Nosy List: Amit Saha, Amit.Saha, The Compiler, kushal.das, michael.foord, python-dev, rbcollins, vstinner
Priority: normal Keywords: patch

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:27adminsetgithub: 70511
2016-03-12 01:08:34berker.peksagsetstatus: open -> closed
resolution: fixed
stage: resolved
2016-03-12 00:27:31Amit.Sahasetmessages: + msg261623
2016-03-11 21:23:27vstinnersetmessages: + msg261606
2016-03-11 21:21:46python-devsetnosy: + python-dev
messages: + msg261605
2016-03-11 17:41:43michael.foordsetmessages: + msg261584
2016-02-25 02:59:09Amit.Sahasetmessages: + msg260836
2016-02-23 11:24:50vstinnersetmessages: + msg260728
2016-02-23 10:06:16michael.foordsetmessages: + msg260722
2016-02-23 01:50:29rhettingersetassignee: michael.foord
2016-02-22 03:37:12Amit.Sahasetfiles: + issue26323.patch

messages: + msg260656
2016-02-21 23:18:58vstinnersetmessages: + msg260647
2016-02-21 22:34:04Amit Sahasetnosy: + Amit Saha
messages: + msg260646
2016-02-18 23:14:10Amit.Sahasetfiles: + issue26323.patch

messages: + msg260487
2016-02-18 13:00:28vstinnersetmessages: + msg260456
2016-02-18 12:29:29Amit.Sahasettitle: Add a assert_called() method for mock objects -> Add assert_called() and assert_called_once() methods for mock objects
2016-02-18 12:28:19Amit.Sahasetfiles: + issue26323.patch

messages: + msg260454
2016-02-18 12:27:09Amit.Sahasetmessages: + msg260453
2016-02-18 11:56:03vstinnersetmessages: + msg260451
2016-02-18 06:25:04Amit.Sahasetfiles: + issue26323.patch

messages: + msg260432
2016-02-18 05:31:01kushal.dassetnosy: + kushal.das
messages: + msg260428
2016-02-17 22:47:50Amit.Sahasetmessages: + msg260412
2016-02-17 21:06:53vstinnersetmessages: + msg260410
2016-02-17 21:05:38vstinnersetnosy: + rbcollins, vstinner

messages: + msg260409
versions: + Python 3.6
2016-02-11 23:11:08Amit.Sahasetfiles: + issue26323.patch

messages: + msg260144
2016-02-11 23:07:53Amit.Sahasetfiles: + issue26323.patch
keywords: + patch
messages: + msg260143
2016-02-10 05:51:32The Compilersetnosy: + michael.foord
2016-02-10 05:50:44The Compilersetnosy: + The Compiler
messages: + msg259981
2016-02-10 00:04:48Amit.Sahasettype: enhancement
2016-02-10 00:00:45Amit.Sahacreate