- Python Home
- About
- News
- Documentation
- Downloads
- Community
- Foundation
- Developer's Guide
- Issue Tracker
- Issues
- Summaries
- User
- Administration
- Help
➜
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.
Created on 2018-12-24 15:19 by Adnan Umer, last changed 2022-04-11 14:59 by admin. This issue is now closed.
| Messages (3) | ||
|---|---|---|
| msg332463 - (view) | Author: Adnan Umer (Adnan Umer) | Date: 2018-12-24 15:19 |
When a method/bounded function is mocked and side_effect is supplied to it, the side_effect function doesn't get the reference to the instance.
Suppose we have something like this
class SomeClass:
def do_something(self, x):
pass
def some_function(x):
cls = SomeClass()
y = class.do_something(x)
return y
And the test for some_function will be
def do_something_side_effect(x):
retrun x
def test_some_function():
with mock.path("SomeCass.do_something") as do_something_mock:
do_something_mock.side_effect = do_something_side_effect
assert some_function(1)
Here do_something_side_effect mock will not have access to SomeClass instance.
|
||
| msg332848 - (view) | Author: Rémi Lapeyre (remi.lapeyre) * | Date: 2019-01-02 00:41 |
Is there a problem with:
from unittest import mock
class SomeClass:
def do_something(self, x):
pass
def some_function(x):
obj = SomeClass()
y = obj.do_something(x)
return y
def do_something_side_effect(self, x):
print(self)
return x
def test_some_function():
with mock.patch.object(SomeClass, "do_something", do_something_side_effect):
assert some_function(1)
?
|
||
| msg335548 - (view) | Author: Karthikeyan Singaravelan (xtreak) * ![]() |
Date: 2019-02-14 18:04 |
Thanks for the report. I think this is design by choice that self is not passed to the side_effect directly set on the mock [0]. Changing this would break existing tests like [1] . You can use the approach by @remi.lapeyre which directly replaces the function on the target inside the context manager at [2] passing self to the side_effect callable. [0] https://github.com/python/cpython/blob/8a1657b93469580ef345c7c91738587f3d76e87d/Lib/unittest/mock.py#L1043 [1] https://github.com/python/cpython/blob/8a1657b93469580ef345c7c91738587f3d76e87d/Lib/unittest/test/testmock/testmock.py#L664 [2] https://github.com/python/cpython/blob/8a1657b93469580ef345c7c91738587f3d76e87d/Lib/unittest/mock.py#L1376 |
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022-04-11 14:59:09 | admin | set | github: 79758 |
| 2022-03-19 19:47:30 | iritkatriel | set | status: open -> closed resolution: not a bug stage: resolved |
| 2019-02-14 18:04:09 | xtreak | set | nosy:
+ cjw296, xtreak, mariocj89 messages: + msg335548 versions: - Python 2.7, Python 3.4, Python 3.5, Python 3.6 |
| 2019-01-02 00:41:13 | remi.lapeyre | set | nosy:
+ remi.lapeyre messages: + msg332848 |
| 2018-12-24 15:19:00 | Adnan Umer | create |
Powered by Roundup Copyright © 1990-2022, Python Software Foundation
Legal Statements

