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

async_stub doesn't need to be awaited #374

Open
AstraLuma opened this issue Jul 22, 2023 · 1 comment
Open

async_stub doesn't need to be awaited #374

AstraLuma opened this issue Jul 22, 2023 · 1 comment
Labels

Comments

@AstraLuma
Copy link

(event_loop from pytest-asyncio)

async def test_demo(event_loop, mocker):
    callee = mocker.async_stub('callee')

    event_loop.call_soon_threadsafe(callee)  # Wrong!

    await asyncio.sleep(0)

    assert callee.called == 1

Expected behavior: callee.called should be zero

Actual behavior: callee.called is one, and a warning is produced:

tests/test_basics.py::test_demo
  /usr/lib64/python3.11/asyncio/events.py:80: RuntimeWarning: coroutine 'AsyncMockMixin._execute_mock_call' was never awaited
    self._context.run(self._callback, *self._args)
  Enable tracemalloc to get traceback where the object was allocated.
  See https://docs.pytest.org/en/stable/how-to/capture-warnings.html#resource-warnings for more info.

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html

I'm calling this behavior incorrect (or at least unhelpful) because if I were to use a real coroutine instead of async_stub(), the body never would have executed.

@nicoddemus
Copy link
Member

Hi @AstraLuma,

Can you please replace the mocker fixture to use unittest.mock instead? That is helpful to see if the problem lies with pytest-mock or with unittest.mock itself.

async def test_demo(event_loop):
    callee = unittest.mock.AsyncMock('callee')

    event_loop.call_soon_threadsafe(callee)  # Wrong!

    await asyncio.sleep(0)

    assert callee.called == 1

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

No branches or pull requests

2 participants