Make spy a context manager
See original GitHub issueCapture return value
NOTE: already implemented in 1.11
class SomeClass:
def method(self, a):
return a * 2
def facade(self):
self.method(4)
return 'Done'
def test_some_class(mocker):
spy = mocker.spy(SomeClass, 'method')
o = SomeClass()
o.facade()
assert spy.called
assert spy.call_count == 1
print(spy.mock_calls[0])
# nicely outputs method arguments
# call(<m.SomeClass object at 0x7febfed74518>, 4)
print(spy.return_value)
# just tells us we have mock object here
# <MagicMock name='method()' id='140651569552856'>
# it would be much better to have there
# assert spy.return_value == 8
# or, rather
# assert spy.mock_calls[0].return_value == 8
Spy as context manager
That’s probably kind of misusing mocker object, but in my case there’s a quite long test and I want to restrict fragment of code where particular spy object is applied.
def test_context_mgr(mocker):
o = SomeClass()
with mocker.spy(SomeClass, 'method') as spy: # AttributeError: __enter__ here
o.facade()
assert spy.call_count == 1
o.facade()
Currently I can only find arguments
Issue Analytics
- State:
- Created 5 years ago
- Reactions:5
- Comments:5 (2 by maintainers)
Top Results From Across the Web
python - How can I mock and add a target when using a `patch ...
It seems like, when providing the new parameter, the patch context manager isn't a regular patch object and I can't spy on it...
Read more >contextlib — Utilities for with-statement contexts ... - Python Docs
Source code: Lib/contextlib.py This module provides utilities for common tasks involving the with statement. For more information see also Context Manager ...
Read more >03: Python context manager tutorial | 800+ Java & Big Data ...
A. A way to make a context manager is to make a class which follows the context management protocol, by implementing __enter__ and...
Read more >pytest-mock 1.6.1 - PyPI
The spy acts exactly like the original method in all cases, ... The purpose of this plugin is to make the use of...
Read more >Mocking context managers - Decoy - Mike Cousins
You can use Decoy to mock out your dependencies that should provide a context manager interface. Generator-based context managers#. Using the contextlib module, ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Mark90 indeed the issue was a bit confusing, thanks for bringing this to attention.
I’ve updated the title and mentioned that the return value has been implemented in 1.11. 👍
Didn’t notice that you already implemented this with 1.11. Please disregard my comment.
Awesome work by the way 😃