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.

classification
Title: mock.patch with autospec does not consume self / cls argument
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.9, Python 3.8, Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: cbelu, cjw296, lisroach, mariocj89, michael.foord, pconnell, xtreak
Priority: normal Keywords: patch

Created on 2017-11-20 15:50 by cbelu, last changed 2022-04-11 14:58 by admin.

Pull Requests
URL Status Linked Edit
PR 4476 open cbelu, 2017-11-20 16:02
Messages (2)
msg306559 - (view) Author: Claudiu Belu (cbelu) * Date: 2017-11-20 15:50
Currently, the autospec=True argument can be passed to mock.patch, but when trying to make assertions on the call and its arguments, it can fail, as it expects an instance / class instance reference as the first argument (self / cls arguments are not consumed when autospec-ing).

Steps to reproduce:

>>> import mock
>>> from mock.tests import testmock
>>>
>>> with mock.patch.object(testmock.Something, 'meth', autospec=True):
...     smth = testmock.Something()
...     smth.meth(mock.sentinel.a, mock.sentinel.b, mock.sentinel.c, mock.sentinel.d)
...     smth.meth.assert_called_once_with(mock.sentinel.a, mock.sentinel.b, mock.sentinel.c, mock.sentinel.d)
...
<MagicMock name='meth()' id='139792133225424'>
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "/usr/local/lib/python2.7/dist-packages/mock/mock.py", line 318, in assert_called_once_with
    return mock.assert_called_once_with(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/mock/mock.py", line 948, in assert_called_once_with
    return self.assert_called_with(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/mock/mock.py", line 937, in assert_called_with
    six.raise_from(AssertionError(_error_message(cause)), cause)
  File "/usr/local/lib/python2.7/dist-packages/six.py", line 718, in raise_from
    raise value
AssertionError: Expected call: meth(sentinel.a, sentinel.b, sentinel.c, sentinel.d)
Actual call: meth(<mock.tests.testmock.Something object at 0x7f23e456dcd0>, sentinel.a, sentinel.b, sentinel.c, sentinel.d)

Expected result: no AssertionError.
msg306560 - (view) Author: Claudiu Belu (cbelu) * Date: 2017-11-20 15:58
Currently, the autospec=True argument can be passed to mock.patch, but when trying to make assertions on the call and its arguments, it can fail, as it expects an instance / class instance reference as the first argument (self / cls arguments are not consumed when autospec-ing).

Steps to reproduce:

ubuntu@ubuntu:/opt/stack/cpython$ ./python
Python 3.7.0a2+ (heads/mock-add-autospec:51a9270, Nov 20 2017, 06:48:44)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from unittest import mock
from unittest.test.testmock import testmock

with mock.patch.object(testmock.Something, 'meth', autospec=True):
    smth = testmock.Something()
    smth.meth(mock.sentinel.a, mock.sentinel.b, mock.sentinel.c, mock.sentinel.d)
    smth.meth.assert_called_once_with(mock.sentinel.a, mock.sentinel.b, mock.sentinel.c, mock.sentinel.d)

>>> >>> >>> ... ... ... ... <MagicMock name='meth()' id='140281858112024'>
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "/opt/stack/cpython/Lib/unittest/mock.py", line 196, in assert_called_once_with
    return mock.assert_called_once_with(*args, **kwargs)
  File "/opt/stack/cpython/Lib/unittest/mock.py", line 856, in assert_called_once_with
    return self.assert_called_with(*args, **kwargs)
  File "/opt/stack/cpython/Lib/unittest/mock.py", line 845, in assert_called_with
    raise AssertionError(_error_message()) from cause
AssertionError: Expected call: meth(sentinel.a, sentinel.b, sentinel.c, sentinel.d)
Actual call: meth(<unittest.test.testmock.testmock.Something object at 0x7f95ea529a80>, sentinel.a, sentinel.b, sentinel.c, sentinel.d)

Expected result: no AssertionError.
History
Date User Action Args
2022-04-11 14:58:54adminsetgithub: 76273
2019-10-28 20:46:16pconnellsetnosy: + pconnell
2019-09-13 17:04:23xtreaksetnosy: + cjw296, lisroach, mariocj89, xtreak

versions: + Python 3.9, - Python 2.7
2018-05-21 22:17:20cheryl.sabellasetnosy: + michael.foord

versions: - Python 3.5, Python 3.6
2017-11-20 16:02:47cbelusetkeywords: + patch
stage: patch review
pull_requests: + pull_request4412
2017-11-20 15:58:04cbelusetmessages: + msg306560
2017-11-20 15:51:27cbelusetcomponents: + Library (Lib)
2017-11-20 15:50:34cbelusettype: behavior
2017-11-20 15:50:21cbelucreate