Skip to content

Commit ca8bed5

Browse files
authored
Track return value of spied functions (#155)
Related to #118
1 parent 540f682 commit ca8bed5

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

CHANGELOG.rst

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
1.11.0
2+
------
3+
4+
* The object returned by ``mocker.spy`` now also tracks the return value
5+
of the spied method/function.
6+
17
1.10.4
28
------
39

README.rst

+4-2
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,9 @@ These objects from the ``mock`` module are accessible directly from ``mocker`` f
8585
Spy
8686
---
8787

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

91-
9291
.. code-block:: python
9392
9493
def test_spy(mocker):
@@ -101,6 +100,9 @@ features with it, like retrieving call count. It also works for class and static
101100
assert foo.bar() == 42
102101
assert foo.bar.call_count == 1
103102
103+
Since version ``1.11``, it is also possible to query the ``return_value`` attribute
104+
to observe what the spied function/method returned.
105+
104106
Stub
105107
----
106108

pytest_mock.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import inspect
44
import sys
5+
from functools import wraps
56

67
import pytest
78

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

107-
result = self.patch.object(obj, name, side_effect=method, autospec=autospec)
108+
@wraps(method)
109+
def wrapper(*args, **kwargs):
110+
r = method(*args, **kwargs)
111+
result.return_value = r
112+
return r
113+
114+
result = self.patch.object(obj, name, side_effect=wrapper, autospec=autospec)
108115
return result
109116

110117
def stub(self, name=None):

test_pytest_mock.py

+13
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,9 @@ def bar(self, arg):
238238
assert foo.bar(arg=10) == 20
239239
assert other.bar(arg=10) == 20
240240
foo.bar.assert_called_once_with(arg=10)
241+
assert foo.bar.return_value == 20
241242
spy.assert_called_once_with(arg=10)
243+
assert spy.return_value == 20
242244

243245

244246
@skip_pypy
@@ -272,6 +274,7 @@ class Foo(Base):
272274
assert other.bar(arg=10) == 20
273275
calls = [mocker.call(foo, arg=10), mocker.call(other, arg=10)]
274276
assert spy.call_args_list == calls
277+
assert spy.return_value == 20
275278

276279

277280
@skip_pypy
@@ -284,7 +287,9 @@ def bar(cls, arg):
284287
spy = mocker.spy(Foo, "bar")
285288
assert Foo.bar(arg=10) == 20
286289
Foo.bar.assert_called_once_with(arg=10)
290+
assert Foo.bar.return_value == 20
287291
spy.assert_called_once_with(arg=10)
292+
assert spy.return_value == 20
288293

289294

290295
@skip_pypy
@@ -301,7 +306,9 @@ class Foo(Base):
301306
spy = mocker.spy(Foo, "bar")
302307
assert Foo.bar(arg=10) == 20
303308
Foo.bar.assert_called_once_with(arg=10)
309+
assert Foo.bar.return_value == 20
304310
spy.assert_called_once_with(arg=10)
311+
assert spy.return_value == 20
305312

306313

307314
@skip_pypy
@@ -320,7 +327,9 @@ def bar(cls, arg):
320327
spy = mocker.spy(Foo, "bar")
321328
assert Foo.bar(arg=10) == 20
322329
Foo.bar.assert_called_once_with(arg=10)
330+
assert Foo.bar.return_value == 20
323331
spy.assert_called_once_with(arg=10)
332+
assert spy.return_value == 20
324333

325334

326335
@skip_pypy
@@ -333,7 +342,9 @@ def bar(arg):
333342
spy = mocker.spy(Foo, "bar")
334343
assert Foo.bar(arg=10) == 20
335344
Foo.bar.assert_called_once_with(arg=10)
345+
assert Foo.bar.return_value == 20
336346
spy.assert_called_once_with(arg=10)
347+
assert spy.return_value == 20
337348

338349

339350
@skip_pypy
@@ -350,7 +361,9 @@ class Foo(Base):
350361
spy = mocker.spy(Foo, "bar")
351362
assert Foo.bar(arg=10) == 20
352363
Foo.bar.assert_called_once_with(arg=10)
364+
assert Foo.bar.return_value == 20
353365
spy.assert_called_once_with(arg=10)
366+
assert spy.return_value == 20
354367

355368

356369
@contextmanager

0 commit comments

Comments
 (0)