twister: Pass extra test args to pytest

Pass additional args to test binary. Twister
passes it only for native_sim.
Fixes #82463

Signed-off-by: Grzegorz Chwierut <grzegorz.chwierut@nordicsemi.no>
This commit is contained in:
Grzegorz Chwierut 2024-12-05 17:43:42 +01:00 committed by Anas Nashif
commit c4d884cc65
5 changed files with 21 additions and 0 deletions

View file

@ -67,6 +67,8 @@ class DeviceAdapter(abc.ABC):
if not self.command:
self.generate_command()
if self.device_config.extra_test_args:
self.command.extend(self.device_config.extra_test_args.split())
if self.device_config.type != 'hardware':
self._flash_and_run()

View file

@ -126,6 +126,10 @@ def pytest_addoption(parser: pytest.Parser):
'--twister-fixture', action='append', dest='fixtures', metavar='FIXTURE', default=[],
help='Twister fixture supported by this platform. May be given multiple times.'
)
twister_harness_group.addoption(
'--extra-test-args',
help='Additional args passed to the test binary'
)
def pytest_configure(config: pytest.Config):

View file

@ -36,6 +36,7 @@ class DeviceConfig:
post_flash_script: Path | None = None
fixtures: list[str] = None
app_build_dir: Path | None = None
extra_test_args: str = ''
def __post_init__(self):
domains = self.build_dir / 'domains.yaml'
@ -81,6 +82,7 @@ class TwisterHarnessConfig:
post_script=_cast_to_path(config.option.post_script),
post_flash_script=_cast_to_path(config.option.post_flash_script),
fixtures=config.option.fixtures,
extra_test_args=config.option.extra_test_args
)
devices.append(device_from_cli)

View file

@ -426,6 +426,9 @@ class Pytest(Harness):
for fixture in handler.options.fixture:
command.append(f'--twister-fixture={fixture}')
if handler.options.extra_test_args and handler.type_str == 'native':
command.append(f'--extra-test-args={shlex.join(handler.options.extra_test_args)}')
command.extend(pytest_args_yaml)
if handler.options.pytest_args:

View file

@ -28,6 +28,7 @@ def testinstance() -> TestInstance:
testinstance.handler.options.verbose = 1
testinstance.handler.options.fixture = ['fixture1:option1', 'fixture2']
testinstance.handler.options.pytest_args = None
testinstance.handler.options.extra_test_args = []
testinstance.handler.type_str = 'native'
return testinstance
@ -72,6 +73,15 @@ def test_pytest_command_extra_args(testinstance: TestInstance):
assert c in command
def test_pytest_command_extra_test_args(testinstance: TestInstance):
pytest_harness = Pytest()
extra_test_args = ['-stop_at=3', '-no-rt']
testinstance.handler.options.extra_test_args = extra_test_args
pytest_harness.configure(testinstance)
command = pytest_harness.generate_command()
assert f'--extra-test-args={extra_test_args[0]} {extra_test_args[1]}' in command
def test_pytest_command_extra_args_in_options(testinstance: TestInstance):
pytest_harness = Pytest()
pytest_args_from_yaml = '--extra-option'