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 <kamilx.paszkiet@intel.com>
This commit is contained in:
Paszkiet Kamil 2024-01-18 11:19:47 +01:00 committed by Anas Nashif
commit 114548ef60
2 changed files with 7 additions and 10 deletions

View file

@ -37,6 +37,11 @@ def zephyr_test_directory():
@pytest.fixture @pytest.fixture
def clear_log(): 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 # Required to fix the pytest logging error
# See: https://github.com/pytest-dev/pytest/issues/5502 # See: https://github.com/pytest-dev/pytest/issues/5502
loggers = [logging.getLogger()] \ loggers = [logging.getLogger()] \

View file

@ -5,14 +5,13 @@
""" """
Blackbox tests for twister's command line functions Blackbox tests for twister's command line functions
""" """
import logging
import importlib import importlib
import mock import mock
import os import os
import pytest import pytest
import sys 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 from twisterlib.testplan import TestPlan
sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts/pylib/twister/twisterlib")) sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts/pylib/twister/twisterlib"))
@ -155,14 +154,7 @@ class TestHardwaremap:
os.remove(path) os.remove(path)
assert str(sys_exit.value) == '0' assert str(sys_exit.value) == '0'
loggers = [logging.getLogger()] + \ clear_log_in_test()
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)
@pytest.mark.parametrize( @pytest.mark.parametrize(
('manufacturer', 'product', 'serial', 'runner'), ('manufacturer', 'product', 'serial', 'runner'),