From 33a6afc5cbc838fe438c95d4d016db5830ddd3b2 Mon Sep 17 00:00:00 2001 From: "oleg.korshunov" Date: Thu, 9 Jan 2025 23:27:54 +0300 Subject: [PATCH] Returned the old description, corrected it to asyncio.Runner --- docs/en/docs/advanced/async-tests.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/docs/en/docs/advanced/async-tests.md b/docs/en/docs/advanced/async-tests.md index a44449f18..df1b5d2d9 100644 --- a/docs/en/docs/advanced/async-tests.md +++ b/docs/en/docs/advanced/async-tests.md @@ -77,13 +77,16 @@ As the testing function is now asynchronous, you can now also call (and await) o If you encounter a `RuntimeError: Task attached to a different loop` when integrating asynchronous function calls in your tests, you can override the default pytest event loop using the following fixture: ```python +import asyncio +import pytest +from collections.abc import Generator + @pytest.fixture(scope="session") -def event_loop() -> Generator[AbstractEventLoop, None, None]: - """Overrides pytest default function scoped event loop""" - policy = asyncio.get_event_loop_policy() - loop = policy.new_event_loop() - yield loop - loop.close() +def event_loop() -> Generator[asyncio.AbstractEventLoop, None, None]: + """Overrides pytest default function scoped event loop using asyncio.Runner""" + with asyncio.Runner() as runner: + yield runner.get_loop() + ``` ///