From 86e03ead032042ecf252429dd7847a53ef0aeeed Mon Sep 17 00:00:00 2001 From: Pavel Savchenko Date: Wed, 3 Aug 2016 11:23:12 +0200 Subject: [PATCH] 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 --- pytest_mock.py | 8 ++++++-- test_pytest_mock.py | 17 +++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/pytest_mock.py b/pytest_mock.py index 457349e..4a9782c 100644 --- a/pytest_mock.py +++ b/pytest_mock.py @@ -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) diff --git a/test_pytest_mock.py b/test_pytest_mock.py index cf9506c..6228f5d 100644 --- a/test_pytest_mock.py +++ b/test_pytest_mock.py @@ -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 """ @@ -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") @@ -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) @@ -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) @@ -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) @@ -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)