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

Track return value of spied functions #155

Merged
merged 1 commit into from
Sep 28, 2019
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
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
1.11.0
------

* The object returned by ``mocker.spy`` now also tracks the return value
of the spied method/function.

1.10.4
------

Expand Down
6 changes: 4 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,9 @@ These objects from the ``mock`` module are accessible directly from ``mocker`` f
Spy
---

The spy acts exactly like the original method in all cases, except it allows use of `mock`
The spy acts exactly like the original method in all cases, except it allows use of ``mock``
features with it, like retrieving call count. It also works for class and static methods.


.. code-block:: python

def test_spy(mocker):
Expand All @@ -101,6 +100,9 @@ features with it, like retrieving call count. It also works for class and static
assert foo.bar() == 42
assert foo.bar.call_count == 1

Since version ``1.11``, it is also possible to query the ``return_value`` attribute
to observe what the spied function/method returned.

Stub
----

Expand Down
9 changes: 8 additions & 1 deletion pytest_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import inspect
import sys
from functools import wraps

import pytest

Expand Down Expand Up @@ -104,7 +105,13 @@ def spy(self, obj, name):
if isinstance(value, (classmethod, staticmethod)):
autospec = False

result = self.patch.object(obj, name, side_effect=method, autospec=autospec)
@wraps(method)
def wrapper(*args, **kwargs):
r = method(*args, **kwargs)
result.return_value = r
return r

result = self.patch.object(obj, name, side_effect=wrapper, autospec=autospec)
return result

def stub(self, name=None):
Expand Down
13 changes: 13 additions & 0 deletions test_pytest_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,9 @@ def bar(self, arg):
assert foo.bar(arg=10) == 20
assert other.bar(arg=10) == 20
foo.bar.assert_called_once_with(arg=10)
assert foo.bar.return_value == 20
spy.assert_called_once_with(arg=10)
assert spy.return_value == 20


@skip_pypy
Expand Down Expand Up @@ -272,6 +274,7 @@ class Foo(Base):
assert other.bar(arg=10) == 20
calls = [mocker.call(foo, arg=10), mocker.call(other, arg=10)]
assert spy.call_args_list == calls
assert spy.return_value == 20


@skip_pypy
Expand All @@ -284,7 +287,9 @@ def bar(cls, arg):
spy = mocker.spy(Foo, "bar")
assert Foo.bar(arg=10) == 20
Foo.bar.assert_called_once_with(arg=10)
assert Foo.bar.return_value == 20
spy.assert_called_once_with(arg=10)
assert spy.return_value == 20


@skip_pypy
Expand All @@ -301,7 +306,9 @@ class Foo(Base):
spy = mocker.spy(Foo, "bar")
assert Foo.bar(arg=10) == 20
Foo.bar.assert_called_once_with(arg=10)
assert Foo.bar.return_value == 20
spy.assert_called_once_with(arg=10)
assert spy.return_value == 20


@skip_pypy
Expand All @@ -320,7 +327,9 @@ def bar(cls, arg):
spy = mocker.spy(Foo, "bar")
assert Foo.bar(arg=10) == 20
Foo.bar.assert_called_once_with(arg=10)
assert Foo.bar.return_value == 20
spy.assert_called_once_with(arg=10)
assert spy.return_value == 20


@skip_pypy
Expand All @@ -333,7 +342,9 @@ def bar(arg):
spy = mocker.spy(Foo, "bar")
assert Foo.bar(arg=10) == 20
Foo.bar.assert_called_once_with(arg=10)
assert Foo.bar.return_value == 20
spy.assert_called_once_with(arg=10)
assert spy.return_value == 20


@skip_pypy
Expand All @@ -350,7 +361,9 @@ class Foo(Base):
spy = mocker.spy(Foo, "bar")
assert Foo.bar(arg=10) == 20
Foo.bar.assert_called_once_with(arg=10)
assert Foo.bar.return_value == 20
spy.assert_called_once_with(arg=10)
assert spy.return_value == 20


@contextmanager
Expand Down