Comments (5)
Is there any progress?
from pytest-mock.
This a nice feature that I didn't even think about because I guess never really mocked a property before.
Below it is a draft I've come up in a hurry that is really buggy but get the basics done:
def spy(self, obj, name):
if isinstance(getattr(obj, name), property):
prop = getattr(obj, name)
class SpyPropertyMock(mock_module.PropertyMock):
def __get__(self, obj, obj_type):
self()
return prop.__get__(obj)
def __set__(self, obj, val):
self(val)
prop.fset(obj, val)
result = self.patch.object(obj, name, new_callable=SpyPropertyMock)
return result
else:
method = getattr(obj, name)
autospec = inspect.ismethod(method) or inspect.isfunction(method)
# Can't use autospec classmethod or staticmethod objects
# see: https://bugs.python.org/issue23078
if inspect.isclass(obj):
# bypass class descriptor:
# http://stackoverflow.com/questions/14187973/python3-check-if-method-is-static
value = obj.__getattribute__(obj, name)
if isinstance(value, (classmethod, staticmethod)):
autospec = False
result = self.patch.object(obj, name, side_effect=method,
autospec=autospec)
return result
A test using it:
def testFoo(mocker):
mocked = mocker.spy(Foo, 'foo')
foo = Foo()
foo.foo = 'b'
assert foo.foo == 'b'
assert foo.foo != 'c'
assert foo.foo != 'd'
print mocked.mock_calls # [call(u'b'), call(), call(), call()]
With this, the mock object returned can be used to inspect mock_calls
to property and property value is in fact changed.
Problems I've seen so far:
- I'm unable to access
Foo.foo
without getting errors. - Different from method spies, object accessed by
foo
attr isn't a mock object, so it is only possible to access mock call objects through returned object.
Anyway I have to get more used to PropertyMock
before moving on, let me know more details about your problems and implementation (maybe open a PR?) because I can try to help :)
from pytest-mock.
I'm not sure @blueyed... I think it would certainly be an useful feature. Do you have some sample code to share?
cc @fogo
from pytest-mock.
@nicoddemus
My idea was to first patch the class to allow setting a new attribute (using PropertyMock
) and then patch the attribute itself.
But it felt like having to patch/use fget
/fset
from the property
class itself.
from pytest-mock.
I don't think so, but I would be happy to review and merge a PR! 😁
from pytest-mock.
Related Issues (20)
- mocker.patch does not change return value HOT 3
- missing 1 required positional argument: 'mocker' HOT 3
- assert_called_once_with calls __eq__ multiple times HOT 3
- sdist is missing tox.ini HOT 1
- Spying on pydantic objects HOT 1
- Changelog broken HOT 2
- Mock method don't work when test from other file fail HOT 4
- async_stub doesn't need to be awaited HOT 1
- mocker.stub() is a coroutine function HOT 1
- feature request: fail on improper use of mock.side_effect HOT 2
- Record multiple calls to spy HOT 2
- How to patch return_value attribute? HOT 2
- The documentation should be clearer about not supporting context managers HOT 1
- Not sure why this is not working HOT 3
- `resetall` does not work when using `create_autospec` HOT 2
- Behavior of spy on class attributes HOT 3
- Call args/kwargs stored in a Spy can be overwritten without intention HOT 1
- test_failure_message_with_no_name fails with 3.11.7/3.12.1 HOT 1
- Failing tests for python 3.12 HOT 1
- Allow for easier import of `mocker.Mock | mocker.MagicMock` types
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pytest-mock.