From 39288d2e539a62b809c8b5628c43fb002b26141e Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 15 Mar 2025 08:30:32 +0100 Subject: [PATCH] test: Add fixture to prevent having disabled loggers --- tests/conftest.py | 8 ++++++++ tests/test_configuration.py | 1 + tests/test_log_setup.py | 8 +++++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index ca382f6ae..b7e766b3e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -549,6 +549,14 @@ def user_dir(mocker, tmp_path) -> Path: return user_dir +@pytest.fixture() +def keep_log_config_loggers(mocker): + # Mock the _handle_existing_loggers function to prevent it from disabling all loggers. + # This is necessary to keep all loggers active, and avoid random failures if + # this file is ran before the test_rest_client file. + mocker.patch("logging.config._handle_existing_loggers") + + @pytest.fixture(autouse=True) def patch_coingecko(mocker) -> None: """ diff --git a/tests/test_configuration.py b/tests/test_configuration.py index 7ee04e133..6c54ad350 100644 --- a/tests/test_configuration.py +++ b/tests/test_configuration.py @@ -614,6 +614,7 @@ def test_cli_verbose_with_params(default_conf, mocker, caplog) -> None: assert log_has("Verbosity set to 3", caplog) +@pytest.mark.usefixtures("keep_log_config_loggers") def test_set_logfile(default_conf, mocker, tmp_path): default_conf["ft_tests_force_logging"] = True patched_configuration_load_config_file(mocker, default_conf) diff --git a/tests/test_log_setup.py b/tests/test_log_setup.py index c8ccf225e..9c3b530bf 100644 --- a/tests/test_log_setup.py +++ b/tests/test_log_setup.py @@ -16,6 +16,7 @@ from freqtrade.loggers.set_log_levels import ( ) +@pytest.mark.usefixtures("keep_log_config_loggers") def test_set_loggers() -> None: # Reset Logging to Debug, otherwise this fails randomly as it's set globally logging.getLogger("requests").setLevel(logging.DEBUG) @@ -62,6 +63,7 @@ def test_set_loggers() -> None: @pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows") +@pytest.mark.usefixtures("keep_log_config_loggers") def test_set_loggers_syslog(): logger = logging.getLogger() orig_handlers = logger.handlers @@ -87,6 +89,7 @@ def test_set_loggers_syslog(): @pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows") +@pytest.mark.usefixtures("keep_log_config_loggers") def test_set_loggers_Filehandler(tmp_path): logger = logging.getLogger() orig_handlers = logger.handlers @@ -114,6 +117,7 @@ def test_set_loggers_Filehandler(tmp_path): @pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows") +@pytest.mark.usefixtures("keep_log_config_loggers") def test_set_loggers_Filehandler_without_permission(tmp_path): logger = logging.getLogger() orig_handlers = logger.handlers @@ -138,7 +142,8 @@ def test_set_loggers_Filehandler_without_permission(tmp_path): @pytest.mark.skip(reason="systemd is not installed on every system, so we're not testing this.") -def test_set_loggers_journald(mocker): +@pytest.mark.usefixtures("keep_log_config_loggers") +def test_set_loggers_journald(): logger = logging.getLogger() orig_handlers = logger.handlers logger.handlers = [] @@ -158,6 +163,7 @@ def test_set_loggers_journald(mocker): logger.handlers = orig_handlers +@pytest.mark.usefixtures("keep_log_config_loggers") def test_set_loggers_journald_importerror(import_fails): logger = logging.getLogger() orig_handlers = logger.handlers