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, cjw296, mariocj89, michael.foord, rhettinger, xtreak
Date 2018-12-09.14:07:36
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1544364456.86.0.788709270274.issue28054@psf.upfronthosting.co.za>
In-reply-to
Content
It seems that TestCase in unittest.case accepts failureException attribute that is raised on assertion failure. My initial approach is to subclass TestCase and add a custom failure exception like MockException that is caught to add the diff to the original message. I am adding @cjw296 and @mariocj89 for feedback since I hope this can reuse unittest.case.TestCase implementation for diffs. I think this is better done with a flag like verbose=True to display the difference and defaulting to False since sometimes the message can be long depending on args and kwargs. Also I find args (tuple) comparison to be little distracting. Since this is an enhancement I am adding 3.8.

# Sample file

from unittest.mock import Mock

m = Mock()
m(1, 2, foo='bar', bar='baz')
m.assert_called_with(2, 3, bar='baz', foo='car')

# On 3.7

$ python3.7 /tmp/foo_4.py
Traceback (most recent call last):
  File "/tmp/foo_4.py", line 5, in <module>
    m.assert_called_with(2, 3, bar='baz', foo='car')
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py", line 820, in assert_called_with
    raise AssertionError(_error_message()) from cause
AssertionError: Expected call: mock(2, 3, bar='baz', foo='car')

# With patch

$ ./python.exe /tmp/foo_4.py
Traceback (most recent call last):
  File "/tmp/foo_4.py", line 5, in <module>
    m.assert_called_with(2, 3, bar='baz', foo='car')
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/unittest/mock.py", line 844, in assert_called_with
    raise AssertionError(_error_message()) from cause
AssertionError: Expected call: mock(2, 3, bar='baz', foo='car')
Actual call: mock(1, 2, bar='baz', foo='bar')
args: Tuples differ: (2, 3) != (1, 2)

First differing element 0:
2
1

- (2, 3)
+ (1, 2)
kwargs: {'bar': 'baz', 'foo': 'car'} != {'foo': 'bar', 'bar': 'baz'}
- {'bar': 'baz', 'foo': 'car'}
?                        ^

+ {'bar': 'baz', 'foo': 'bar'}
?                        ^
History
Date User Action Args
2018-12-09 14:07:36xtreaksetrecipients: + xtreak, rhettinger, cjw296, michael.foord, asfaltboy, Eli Rose, mariocj89
2018-12-09 14:07:36xtreaksetmessageid: <1544364456.86.0.788709270274.issue28054@psf.upfronthosting.co.za>
2018-12-09 14:07:36xtreaklinkissue28054 messages
2018-12-09 14:07:36xtreakcreate