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.

Title: Wrong order of expected/actual for assert_called_once_with
Type: Stage:
Components: Tests Versions: Python 2.7
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: flwang, michael.foord, r.david.murray
Priority: normal Keywords:

Created on 2014-06-08 07:37 by flwang, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (6)
msg220025 - (view) Author: Fei Long Wang (flwang) Date: 2014-06-08 07:37
>>> m=mock.Mock()
>>> m.some_method('foo', 'bar')
<Mock name='mock.some_method()' id='140353787504656'>
>>> m.some_method.assert_called_once_with('foo', 'bar')
>>> m.some_method.assert_called_once_with('foo', 'baz')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/", line 846, in assert_called_once_with
    return self.assert_called_with(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/", line 835, in assert_called_with
    raise AssertionError(msg)
AssertionError: Expected call: some_method('foo', 'baz')   #####
Actual call: some_method('foo', 'bar')                     #####
msg220029 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2014-06-08 09:30
What specifically are you saying is in the wrong order?
msg220063 - (view) Author: Fei Long Wang (flwang) Date: 2014-06-08 22:25
IMHO, the trace should be:

AssertionError: Expected call: some_method('foo', 'bar')   
Actual call: some_method('foo', 'baz')                    

instead of below:

AssertionError: Expected call: some_method('foo', 'baz')   
Actual call: some_method('foo', 'bar')
msg220067 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-06-08 23:10
But the actual call that you made in your example was some_method('foo', 'bar').

Given that we conventionally write unittest assertions with the actual result first and the expected result second (assertEqual(actual, expected), it might be less confusing if the message was:

  AssertionError: Actual call: some_method('foo', 'bar')
  Expected call: some_method('foo', 'baz')

But since the actual call does appear in the assert call that generates the assertionError, it is not obvious that this would actually be a better order.
msg220086 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2014-06-09 09:21
As David points out - in your example the "actual call" made is m.some_method('foo', 'bar'). Your assertion (the expectation) is  m.some_method.assert_called_once_with('foo', 'baz').

So the traceback is correct. I don't think the ordering of expected/actual in the output matters.
msg220115 - (view) Author: Fei Long Wang (flwang) Date: 2014-06-09 20:08
Okay, I can see your point now. Thanks for the clarification.
Date User Action Args
2022-04-11 14:58:04adminsetgithub: 65891
2014-06-09 20:08:48flwangsetmessages: + msg220115
2014-06-09 09:21:14michael.foordsetstatus: open -> closed
resolution: not a bug
messages: + msg220086
2014-06-08 23:10:49r.david.murraysetnosy: + r.david.murray
messages: + msg220067
2014-06-08 22:25:21flwangsetmessages: + msg220063
2014-06-08 09:30:38michael.foordsetmessages: + msg220029
2014-06-08 08:12:23ned.deilysetnosy: + michael.foord
2014-06-08 07:37:07flwangcreate