Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Lib/test/test_unittest/testmock/testmock.py
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,8 @@ def f(x=None): pass
mock = Mock(spec=f)
mock(1)

uncalled_mock = Mock()

with self.assertRaisesRegex(
AssertionError,
'^{}$'.format(
Expand All @@ -1556,6 +1558,14 @@ def f(x=None): pass
mock.assert_has_calls([call()])
self.assertIsNone(cm.exception.__cause__)

with self.assertRaisesRegex(
AssertionError,
'^{}$'.format(
re.escape('Calls not found.\n'
'Expected: [call()]\n'
' Actual: []'))) as cm:
uncalled_mock.assert_has_calls([call()])
self.assertIsNone(cm.exception.__cause__)

with self.assertRaisesRegex(
AssertionError,
Expand Down
6 changes: 3 additions & 3 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ def assert_has_calls(self, calls, any_order=False):
raise AssertionError(
f'{problem}\n'
f'Expected: {_CallList(calls)}'
f'{self._calls_repr(prefix=" Actual").rstrip(".")}'
f'{self._calls_repr(prefix=" Actual", display_empty=True).rstrip(".")}'
) from cause
return

Expand Down Expand Up @@ -1085,15 +1085,15 @@ def _get_child_mock(self, /, **kw):
return klass(**kw)


def _calls_repr(self, prefix="Calls"):
def _calls_repr(self, prefix="Calls", display_empty=False):
"""Renders self.mock_calls as a string.

Example: "\nCalls: [call(1), call(2)]."

If self.mock_calls is empty, an empty string is returned. The
output will be truncated if very long.
"""
if not self.mock_calls:
if not self.mock_calls and not display_empty:
return ""
return f"\n{prefix}: {safe_repr(self.mock_calls)}."

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Display list of actual calls in unittest `Mock.assert_has_calls` failure
message even if empty.