scripts: pytest: pass test name to handler.log

Pass pytest test name to handler.log header to make it easier to
analyze handler.log content.

Signed-off-by: Piotr Golyzniak <piotr.golyzniak@nordicsemi.no>
This commit is contained in:
Piotr Golyzniak 2023-08-04 16:50:00 +02:00 committed by Anas Nashif
parent a4a550888e
commit 967dfd97c3
5 changed files with 11 additions and 9 deletions

View file

@ -18,11 +18,11 @@ logger = logging.getLogger(__name__)
class BinaryAdapterBase(DeviceAdapter, abc.ABC):
def __init__(self, device_config: DeviceConfig) -> None:
def __init__(self, device_config: DeviceConfig, **kwargs) -> None:
"""
:param twister_config: twister configuration
"""
super().__init__(device_config)
super().__init__(device_config, **kwargs)
self._process: subprocess.Popen | None = None
self.process_kwargs: dict = {
'stdout': subprocess.PIPE,

View file

@ -27,7 +27,7 @@ logger = logging.getLogger(__name__)
class DeviceAdapter(abc.ABC):
"""Class defines an interface for all devices."""
def __init__(self, device_config: DeviceConfig) -> None:
def __init__(self, device_config: DeviceConfig, test_name: str = '') -> None:
"""
:param device_config: device configuration
"""
@ -39,6 +39,7 @@ class DeviceAdapter(abc.ABC):
self._device_connected: threading.Event = threading.Event()
self.command: list[str] = []
self._west: str | None = None
self._test_name: str = test_name
self.handler_log_path: Path = Path(device_config.build_dir) / 'handler.log'
self._initialize_log_file(self.handler_log_path)
@ -184,7 +185,7 @@ class DeviceAdapter(abc.ABC):
def _initialize_log_file(self, log_file_path: Path) -> None:
with open(log_file_path, 'a+') as log_file:
log_file.write(f'\n==== Test started at {datetime.now()} ====\n')
log_file.write(f'\n==== Test {self._test_name} started at {datetime.now()} ====\n')
def _start_reader_thread(self) -> None:
self._reader_thread = threading.Thread(target=self._handle_device_output, daemon=True)

View file

@ -27,8 +27,8 @@ logger = logging.getLogger(__name__)
class HardwareAdapter(DeviceAdapter):
"""Adapter class for real device."""
def __init__(self, device_config: DeviceConfig) -> None:
super().__init__(device_config)
def __init__(self, device_config: DeviceConfig, **kwargs) -> None:
super().__init__(device_config, **kwargs)
self._flashing_timeout: float = self.base_timeout
self._serial_connection: serial.Serial | None = None
self._serial_pty_proc: subprocess.Popen | None = None

View file

@ -17,8 +17,8 @@ logger = logging.getLogger(__name__)
class QemuAdapter(BinaryAdapterBase):
def __init__(self, device_config: DeviceConfig) -> None:
super().__init__(device_config)
def __init__(self, device_config: DeviceConfig, **kwargs) -> None:
super().__init__(device_config, **kwargs)
qemu_fifo_file_path = Path(self.device_config.build_dir) / 'qemu-fifo'
self._fifo_connection: FifoHandler = FifoHandler(qemu_fifo_file_path, self.base_timeout)

View file

@ -17,13 +17,14 @@ logger = logging.getLogger(__name__)
@pytest.fixture(scope='function')
def dut(request: pytest.FixtureRequest) -> Generator[DeviceAdapter, None, None]:
"""Return device instance."""
test_name = request.node.name
twister_harness_config: TwisterHarnessConfig = request.config.twister_harness_config # type: ignore
device_config: DeviceConfig = twister_harness_config.devices[0]
device_type = device_config.type
device_class: Type[DeviceAdapter] = DeviceFactory.get_device(device_type)
device = device_class(device_config)
device = device_class(device_config, test_name=test_name)
try:
device.launch()