Skip to content

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

@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

Activity

added this to the v1.0 milestone on Apr 19, 2024
seifertm

seifertm commented on May 26, 2025

@seifertm
Contributor

The event_loop fixture has been deprecated in v0.23 and removed in v1.0.0.
Therefore, I will close this issue as obsolete.

twisteroidambassador

twisteroidambassador commented on May 26, 2025

@twisteroidambassador
Author

The event_loop fixture has been deprecated in v0.23 and removed in v1.0.0. Therefore, I will close this issue as obsolete.

This bug manifests when not using the event_loop fixture, so it being removed only exacerbates the issue.

From what I can see, the problem here is that the scope of the event loop intersects with mocker: after the mocker patches socket, the existing event loop is torn down and recreated while socket is still being mocked, causing the creation of event loop to fail. The old event_loop fixture somehow "nests" with the mocker fixture correctly, while not using the event_loop fixture made the event loop's life cycle conflict with the mocker fixture.

The workaround I used is to not use the mocker fixture either, and switch to unittest.mock and with mock.patch, which made the lifecycle of the mock fit entirely into the test function.

To properly fix this in pytest-asyncio, it's probably necessary to ensure the lifecycle of the event loop fully encompass everything inside the test function including fixtures.

modified the milestones: v1.0, v1.1 on May 26, 2025
seifertm

seifertm commented on May 26, 2025

@seifertm
Contributor

@twisteroidambassador Thanks for the clarification!

To properly fix this in pytest-asyncio, it's probably necessary to ensure the lifecycle of the event loop fully encompass everything inside the test function including fixtures.

We'd like to do that, as it would solve other issues like #112, but it's not part of the v1.0 release. I'm reopening the issue :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @seifertm@twisteroidambassador

        Issue actions

          Weird interaction with pytest-mock: not requesting event_loop fixture leads to errors · Issue #818 · pytest-dev/pytest-asyncio