9

When calling unittest.TestCase.assertEqual() on two complex dictionaries, I get a nice diff.

Is there any way to get a diff from Python 2.7's mock.Mock.assert_called_with? I'm testing calls that take some dict parameters with complex values, and it's very difficult to see what the problem is when one of them is just missing a boolean.

e.g.

AssertionError: Expected call: post('http://cloud-atlas.cogolo.net/api/executions', auth=('cloudatlas', 'cloudatlas'), data={'environment': 'hadoop', 'source': 'repo', 'output_path': 'hdfs://namenode-01/user/erose/testing', 'production': True, 'input_path': 'hdfs://namenode-01/foo/bar', 'name': 'linecount', 'mrcloud_options': '{"mrcloud_option_A": "foobar", "hadoop_user_name": "erose"}', 'details': '{"path_to_file": "linecount.py", "parameters": {}, "branch": "master", "repo_name": "example_repo"}'}, headers={'X-COGO-CLOUDATLAS-USER': 'erose'})
Actual call: post('http://cloud-atlas.cogolo.net/api/executions', auth=('cloudatlas', 'cloudatlas'), data={'input_path': 'hdfs://namenode-01/foo/bar', 'name': 'linecount', 'environment': 'hadoop', 'source': 'repo', 'output_path': 'hdfs://namenode-01/user/erose/testing', 'details': '{"path_to_file": "linecount.py", "parameters": {}, "branch": "master", "repo_name": "example_repo"}', 'mrcloud_options': '{"mrcloud_option_A": "foobar", "hadoop_user_name": "erose"}'}, headers={'X-COGO-CLOUDATLAS-USER': 'erose'})
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Eli Rose
  • 6,788
  • 8
  • 35
  • 55
  • 1
    I'm not aware of any 'easy' way to do so; you'd have to patch the [`Mock._format_mock_failure_message()` function](https://hg.python.org/cpython/file/3.5/Lib/unittest/mock.py#l737). – Martijn Pieters Sep 07 '16 at 15:43
  • Dang. Thanks. Since `mock` is built-in in python 3, would pull requests against it have to go through a PEP? – Eli Rose Sep 08 '16 at 00:19
  • No; just submit a request at bugs.python.org – Martijn Pieters Sep 08 '16 at 01:09
  • Sadly this still hasn't been implemented. Future visitors may consider the option doing assert_called() instead, followed by assertDictEqual on the complex dictionary (obtained from call_args). You could even finish with the assert_called_with to check the remaining of the arguments. A bit messy, but this should at least produce useful output every time. – Asky McAskface Dec 30 '22 at 09:19

0 Answers0