Issue31541
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) * ![]() |
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) * ![]() |
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:52 | admin | set | github: 75722 |
| 2018-11-13 03:21:10 | pablogsal | set | messages: - msg329801 |
| 2018-11-13 03:21:01 | pablogsal | set | status: open -> closed resolution: not a bug messages: + msg329801 stage: resolved |
| 2018-11-13 03:20:35 | pablogsal | set | nosy:
+ pablogsal messages: + msg329800 |
| 2018-11-12 07:05:32 | xtreak | set | messages: + msg329721 |
| 2018-09-25 12:36:41 | xtreak | set | nosy:
+ xtreak |
| 2017-09-21 09:35:48 | jonathan.huot | create | |
