question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make spy a context manager

See original GitHub issue

Capture 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:open
  • Created 5 years ago
  • Reactions:5
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
nicoddemuscommented, Nov 18, 2019

@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. 👍

1reaction
Mark90commented, Nov 13, 2019

Didn’t notice that you already implemented this with 1.11. Please disregard my comment.

Awesome work by the way 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found