-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix broken event loop when a function-scoped test is in between …
…two wider-scoped tests. The event_loop fixture finalizers only close event loops that were not created by pytest-asyncio. This prevents the finalizers from accidentally closing a module-scoped loop, for example.
- Loading branch information
Showing
4 changed files
with
76 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from __future__ import annotations | ||
|
||
from textwrap import dedent | ||
|
||
from pytest import Pytester | ||
|
||
|
||
def test_function_scoped_loop_restores_previous_loop_scope(pytester: Pytester): | ||
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") | ||
pytester.makepyfile( | ||
dedent( | ||
"""\ | ||
import asyncio | ||
import pytest | ||
module_loop: asyncio.AbstractEventLoop | ||
@pytest.mark.asyncio(loop_scope="module") | ||
async def test_remember_loop(): | ||
global module_loop | ||
module_loop = asyncio.get_running_loop() | ||
@pytest.mark.asyncio(loop_scope="function") | ||
async def test_with_function_scoped_loop(): | ||
pass | ||
@pytest.mark.asyncio(loop_scope="module") | ||
async def test_runs_in_same_loop(): | ||
global module_loop | ||
assert asyncio.get_running_loop() is module_loop | ||
""" | ||
) | ||
) | ||
result = pytester.runpytest("--asyncio-mode=strict") | ||
result.assert_outcomes(passed=3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters