From 3bf95247d43baba59f3a41d08c581605b02c795c Mon Sep 17 00:00:00 2001 From: ZeliardM <140266236+ZeliardM@users.noreply.github.com> Date: Wed, 18 Nov 2026 08:14:51 -0500 Subject: [PATCH] Keep tests from changing logger levels and fixture data for later tests (#1844) Some tests leave the `kasa` logger at a different level or rename devices in shared fixture data. Later log assertions and device dump comparisons then pass or fail depending on the test order. This PR restores logger levels through `caplog` and renames devices on copies, so the affected tests pass independently of their order. In details: - Use `caplog.set_level` for tests that change the `kasa` logger, and remove an unused logger override. - Add an autouse fixture that restores any changed logger levels and fails the test with a pointer to `caplog.set_level`. - Rename README devices on deep copies of the fixture data. - Cover KLAP handshake debug logging explicitly, instead of relying on an earlier test leaving DEBUG enabled. Co-authored-by: Claude Opus 5.5 --- tests/conftest.py | 34 ++++++++++++++++++++++++++ tests/protocols/test_iotprotocol.py | 5 +--- tests/test_device_factory.py | 2 +- tests/test_readme_examples.py | 6 +++++ tests/transports/test_klaptransport.py | 30 ++++++++++++++++++++++- 5 files changed, 71 insertions(+), 6 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 7a51b49e..8ed69176 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,6 +2,7 @@ from __future__ import annotations import asyncio import functools +import logging import os import sys import warnings @@ -69,6 +70,39 @@ async def _close_transport_and_http_sessions(monkeypatch): await session.close() +def _logger_levels() -> dict[str, int]: + levels = {"root": logging.getLogger().level} + for name, logger in list(logging.Logger.manager.loggerDict.items()): + if isinstance(logger, logging.Logger): + levels[name] = logger.level + return levels + + +@pytest.fixture(autouse=True) +def _logger_levels_unchanged(request: pytest.FixtureRequest): + """Fail a test that leaves a logger's level changed. + + A level set directly stays for the later tests on the same worker, so + they pass or fail depending on the order. Use caplog.set_level, which + pytest restores after the test. + """ + before = _logger_levels() + yield + changed = { + name: level + for name, level in _logger_levels().items() + if level != before.get(name, logging.NOTSET) + } + for name in changed: + logging.getLogger(name).setLevel(before.get(name, logging.NOTSET)) + if changed: + levels = {name: logging.getLevelName(level) for name, level in changed.items()} + pytest.fail( + f"{request.node.nodeid} left logger levels changed: {levels}. " + "Use caplog.set_level, which restores them." + ) + + def load_fixture(foldername: str, filename: str) -> str: """Load a fixture.""" path = Path(Path(__file__).parent / "fixtures" / foldername / filename) diff --git a/tests/protocols/test_iotprotocol.py b/tests/protocols/test_iotprotocol.py index 8842b551..26a5bdf1 100644 --- a/tests/protocols/test_iotprotocol.py +++ b/tests/protocols/test_iotprotocol.py @@ -557,7 +557,7 @@ async def test_protocol_logging( encryption_class, ) -> None: caplog.set_level(log_level) - logging.getLogger("kasa").setLevel(log_level) + caplog.set_level(log_level, logger="kasa") encrypted = encryption_class.encrypt('{"great":"success"}')[ transport_class.BLOCK_SIZE : ] @@ -980,9 +980,6 @@ async def test_iot_queries_redaction( assert device_id not in caplog.text caplog.set_level(logging.DEBUG, logger="kasa") - # The fake iot protocol also logs so disable it - test_logger = logging.getLogger("kasa.tests.fakeprotocol_iot") - test_logger.setLevel(logging.INFO) # Debug no redaction caplog.clear() diff --git a/tests/test_device_factory.py b/tests/test_device_factory.py index 137000d3..1f217678 100644 --- a/tests/test_device_factory.py +++ b/tests/test_device_factory.py @@ -141,7 +141,7 @@ async def test_connect_logs_connect_time( config = DeviceConfig( host=host, credentials=Credentials("foor", "bar"), connection_type=ctype ) - logging.getLogger("kasa").setLevel(logging.DEBUG) + caplog.set_level(logging.DEBUG, logger="kasa") await connect( config=config, ) diff --git a/tests/test_readme_examples.py b/tests/test_readme_examples.py index f77c302e..05a5f5fd 100644 --- a/tests/test_readme_examples.py +++ b/tests/test_readme_examples.py @@ -1,4 +1,5 @@ import asyncio +import copy import pytest import xdoctest @@ -181,6 +182,11 @@ async def readmes_mock(mocker: MockerFixture): "127.0.0.5": get_fixture_info("HS220(US)_1.0_1.5.7.json", "IOT"), # Dimmer "127.0.0.6": get_fixture_info("H200(US)_1.0_1.3.6.json", "SMARTCAM"), # Hub } + # Rename the devices on copies, not on the fixtures other tests share + fixture_infos = { + ip: info._replace(data=copy.deepcopy(info.data)) + for ip, info in fixture_infos.items() + } fixture_infos["127.0.0.1"].data["system"]["get_sysinfo"]["alias"] = ( "Bedroom Power Strip" ) diff --git a/tests/transports/test_klaptransport.py b/tests/transports/test_klaptransport.py index 8dc886fa..462a7d8d 100644 --- a/tests/transports/test_klaptransport.py +++ b/tests/transports/test_klaptransport.py @@ -222,7 +222,7 @@ async def test_protocol_logging( mocker: MockerFixture, caplog: pytest.LogCaptureFixture, log_level: int ) -> None: caplog.set_level(log_level) - logging.getLogger("kasa").setLevel(log_level) + caplog.set_level(log_level, logger="kasa") def _return_encrypted(*_, **__): # Devices sign the response for the sequence number of the request @@ -583,6 +583,34 @@ async def test_handshake( await protocol.close() +@pytest.mark.xdist_group(name="caplog") +async def test_handshake_debug_logging( + mocker: MockerFixture, caplog: pytest.LogCaptureFixture +) -> None: + """Both handshake steps are logged when debug logging is enabled.""" + caplog.set_level(logging.DEBUG, logger="kasa") + server_seed = secrets.token_bytes(16) + credentials = Credentials("foo", "bar") + device_auth_hash = KlapTransport.generate_auth_hash(credentials) + + async def _return_handshake_response(url: URL, params=None, data=None, *_, **__): + if url == URL("http://127.0.0.1:80/app/handshake1"): + seed_auth_hash = _sha256(data + device_auth_hash) + return _mock_response(200, server_seed + seed_auth_hash) + return _mock_response(200, b"") + + mocker.patch.object( + aiohttp.ClientSession, "post", side_effect=_return_handshake_response + ) + transport = KlapTransport(config=DeviceConfig("127.0.0.1", credentials=credentials)) + + await transport.perform_handshake() + assert "Handshake1 posted" in caplog.text + assert "Handshake1 success" in caplog.text + assert "Handshake2 posted" in caplog.text + await transport.close() + + async def test_query(mocker: MockerFixture) -> None: client_seed = None last_seq = None