Add autouse fixture to patch asyncio.sleep (#1131)

This commit is contained in:
Steven B. 2024-09-27 10:57:23 +01:00 committed by GitHub
parent 038b6993ca
commit db686e191a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 3 deletions

View File

@ -1,5 +1,6 @@
from __future__ import annotations
import asyncio
import warnings
from unittest.mock import MagicMock, patch
@ -96,6 +97,18 @@ def pytest_collection_modifyitems(config, items):
item.add_marker(requires_dummy)
@pytest.fixture(autouse=True, scope="session")
def asyncio_sleep_fixture(): # noqa: PT004
"""Patch sleep to prevent tests actually waiting."""
orig_asyncio_sleep = asyncio.sleep
async def _asyncio_sleep(*_, **__):
await orig_asyncio_sleep(0)
with patch("asyncio.sleep", side_effect=_asyncio_sleep):
yield
# allow mocks to be awaited
# https://stackoverflow.com/questions/51394411/python-object-magicmock-cant-be-used-in-await-expression/51399767#51399767

View File

@ -66,8 +66,6 @@ async def test_protocol_retries_via_client_session(
):
host = "127.0.0.1"
conn = mocker.patch.object(aiohttp.ClientSession, "post", side_effect=error)
mocker.patch.object(protocol_class, "BACKOFF_SECONDS_AFTER_TIMEOUT", 0)
mocker.patch("asyncio.sleep")
config = DeviceConfig(host)
with pytest.raises(KasaException):
@ -140,7 +138,6 @@ async def test_protocol_retry_recoverable_error(
"post",
side_effect=aiohttp.ClientOSError("foo"),
)
mocker.patch("asyncio.sleep")
config = DeviceConfig(host)
with pytest.raises(KasaException):