Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spy: handle AttributeError with __getattribute__ #42

Merged
merged 1 commit into from
May 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@
functions which receive a parameter named ``method``.
Thanks `@sagarchalise`_ for the report (`#31`_).

* Fix AttributeError with ``mocker.spy`` when spying on inherited methods
(`#42`_).

.. _@sagarchalise: https://github.com/sagarchalise
.. _@satyrius: https://github.com/satyrius
.. _#31: https://github.com/pytest-dev/pytest-mock/issues/31
.. _#32: https://github.com/pytest-dev/pytest-mock/issues/32
.. _#42: https://github.com/pytest-dev/pytest-mock/issues/42

0.10.1
------
Expand Down
12 changes: 8 additions & 4 deletions pytest_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,15 @@ def spy(self, obj, name):
# Can't use autospec classmethod or staticmethod objects
# see: https://bugs.python.org/issue23078
if inspect.isclass(obj):
# bypass class descriptor:
# 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
try:
value = obj.__getattribute__(obj, name)
except AttributeError:
pass
else:
if isinstance(value, (classmethod, staticmethod)):
autospec = False

result = self.patch.object(obj, name, side_effect=method,
autospec=autospec)
Expand Down
57 changes: 57 additions & 0 deletions test_pytest_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,27 @@ def bar(self, arg):
assert spy.call_args_list == calls


@skip_pypy
def test_instance_method_by_subclass_spy(mocker):
from pytest_mock import mock_module

class Base(object):

def bar(self, arg):
return arg * 2

class Foo(Base):
pass

spy = mocker.spy(Foo, 'bar')
foo = Foo()
other = Foo()
assert foo.bar(arg=10) == 20
assert other.bar(arg=10) == 20
calls = [mock_module.call(foo, arg=10), mock_module.call(other, arg=10)]
assert spy.call_args_list == calls


@skip_pypy
def test_class_method_spy(mocker):
class Foo(object):
Expand All @@ -225,6 +246,24 @@ def bar(cls, arg):
spy.assert_called_once_with(arg=10)


@skip_pypy
@pytest.mark.xfail(sys.version_info[0] == 2, reason='does not work on Python 2')
def test_class_method_subclass_spy(mocker):
class Base(object):

@classmethod
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this supposed to be a classmethod?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it is now - I was pushing a fixup/amended commit.

def bar(self, arg):
return arg * 2

class Foo(Base):
pass

spy = mocker.spy(Foo, 'bar')
assert Foo.bar(arg=10) == 20
Foo.bar.assert_called_once_with(arg=10)
spy.assert_called_once_with(arg=10)


@skip_pypy
def test_class_method_with_metaclass_spy(mocker):
class MetaFoo(type):
Expand Down Expand Up @@ -258,6 +297,24 @@ def bar(arg):
spy.assert_called_once_with(arg=10)


@skip_pypy
@pytest.mark.xfail(sys.version_info[0] == 2, reason='does not work on Python 2')
def test_static_method_subclass_spy(mocker):
class Base(object):

@staticmethod
def bar(arg):
return arg * 2

class Foo(Base):
pass

spy = mocker.spy(Foo, 'bar')
assert Foo.bar(arg=10) == 20
Foo.bar.assert_called_once_with(arg=10)
spy.assert_called_once_with(arg=10)


@contextmanager
def assert_traceback():
"""
Expand Down