Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock called_with does not ensure self/cls argument is used #75722

Closed
JonathanHuot mannequin opened this issue Sep 21, 2017 · 3 comments
Closed

Mock called_with does not ensure self/cls argument is used #75722

JonathanHuot mannequin opened this issue Sep 21, 2017 · 3 comments
Labels
3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@JonathanHuot
Copy link
Mannequin

JonathanHuot mannequin commented Sep 21, 2017

BPO 31541
Nosy @pablogsal, @JonathanHuot, @tirkarthi

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2018-11-13.03:21:01.320>
created_at = <Date 2017-09-21.09:35:48.433>
labels = ['3.7', 'invalid', 'type-bug', 'library']
title = 'Mock called_with does not ensure self/cls argument is used'
updated_at = <Date 2018-11-13.03:21:10.267>
user = 'https://github.com/JonathanHuot'

bugs.python.org fields:

activity = <Date 2018-11-13.03:21:10.267>
actor = 'pablogsal'
assignee = 'none'
closed = True
closed_date = <Date 2018-11-13.03:21:01.320>
closer = 'pablogsal'
components = ['Library (Lib)']
creation = <Date 2017-09-21.09:35:48.433>
creator = 'jonathan.huot'
dependencies = []
files = []
hgrepos = []
issue_num = 31541
keywords = []
message_count = 3.0
messages = ['302677', '329721', '329800']
nosy_count = 3.0
nosy_names = ['pablogsal', 'jonathan.huot', 'xtreak']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue31541'
versions = ['Python 3.3', 'Python 3.4', 'Python 3.5', 'Python 3.6', 'Python 3.7']

@JonathanHuot
Copy link
Mannequin Author

JonathanHuot mannequin commented Sep 21, 2017

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

@JonathanHuot JonathanHuot mannequin added 3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Sep 21, 2017
@tirkarthi
Copy link
Member

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.

@pablogsal
Copy link
Member

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.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants