From a9980e2a90d292644627044e3488724a802254b7 Mon Sep 17 00:00:00 2001 From: Dor Blayzer <59066376+Dor-bl@users.noreply.github.com> Date: Sun, 8 Nov 2026 22:48:40 +0100 Subject: [PATCH] test: add unit tests for setup_logger Cover the default and custom-level configurations of setup_logger. An autouse fixture restores the shared module logger after each test, since setup_logger adds a handler on every call. --- test/unit/common/logger_test.py | 61 +++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 test/unit/common/logger_test.py diff --git a/test/unit/common/logger_test.py b/test/unit/common/logger_test.py new file mode 100644 index 00000000..6c53af12 --- /dev/null +++ b/test/unit/common/logger_test.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import logging +import sys +from collections.abc import Iterator + +import pytest + +from appium.common.logger import logger, setup_logger + + +@pytest.fixture(autouse=True) +def restore_logger() -> Iterator[None]: + # setup_logger mutates the shared module logger, so put it back after each test. + handlers = list(logger.handlers) + level = logger.level + propagate = logger.propagate + yield + logger.handlers[:] = handlers + logger.setLevel(level) + logger.propagate = propagate + + +def test_setup_logger_default() -> None: + handlers_before = list(logger.handlers) + + setup_logger() + + assert logger.propagate is False + assert logger.level == logging.NOTSET + assert len(logger.handlers) == len(handlers_before) + 1 + + new_handler = logger.handlers[-1] + assert isinstance(new_handler, logging.StreamHandler) + assert new_handler.stream == sys.stderr + + +def test_setup_logger_custom_level() -> None: + handlers_before = list(logger.handlers) + + setup_logger(level=logging.DEBUG) + + assert logger.propagate is False + assert logger.level == logging.DEBUG + assert len(logger.handlers) == len(handlers_before) + 1 + + new_handler = logger.handlers[-1] + assert isinstance(new_handler, logging.StreamHandler) + assert new_handler.stream == sys.stderr