Browse Source
Introduce a new harness based on pytest that does basic shell command handling. The harness is enabeld using: harness: shell and expects a file with parameters in the form: test_shell_harness: - command: "kernel version" expected: "Zephyr version .*" - ... Multiple commands and their expected output can be tested. Signed-off-by: Anas Nashif <anas.nashif@intel.com>pull/85469/head
6 changed files with 72 additions and 2 deletions
@ -0,0 +1,7 @@
@@ -0,0 +1,7 @@
|
||||
# Copyright (c) 2025 Intel Corporation |
||||
# |
||||
# SPDX-License-Identifier: Apache-2.0 |
||||
|
||||
|
||||
def pytest_addoption(parser): |
||||
parser.addoption('--testdata') |
@ -0,0 +1,36 @@
@@ -0,0 +1,36 @@
|
||||
# Copyright (c) 2025 Intel Corporation |
||||
# |
||||
# SPDX-License-Identifier: Apache-2.0 |
||||
import logging |
||||
import re |
||||
|
||||
import pytest |
||||
import yaml |
||||
from twister_harness import Shell |
||||
|
||||
logger = logging.getLogger(__name__) |
||||
|
||||
|
||||
@pytest.fixture |
||||
def testdata_path(request): |
||||
return request.config.getoption("--testdata") |
||||
|
||||
|
||||
def get_next_commands(testdata_path): |
||||
with open(testdata_path) as yaml_file: |
||||
data = yaml.safe_load(yaml_file) |
||||
for entry in data['test_shell_harness']: |
||||
yield entry['command'], entry['expected'] |
||||
|
||||
|
||||
def test_shell_harness(shell: Shell, testdata_path): |
||||
for command, expected in get_next_commands(testdata_path): |
||||
logger.info('send command: %s', command) |
||||
lines = shell.exec_command(command) |
||||
match = False |
||||
for line in lines: |
||||
if re.match(expected, line): |
||||
match = True |
||||
break |
||||
assert match, 'expected response not found' |
||||
logger.info('response is valid') |
Loading…
Reference in new issue