How to mock a property

hek2mgl picture hek2mgl · Mar 9, 2017 · Viewed 9k times · Source

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.

Answer

jsbueno picture jsbueno · Mar 9, 2017

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)