Skip to content

Commit 86e03ea

Browse files
committed
Add original call assertion error message and improve result message
* assert both args and kwargs at the same time for a more detailed report * update tests to assert the above This improves on PR #36 according to the points raised in PR #57
1 parent f8acbf5 commit 86e03ea

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

pytest_mock.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,12 @@ def assert_wrapper(__wrapped_mock_method__, *args, **kwargs):
159159
__mock_self = args[0]
160160
if __mock_self.call_args is not None:
161161
actual_args, actual_kwargs = __mock_self.call_args
162-
assert actual_args == args[1:]
163-
assert actual_kwargs == kwargs
162+
try:
163+
assert (args[1:], kwargs) == (actual_args, actual_kwargs)
164+
except AssertionError as detailed_comparison:
165+
e.args = (e.msg + "\n\n... pytest introspection follows:\n" +
166+
detailed_comparison.msg, )
167+
print(e.args)
164168
raise AssertionError(*e.args)
165169

166170

test_pytest_mock.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ def assert_traceback():
339339

340340

341341
@contextmanager
342-
def assert_argument_introspection(left, right):
342+
def assert_argument_introspection(arg_left, arg_right, kwarg_left, kwarg_right):
343343
"""
344344
Assert detailed argument introspection is used
345345
"""
@@ -351,7 +351,8 @@ def assert_argument_introspection(left, right):
351351
# NOTE: we assert with either verbose or not, depending on how our own
352352
# test was run by examining sys.argv
353353
verbose = any(a.startswith('-v') for a in sys.argv)
354-
expected = '\n '.join(util._compare_eq_iterable(left, right, verbose))
354+
expected = '\n '.join(util._compare_eq_iterable(
355+
(arg_left, kwarg_left), (arg_right, kwarg_right), verbose))
355356
assert expected in e.msg
356357
else:
357358
raise AssertionError("DID NOT RAISE")
@@ -394,7 +395,7 @@ def test_assert_called_args_with_introspection(mocker):
394395
stub.assert_called_with(*complex_args)
395396
stub.assert_called_once_with(*complex_args)
396397

397-
with assert_argument_introspection(complex_args, wrong_args):
398+
with assert_argument_introspection(wrong_args, complex_args, {}, {}):
398399
stub.assert_called_with(*wrong_args)
399400
stub.assert_called_once_with(*wrong_args)
400401

@@ -409,7 +410,7 @@ def test_assert_called_kwargs_with_introspection(mocker):
409410
stub.assert_called_with(**complex_kwargs)
410411
stub.assert_called_once_with(**complex_kwargs)
411412

412-
with assert_argument_introspection(complex_kwargs, wrong_kwargs):
413+
with assert_argument_introspection((), (), wrong_kwargs, complex_kwargs):
413414
stub.assert_called_with(**wrong_kwargs)
414415
stub.assert_called_once_with(**wrong_kwargs)
415416

@@ -500,12 +501,12 @@ def test_foo(mocker):
500501
assert len(traceback_lines) == 1 # make sure there are no duplicated tracebacks (#44)
501502

502503

503-
def test_assertion_error_is_not_descriptive(mocker):
504-
"""Demonstrate that assert_wrapper does really bad things to assertion messages"""
504+
def test_assertion_error_is_descriptive(mocker):
505+
"""Verify assert_wrapper starts with original call comparison error msg"""
505506
import mock
506507
from pytest_mock import _mock_module_originals
507508
mocker_mock = mocker.patch('os.remove')
508-
mock_mock = mock.Mock()
509+
mock_mock = mock.patch('os.remove').start() # use same func name
509510
assert_called_with = _mock_module_originals['assert_called_with']
510511

511512
mocker_mock(a=1, b=2)
@@ -521,4 +522,4 @@ def test_assertion_error_is_not_descriptive(mocker):
521522
except AssertionError as e:
522523
mock_error_message = e.msg
523524

524-
assert mock_error_message == mocker_error_message
525+
assert mocker_error_message.startswith(mock_error_message)

0 commit comments

Comments
 (0)