pytest-dev / pytest-dev/pytest-mock

async_stub doesn't need to be awaited

Open
#374 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
Python
Stars
2k
Forks
173
Avg merge
1d 10h
Merged PRs (30d)
6

Description

(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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the async_stub implementation and reproduce the example from tests/test_basics.py with pytest-asyncio's event_loop. Compare the async stub with a real coroutine and verify the expected behavior: scheduling the stub without awaiting it should not increment callee.called or produce an unawaited-coroutine warning.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.