classification
Title: assert_has_calls output is formatted inconsistently
Type: enhancement Stage: resolved
Components: Tests Versions: Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: orsenthil Nosy List: ezio.melotti, michael.foord, orsenthil, python-dev, rzimmerman
Priority: normal Keywords: patch

Created on 2015-10-08 22:08 by rzimmerman, last changed 2016-01-12 14:20 by orsenthil. This issue is now closed.

Files
File name Uploaded Description Edit
mock_assert_has_calls.patch rzimmerman, 2015-10-08 22:08 format expected calls using _CallList review
Messages (4)
msg252569 - (view) Author: Robert Zimmerman (rzimmerman) * Date: 2015-10-08 22:08
For longer lists of calls, the "Expected" list is printed all on one line while the "Actual" list is pprinted and on multiple lines. This makes it hard to do a visual compare of which calls are missing/incorrect.

Example:

    AssertionError: Calls not found.
    Expected: [call('bbbbbbbbbbbbbbbbbbbb'), call('bbbbbbbbbbbbbbbbbbbb'), call('bbbbbbbbbbbbbbbbbbbb'), call('bbbbbbbbbbbbbbbbbbbb'), call('bbbbbbbbbbbbbbbbbbbb'), call('bbbbbbbbbbbbbbbbbbbb'), call('bbbbbbbbbbbbbbbbbbbb')]
    Actual: [call('aaaaaaaaaaaaaaaaaaa'),
     call('aaaaaaaaaaaaaaaaaaa'),
     call('aaaaaaaaaaaaaaaaaaa'),
     call('aaaaaaaaaaaaaaaaaaa'),
     call('aaaaaaaaaaaaaaaaaaa'),
     call('aaaaaaaaaaaaaaaaaaa'),
     call('aaaaaaaaaaaaaaaaaaa')]


What I'd expect:

    AssertionError: Calls not found.
    Expected: [call('bbbbbbbbbbbbbbbbbbbb'),
     call('bbbbbbbbbbbbbbbbbbbb'),
     call('bbbbbbbbbbbbbbbbbbbb'),
     call('bbbbbbbbbbbbbbbbbbbb'),
     call('bbbbbbbbbbbbbbbbbbbb'),
     call('bbbbbbbbbbbbbbbbbbbb'),
     call('bbbbbbbbbbbbbbbbbbbb')]
    Actual: [call('aaaaaaaaaaaaaaaaaaa'),
     call('aaaaaaaaaaaaaaaaaaa'),
     call('aaaaaaaaaaaaaaaaaaa'),
     call('aaaaaaaaaaaaaaaaaaa'),
     call('aaaaaaaaaaaaaaaaaaa'),
     call('aaaaaaaaaaaaaaaaaaa'),
     call('aaaaaaaaaaaaaaaaaaa')]
msg258102 - (view) Author: Senthil Kumaran (orsenthil) * (Python committer) Date: 2016-01-12 14:08
The patch looks good to me. The change is in the error message of the AssertionError and don't test the error messages of Exceptions in our unittest. This change is a good usability improvement and I will commit this patch.
msg258103 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-01-12 14:18
New changeset 77d24f51effc by Senthil Kumaran in branch 'default':
Issue25347 - Format the error message output of mock's assert_has_calls method.
https://hg.python.org/cpython/rev/77d24f51effc
msg258104 - (view) Author: Senthil Kumaran (orsenthil) * (Python committer) Date: 2016-01-12 14:20
Verified the change and committed.

The new output behavior will be like this.

```
>>> from unittest.mock import Mock
>>> mock = Mock(return_value=None)
>>> for i in range(1, 10):
...     mock(i)
...
>>> from unittest.mock import call
>>> calls = [call(i) for i in range(2, 10)]
>>> mock.assert_has_calls(calls)
>>> calls.append(call(100))
>>> mock.assert_has_calls(calls)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/senthilkumaran/python/cpython/Lib/unittest/mock.py", line 824, in assert_has_calls
    ) from cause
AssertionError: Calls not found.
Expected: [call(2),
 call(3),
 call(4),
 call(5),
 call(6),
 call(7),
 call(8),
 call(9),
 call(100)]
Actual: [call(1),
 call(2),
 call(3),
 call(4),
 call(5),
 call(6),
 call(7),
 call(8),
 call(9)]
```
History
Date User Action Args
2016-01-12 14:20:04orsenthilsetstatus: open -> closed
resolution: fixed
messages: + msg258104

stage: patch review -> resolved
2016-01-12 14:18:53python-devsetnosy: + python-dev
messages: + msg258103
2016-01-12 14:08:14orsenthilsetassignee: orsenthil

messages: + msg258102
nosy: + orsenthil
2016-01-04 03:34:18ezio.melottisetnosy: + ezio.melotti

stage: patch review
2015-10-09 20:26:15terry.reedysetversions: - Python 2.7, Python 3.2, Python 3.3, Python 3.4, Python 3.5
2015-10-08 22:08:43rzimmermancreate