pytest-dev / pytest-dev/pytest-asyncio

Weird interaction with pytest-mock: not requesting event_loop fixture leads to errors

Open
#818 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
1.7k
Forks
207
Avg merge
5h 35m
Merged PRs (30d)
9

Description

Consider the two test cases below:

@pytest.mark.asyncio
async def test_mocksocket_mocker(mocker):
    mocker.patch('socket.socket', wraps=MockSocket)
    pass


@pytest.mark.asyncio
async def test_mocksocket_loop_mocker(event_loop, mocker):
    mocker.patch('socket.socket', wraps=MockSocket)
    pass

The top one reports ERROR at teardown of test_mocksocket_mocker. The bottom one does not report any error. The only difference between them is that the bottom one requests the event_loop fixture.

The error message is quite long. It seems to be caused by MockSocket still being active when _provide_clean_event_loop() is called, and the new event loop failing at initialization.

Also, if the test case calls unittest.mock.patch instead of mocker.patch, then there is no error.

Attached is the test file, full console output, and package versions. This is running on Windows 10.

pytest-asyncio-mock.zip

Contributor guide

No contributing guide indexed for this repository

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 by running the two pytest-asyncio examples from the attached test file on the reported Windows setup, comparing mocker.patch with unittest.mock.patch. Read the teardown path around _provide_clean_event_loop() and the supplied console output; done means the first test completes without a teardown error while preserving the existing event_loop behavior.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.