From bdd2c6f5548a5a8cdabf32a9d866b7fb6e95d660 Mon Sep 17 00:00:00 2001 From: Paszkiet Kamil Date: Thu, 18 Jan 2024 11:19:47 +0100 Subject: [PATCH] scripts: twister_blackbox: Added the ability to clear log from the code clear_log is used as a fixture by pytest. We need to be able to call it from the code level. Example of use in the file test_harwaremap.py line 157. Signed-off-by: Paszkiet Kamil --- scripts/tests/twister_blackbox/conftest.py | 5 +++++ scripts/tests/twister_blackbox/test_hardwaremap.py | 12 ++---------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/scripts/tests/twister_blackbox/conftest.py b/scripts/tests/twister_blackbox/conftest.py index 11429319b96c031..1271af06694f1db 100644 --- a/scripts/tests/twister_blackbox/conftest.py +++ b/scripts/tests/twister_blackbox/conftest.py @@ -37,6 +37,11 @@ def zephyr_test_directory(): @pytest.fixture def clear_log(): + # clear_log is used by pytest fixture + # However, clear_log_in_test is prepared to be used directly in the code, wherever required + clear_log_in_test() + +def clear_log_in_test(): # Required to fix the pytest logging error # See: https://github.com/pytest-dev/pytest/issues/5502 loggers = [logging.getLogger()] \ diff --git a/scripts/tests/twister_blackbox/test_hardwaremap.py b/scripts/tests/twister_blackbox/test_hardwaremap.py index c1efd5be495310a..c1f6ff1eaf81053 100644 --- a/scripts/tests/twister_blackbox/test_hardwaremap.py +++ b/scripts/tests/twister_blackbox/test_hardwaremap.py @@ -5,14 +5,13 @@ """ Blackbox tests for twister's command line functions """ -import logging import importlib import mock import os import pytest import sys -from conftest import ZEPHYR_BASE, testsuite_filename_mock +from conftest import ZEPHYR_BASE, testsuite_filename_mock, clear_log_in_test from twisterlib.testplan import TestPlan sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts/pylib/twister/twisterlib")) @@ -155,14 +154,7 @@ def mocked_comports(): os.remove(path) assert str(sys_exit.value) == '0' - loggers = [logging.getLogger()] + \ - list(logging.Logger.manager.loggerDict.values()) + \ - [logging.getLogger(name) for \ - name in logging.root.manager.loggerDict] - for logger in loggers: - handlers = getattr(logger, 'handlers', []) - for handler in handlers: - logger.removeHandler(handler) + clear_log_in_test() @pytest.mark.parametrize( ('manufacturer', 'product', 'serial', 'runner'),