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

How to spy call of async/await function? #60

Closed
hyzhak opened this issue Aug 24, 2016 · 3 comments
Closed

How to spy call of async/await function? #60

hyzhak opened this issue Aug 24, 2016 · 3 comments

Comments

@hyzhak
Copy link

hyzhak commented Aug 24, 2016

I'm trying to do this:

async def async_func2():
    pass


async def async_func1():
    await async_func2()


@pytest.mark.asyncio
async def test_async_func2(mocker):
    mock_async_func2 = mocker.patch(__name__ + '.async_func2')
    mock_async_func2.return_value = 'something'
    res = await async_func1()
    mock_async_func2.assert_called_once_with()

But got:

_________________________________________ test_async_func2 __________________________________________

mocker = <pytest_mock.MockFixture object at 0x7febf7b4c1d0>

    @pytest.mark.asyncio
    async def test_async_func2(mocker):
        mock_async_func2 = mocker.patch(__name__ + '.async_func2')
        mock_async_func2.return_value = 'something'
>       res = await async_func1()

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    async def async_func1():
>       await async_func2()
E       TypeError: object str can't be used in 'await' expression

My goal is to mock async_func2 function and catch all calls of it. How can I archive it with this library?

@hyzhak
Copy link
Author

hyzhak commented Aug 24, 2016

solution was much simpler than I thought:

@pytest.mark.asyncio
async def test_async_func2(mocker):
    mock_async_func2 = mocker.patch(__name__ + '.async_func2')
    async def return_async_value(val):
        return val
    mock_async_func2.return_value = return_async_value('something')
    res = await async_func1()
    mock_async_func2.assert_called_once_with()
    assert res == 'something'

thanks! :)

@hyzhak hyzhak closed this as completed Aug 24, 2016
@leftvalue
Copy link

but I can only run the async_func1 once or I got an error

async def async_func2():
    pass


async def async_func1():
    await async_func2()


@pytest.mark.asyncio
async def test_async_func2(mocker):
    mock_async_func2 = mocker.patch(__name__ + '.async_func2')

    async def return_async_value(val):
        return val

    mock_async_func2.return_value = return_async_value('something')
    res = await mock_async_func2()
    res = await mock_async_func2()
    mock_async_func2.assert_called_once_with()
    assert res == 'something'

run pytest and got

mocker = <pytest_mock.plugin.MockFixture object at 0x103cd2ac8>

    @pytest.mark.asyncio
    async def test_async_func2(mocker):
        mock_async_func2 = mocker.patch(__name__ + '.async_func2')
    
        async def return_async_value(val):
            return val
    
        mock_async_func2.return_value = return_async_value('something')
        res = await mock_async_func2()
>       res = await mock_async_func2()
E       RuntimeError: cannot reuse already awaited coroutine

desktop_top_trending_test.py:101: RuntimeError

@leftvalue
Copy link

finally I found a solution

async def async_func2():
    pass


async def async_func1():
    await async_func2()


@pytest.mark.asyncio
async def test_async_func2(mocker):
    mock_async_func2 = mocker.patch(__name__ + '.async_func2')

    def return_async_value(val):
        f = asyncio.Future()
        f.set_result(val)
        return f

    mock_async_func2.return_value = return_async_value('something')
    res = await mock_async_func2()
    print(res)
    res = await mock_async_func2()
    print(res)
    # mock_async_func2.assert_called_once_with()
    assert res == 'something'

this idea come from discussion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants