Skip to content

Instantly share code, notes, and snippets.

@edvardm
Last active March 27, 2018 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edvardm/6d0a1cadf5d83a251e89aaa52d9f82a8 to your computer and use it in GitHub Desktop.
Save edvardm/6d0a1cadf5d83a251e89aaa52d9f82a8 to your computer and use it in GitHub Desktop.
Make mock assertion failures readable
# mock objects print assertion failures in a way that is hard to read.
#
# Example:
#
# AssertionError: Expected call: <mock assertion failure: 1122331 ...>
# Actual call: <mock assertion failure: 112331 ...>
#
# After the patch:
#
# AssertionError:
# Expected call: <mock assertion failure: 1122331 ...>
# Actual call: <mock assertion failure: 112331 ...>
import mock
# monkeypatch format_mock_failure_message so that expected and actual calls are actually aligned
# to make diff easier to read
def patched_format_mock_failure_message(self, args, kwargs):
message = '\nExpected call: %s\nActual call: %s' # changed only this line
expected_string = self._format_mock_call_signature(args, kwargs)
call_args = self.call_args
if len(call_args) == 3:
call_args = call_args[1:]
actual_string = self._format_mock_call_signature(*call_args)
return message % (expected_string, actual_string)
mock.mock.NonCallableMock._format_mock_failure_message = patched_format_mock_failure_message # pylint: disable=protected-access
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment