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 called_with does not ensure self/cls argument is used
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7, Python 3.6, Python 3.3, Python 3.4, Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: jonathan.huot, pablogsal, xtreak
Priority: normal Keywords:

Created on 2017-09-21 09:35 by jonathan.huot, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg302677 - (view) Author: JonathanHuot (jonathan.huot) Date: 2017-09-21 09:35
Mock "assert_called_with" does not contain a possibility to verify if "self" or "cls" is used when mock is called.

So, in unittests, all tests are passing but code is broken. Example :


Steps to reproduce:
==================

class Something(object):
    def foobar(self):
        pass

    def foo(self):
        self.foobar()

    def bar(self):
        Something.foobar()  # this is broken


from unittest import mock
x = mock.Mock(spec=Something)
x.foo()
x.foo.assert_called_with()
x.bar()
x.bar.assert_called_with()  # this assertion pass!

# real code 
z = Something()
z.foo()
z.bar()  # raise exception
msg329721 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-11-12 07:05
mock can only verify if the function is called with the correct number of arguments as you have passed spec. It doesn't verify whether the function implementation is correct like calling Something.foobar() because it's not designed to verify the function implementation. It wouldn't raise an exception even when self.foo(1, 2) is called inside x.bar because runtime code is not executed and only the signature is checked with mock. I think this is an expected behavior and not a bug.
msg329800 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2018-11-13 03:20
I agree with Karthikeyan: This is is the expected behaviour. The moment you use a mock, any implementation details are lost unless you use wraps or similar to also transfer the call to the/a real object.
History
Date User Action Args
2022-04-11 14:58:52adminsetgithub: 75722
2018-11-13 03:21:10pablogsalsetmessages: - msg329801
2018-11-13 03:21:01pablogsalsetstatus: open -> closed
resolution: not a bug
messages: + msg329801

stage: resolved
2018-11-13 03:20:35pablogsalsetnosy: + pablogsal
messages: + msg329800
2018-11-12 07:05:32xtreaksetmessages: + msg329721
2018-09-25 12:36:41xtreaksetnosy: + xtreak
2017-09-21 09:35:48jonathan.huotcreate