Skip to content

Commit

Permalink
Add original call assertion error message and improve result message
Browse files Browse the repository at this point in the history
* assert both args and kwargs at the same time for a more detailed report
* update tests to assert the above

This improves on PR pytest-dev#36 according to the points raised in PR pytest-dev#57
  • Loading branch information
asfaltboy committed Aug 3, 2016
1 parent f8acbf5 commit 86e03ea
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
8 changes: 6 additions & 2 deletions pytest_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,12 @@ def assert_wrapper(__wrapped_mock_method__, *args, **kwargs):
__mock_self = args[0]
if __mock_self.call_args is not None:
actual_args, actual_kwargs = __mock_self.call_args
assert actual_args == args[1:]
assert actual_kwargs == kwargs
try:
assert (args[1:], kwargs) == (actual_args, actual_kwargs)
except AssertionError as detailed_comparison:
e.args = (e.msg + "\n\n... pytest introspection follows:\n" +
detailed_comparison.msg, )
print(e.args)
raise AssertionError(*e.args)


Expand Down
17 changes: 9 additions & 8 deletions test_pytest_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ def assert_traceback():


@contextmanager
def assert_argument_introspection(left, right):
def assert_argument_introspection(arg_left, arg_right, kwarg_left, kwarg_right):
"""
Assert detailed argument introspection is used
"""
Expand All @@ -351,7 +351,8 @@ def assert_argument_introspection(left, right):
# NOTE: we assert with either verbose or not, depending on how our own
# test was run by examining sys.argv
verbose = any(a.startswith('-v') for a in sys.argv)
expected = '\n '.join(util._compare_eq_iterable(left, right, verbose))
expected = '\n '.join(util._compare_eq_iterable(
(arg_left, kwarg_left), (arg_right, kwarg_right), verbose))
assert expected in e.msg
else:
raise AssertionError("DID NOT RAISE")
Expand Down Expand Up @@ -394,7 +395,7 @@ def test_assert_called_args_with_introspection(mocker):
stub.assert_called_with(*complex_args)
stub.assert_called_once_with(*complex_args)

with assert_argument_introspection(complex_args, wrong_args):
with assert_argument_introspection(wrong_args, complex_args, {}, {}):
stub.assert_called_with(*wrong_args)
stub.assert_called_once_with(*wrong_args)

Expand All @@ -409,7 +410,7 @@ def test_assert_called_kwargs_with_introspection(mocker):
stub.assert_called_with(**complex_kwargs)
stub.assert_called_once_with(**complex_kwargs)

with assert_argument_introspection(complex_kwargs, wrong_kwargs):
with assert_argument_introspection((), (), wrong_kwargs, complex_kwargs):
stub.assert_called_with(**wrong_kwargs)
stub.assert_called_once_with(**wrong_kwargs)

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


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

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

assert mock_error_message == mocker_error_message
assert mocker_error_message.startswith(mock_error_message)

0 comments on commit 86e03ea

Please sign in to comment.