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: mock with side effect : assert_called_once returns None while call_count returns 1
Type: behavior Stage: resolved
Components: Tests Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: cjw296, lisroach, mariocj89, michael.foord, piscvau, xtreak
Priority: normal Keywords:

Created on 2019-11-10 22:09 by piscvau, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
bugmock.tar piscvau, 2019-11-10 22:09 the file test_LC_message_received contains a test reproducing the bug. The other files are for import
Messages (3)
msg356341 - (view) Author: Troulet-lambert Odile (piscvau) Date: 2019-11-10 22:09
Using a mock with side_effect, I would expect that assert_called_once returns True if the mock has been called.
Yet it returns None while call_count== 1 returns True.

If this is not a bug, I would welcome some explanation in the documentation.
msg356353 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-11-11 04:36
assert_called_once is supposed to raise an AssertionError if the mock is not called and to return None like other assert_* helpers. The return value is not supposed to be used and it's more of an assertion action where if it's None then it's implied to be True.

>>> from unittest.mock import Mock
>>> def side_effect(*args, **kwargs): print("side_effect called")
...
>>> m = Mock(side_effect=side_effect)
>>> m.call_count
0
>>> m.assert_called_once()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/kasingar/stuff/python/cpython/Lib/unittest/mock.py", line 881, in assert_called_once
    raise AssertionError(msg)
AssertionError: Expected 'mock' to have been called once. Called 0 times.
>>> m()
side_effect called
>>> m.call_count
1
>>> m.assert_called_once()
>>> m.assert_called()
>>> m.assert_has_calls([call()])
msg356444 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-11-12 11:27
I think we can close this as not a bug. I don't think we need to add boolean return value of True if the assert statements are True since it's never meant to be used. I would wait for others opinion on this.
History
Date User Action Args
2022-04-11 14:59:23adminsetgithub: 82944
2019-11-12 13:30:12cjw296setstatus: open -> closed
resolution: not a bug
stage: resolved
2019-11-12 11:27:28xtreaksetnosy: + cjw296, michael.foord, lisroach, mariocj89
messages: + msg356444
2019-11-11 04:36:22xtreaksetnosy: + xtreak
messages: + msg356353
2019-11-10 22:09:23piscvaucreate