[issue38763] mock with side effect : assert_called_once returns None while call_count returns 1

Karthikeyan Singaravelan report at bugs.python.org
Sun Nov 10 23:36:22 EST 2019


Karthikeyan Singaravelan <tir.karthi at gmail.com> added the comment:

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()])

----------
nosy: +xtreak

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38763>
_______________________________________


More information about the Python-bugs-list mailing list