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) *  |
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) *  |
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) *  |
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) *  |
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) *  |
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) *  |
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) *  |
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) *  |
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) *  |
Date: 2016-03-11 17:41 |
Yes, this can go in. |
|
|
msg261605 - (view) |
Author: Roundup Robot (python-dev)  |
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) *  |
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. |
|
|