Skip to content

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

Open
@twisteroidambassador

Description

@twisteroidambassador

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions