classification
Title: Align expected and actual calls on mock.assert_called_with error message
Type: enhancement Stage: test needed
Components: Library (Lib) Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Adnan Umer, cjw296, mariocj89, michael.foord, taleinat, terry.reedy, xtreak
Priority: normal Keywords:

Created on 2018-12-14 18:45 by xtreak, last changed 2019-01-02 23:43 by terry.reedy.

Messages (7)
msg331849 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python triager) Date: 2018-12-14 18:45
Currently, assert_called_with has expected calls list in the same line with AssertionError that causes the visualizing the difference to be hard. It will be great if Expected call occurs on the next line so that the diff is improved. The change has to be made at https://github.com/python/cpython/blob/f8e9bd568adf85c1e4aea1dda542a96b027797e2/Lib/unittest/mock.py#L749 .

from unittest import mock

m = mock.Mock()
m(1, 2)
m.assert_called_with(2, 3)

Current output : 

Traceback (most recent call last):
  File "/tmp/bar.py", line 5, in <module>
    m.assert_called_with(2, 3)
  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)
Actual call: mock(1, 2)

Proposed output : 

Traceback (most recent call last):
  File "/tmp/bar.py", line 5, in <module>
    m.assert_called_with(2, 3)
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/unittest/mock.py", line 827, in assert_called_with
    raise AssertionError(_error_message()) from cause
AssertionError:
Expected call: mock(2, 3)
Actual call: mock(1, 2)

Some more alignment with the call list starting in the same column

AssertionError:
Expected call: mock(2, 3)
Actual call:   mock(1, 2)


Originally reported in the GitHub repo at https://github.com/testing-cabal/mock/issues/424 . PR for this was closed since GitHub is used only for backporting (https://github.com/testing-cabal/mock/pull/425). I thought to report it here for discussion. Currently call list output is as per proposed output.

AssertionError: Calls not found.
Expected: [call(1, 2, 3)]
Actual: [call(1, 2)].
msg331909 - (view) Author: Mario Corchero (mariocj89) * (Python triager) Date: 2018-12-15 22:27
Makes sense!

I'd not align them though but that might be my view as I generally don't like aligning text like that.

Also if you feel that the exceptions read "weird" with the first sentence is empty, an option might be to say the calls don't match, to make it symmetric with the assert_calls message. Example:


Traceback (most recent call last):
  File "/tmp/bar.py", line 5, in <module>
    m.assert_called_with(2, 3)
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/unittest/mock.py", line 827, in assert_called_with
    raise AssertionError(_error_message()) from cause
AssertionError: Expected call not found.
Expected call: mock(2, 3)
Actual call: mock(1, 2)

This way all error reports give you the issue in the first line of the message and further details in the next lines.
msg331916 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python triager) Date: 2018-12-16 05:05
Thanks Mario for the feedback. The alignment was just a personal preference of mine. I agree with you on adding "Expected call not found" next to AssertionError. I will wait if others have more feedback on this before proceeding on the PR.
msg332302 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2018-12-21 21:41
We don't usually wrap error messages, but this is plausible.  With the addition to the first line, we don't need to repeat 'call'.  We can line things up without violating the PEP 8 recommendation against doing so with spaces.  Also, the convention is to not capitolize the phrase after ':'.

AssertionError: expected call not found.
Expect: mock(2, 3)
Actual: mock(1, 2)
msg332303 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python triager) Date: 2018-12-21 22:00
Thanks for the feedback. I personally prefer 'expected' than 'expect' though it comes at the cost that this cannot be aligned with 'actual'. Other places use 'expected' and it reads more natural in test case scenarios.
msg332893 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2019-01-02 20:34
Perhaps "expected" and "observed" or "detected"?
msg332900 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2019-01-02 23:43
"Expected" and "Observed" seem good.  I like "Received" slightly better, but would not argue with PR author.  It depends on whether one anthropomorphizes the assert function (or test machinery) as saying 'I expected to see' or 'I expected to get'.
History
Date User Action Args
2019-01-02 23:43:08terry.reedysetstage: test needed
messages: + msg332900
versions: + Python 3.8
2019-01-02 20:34:43taleinatsetnosy: + taleinat
messages: + msg332893
2018-12-24 15:31:02cjw296setmessages: - msg332464
2018-12-24 15:30:42cjw296setmessages: - msg332462
2018-12-24 15:20:18Adnan Umersetmessages: + msg332464
2018-12-24 15:15:39Adnan Umersetnosy: + Adnan Umer

messages: + msg332462
versions: - Python 3.8
2018-12-21 22:00:07xtreaksetmessages: + msg332303
2018-12-21 21:41:47terry.reedysetnosy: + terry.reedy
messages: + msg332302
2018-12-16 05:05:01xtreaksetmessages: + msg331916
2018-12-15 22:27:29mariocj89setmessages: + msg331909
2018-12-14 18:45:46xtreakcreate