Skip to content

Commit 2180f6b

Browse files
sfreilichgpshead
authored andcommitted
bpo-36871: Avoid duplicated 'Actual:' in assertion message (GH-16361)
Fixes an issue caught after merge of PR 16005. Tightened test assertions to check the entire assertion message.
1 parent b5a7a4f commit 2180f6b

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

Lib/unittest/mock.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -939,8 +939,8 @@ def assert_has_calls(self, calls, any_order=False):
939939
for e in expected])
940940
raise AssertionError(
941941
f'{problem}\n'
942-
f'Expected: {_CallList(calls)}\n'
943-
f'Actual: {self._calls_repr(prefix="Actual")}'
942+
f'Expected: {_CallList(calls)}'
943+
f'{self._calls_repr(prefix="Actual").rstrip(".")}'
944944
) from cause
945945
return
946946

Lib/unittest/test/testmock/testasync.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -892,21 +892,28 @@ def test_assert_not_awaited(self):
892892
self.mock.assert_not_awaited()
893893

894894
def test_assert_has_awaits_not_matching_spec_error(self):
895-
async def f(): pass
895+
async def f(x=None): pass
896896

897-
mock = AsyncMock(spec=f)
897+
self.mock = AsyncMock(spec=f)
898+
asyncio.run(self._runnable_test(1))
898899

899900
with self.assertRaisesRegex(
900901
AssertionError,
901-
re.escape('Awaits not found.\nExpected:')) as cm:
902-
mock.assert_has_awaits([call()])
902+
'^{}$'.format(
903+
re.escape('Awaits not found.\n'
904+
'Expected: [call()]\n'
905+
'Actual: [call(1)]'))) as cm:
906+
self.mock.assert_has_awaits([call()])
903907
self.assertIsNone(cm.exception.__cause__)
904908

905909
with self.assertRaisesRegex(
906910
AssertionError,
907-
re.escape('Error processing expected awaits.\n'
908-
"Errors: [None, TypeError('too many positional "
909-
"arguments')]\n"
910-
'Expected:')) as cm:
911-
mock.assert_has_awaits([call(), call('wrong')])
911+
'^{}$'.format(
912+
re.escape(
913+
'Error processing expected awaits.\n'
914+
"Errors: [None, TypeError('too many positional "
915+
"arguments')]\n"
916+
'Expected: [call(), call(1, 2)]\n'
917+
'Actual: [call(1)]'))) as cm:
918+
self.mock.assert_has_awaits([call(), call(1, 2)])
912919
self.assertIsInstance(cm.exception.__cause__, TypeError)

Lib/unittest/test/testmock/testmock.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,23 +1436,30 @@ def f(a, b, c, d=None): pass
14361436
mock.assert_has_calls(calls[:-1], any_order=True)
14371437

14381438
def test_assert_has_calls_not_matching_spec_error(self):
1439-
def f(): pass
1439+
def f(x=None): pass
14401440

14411441
mock = Mock(spec=f)
1442+
mock(1)
14421443

14431444
with self.assertRaisesRegex(
14441445
AssertionError,
1445-
re.escape('Calls not found.\nExpected:')) as cm:
1446+
'^{}$'.format(
1447+
re.escape('Calls not found.\n'
1448+
'Expected: [call()]\n'
1449+
'Actual: [call(1)]'))) as cm:
14461450
mock.assert_has_calls([call()])
14471451
self.assertIsNone(cm.exception.__cause__)
14481452

1453+
14491454
with self.assertRaisesRegex(
14501455
AssertionError,
1451-
re.escape('Error processing expected calls.\n'
1452-
"Errors: [None, TypeError('too many positional "
1453-
"arguments')]\n"
1454-
'Expected:')) as cm:
1455-
mock.assert_has_calls([call(), call('wrong')])
1456+
'^{}$'.format(
1457+
re.escape(
1458+
'Error processing expected calls.\n'
1459+
"Errors: [None, TypeError('too many positional arguments')]\n"
1460+
"Expected: [call(), call(1, 2)]\n"
1461+
'Actual: [call(1)]'))) as cm:
1462+
mock.assert_has_calls([call(), call(1, 2)])
14561463
self.assertIsInstance(cm.exception.__cause__, TypeError)
14571464

14581465
def test_assert_any_call(self):

0 commit comments

Comments
 (0)