How to mock a property

Question:

I’m asking how to mock a class property in a unit test using Python 3. I’ve tried the following, which makes sense for me following the docs, but it doesn’t work:

foo.py:

class Foo():
    @property
    def bar(self):
        return 'foobar'


def test_foo_bar(mocker):
    foo = Foo()
    mocker.patch.object(foo, 'bar', new_callable=mocker.PropertyMock)
    print(foo.bar)

I’ve installed pytest and pytest_mock and run the test like this:

pytest foo.py

I got the following error:

>       setattr(self.target, self.attribute, new_attr)
E       AttributeError: can't set attribute

/usr/lib/python3.5/unittest/mock.py:1312: AttributeError

My expectation would be that the test runs without errors.

Asked By: hek2mgl

||

Answers:

The property mechanism relies on the property attribute being defined on the object’s class. You can’t create a “property like” method or attribute on a single instance of a class (for a better understanding, read about Python’s descriptor protocol)

Therefore you have to apply the patch to your class – you can use the with statement so that the class is properly restored after your test:

def test_foo_bar(mock):
    foo = Foo()
    with mock.patch(__name__ + "Foo.bar", new=mocker.PropertyMock)
        print(foo.bar)
Answered By: jsbueno

You can wrap MagicMocks with a call to function property:

from mock import patch, MagicMock

@patch('foo.Foo.bar', property(MagicMock(return_value='mocked_property_value')))
def test_foo_bar():
    foo = Foo()    
    print(foo.bar)
Answered By: Stefan