This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author xtreak
Recipients Eli Rose, asfaltboy, michael.foord, xtreak
Date 2018-11-10.18:46:39
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1541875600.05.0.788709270274.issue28054@psf.upfronthosting.co.za>
In-reply-to
Content
I think this can be useful for keyword arguments as in the original suggestion. I tried an initial implementation as below to use difflib to get the difference like unittest if there are keyword args to be checked against the caller list since the absence of one or other generates a diff with empty {} which I find little distracting during my initial iterations. I would like to know if there is still sufficient interest in getting this to core only for keyword arguments given that there is pytest-mock with specialized error reporting handling more cases : https://github.com/pytest-dev/pytest-mock#improved-reporting-of-mock-call-assertion-errors


$ ./python.exe -q
>>> from unittest.mock import Mock
>>> m = Mock()
>>>
>>> m(foo='bar', bar='baz')
<Mock name='mock()' id='4484887184'>
>>>
>>> m.assert_called_with(bar='baz', foo='car')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/unittest/mock.py", line 838, in assert_called_with
    raise AssertionError(_error_message()) from cause
AssertionError: Expected call: mock(bar='baz', foo='car')
Actual call: mock(bar='baz', foo='bar')
- {'bar': 'baz', 'foo': 'car'}
?                        ^

+ {'bar': 'baz', 'foo': 'bar'}
?                        ^


diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index a9c82dcb5d..8603b4ac4c 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -749,6 +749,20 @@ class NonCallableMock(Base):
         call_args = self.call_args
         if len(call_args) == 3:
             call_args = call_args[1:]
+
+        diffMsg = ''
+        if kwargs and self.call_args[1]:
+            import difflib
+
+            seq1 = kwargs
+            seq2 = self.call_args[1]
+            diffMsg = '\n' + '\n'.join(
+                difflib.ndiff(pprint.pformat(seq1).splitlines(),
+                              pprint.pformat(seq2).splitlines()))
+
+        if diffMsg:
+            message += diffMsg
+
         actual_string = self._format_mock_call_signature(*call_args)
         return message % (expected_string, actual_string)
History
Date User Action Args
2018-11-10 18:46:40xtreaksetrecipients: + xtreak, michael.foord, asfaltboy, Eli Rose
2018-11-10 18:46:40xtreaksetmessageid: <1541875600.05.0.788709270274.issue28054@psf.upfronthosting.co.za>
2018-11-10 18:46:40xtreaklinkissue28054 messages
2018-11-10 18:46:39xtreakcreate