Skip to content

Commit c4d884c

Browse files
gchwiernashif
authored andcommitted
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>
1 parent e41919a commit c4d884c

File tree

5 files changed

+21
-0
lines changed

5 files changed

+21
-0
lines changed

scripts/pylib/pytest-twister-harness/src/twister_harness/device/device_adapter.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ def launch(self) -> None:
6767

6868
if not self.command:
6969
self.generate_command()
70+
if self.device_config.extra_test_args:
71+
self.command.extend(self.device_config.extra_test_args.split())
7072

7173
if self.device_config.type != 'hardware':
7274
self._flash_and_run()

scripts/pylib/pytest-twister-harness/src/twister_harness/plugin.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ def pytest_addoption(parser: pytest.Parser):
126126
'--twister-fixture', action='append', dest='fixtures', metavar='FIXTURE', default=[],
127127
help='Twister fixture supported by this platform. May be given multiple times.'
128128
)
129+
twister_harness_group.addoption(
130+
'--extra-test-args',
131+
help='Additional args passed to the test binary'
132+
)
129133

130134

131135
def pytest_configure(config: pytest.Config):

scripts/pylib/pytest-twister-harness/src/twister_harness/twister_harness_config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class DeviceConfig:
3636
post_flash_script: Path | None = None
3737
fixtures: list[str] = None
3838
app_build_dir: Path | None = None
39+
extra_test_args: str = ''
3940

4041
def __post_init__(self):
4142
domains = self.build_dir / 'domains.yaml'
@@ -81,6 +82,7 @@ def create(cls, config: pytest.Config) -> TwisterHarnessConfig:
8182
post_script=_cast_to_path(config.option.post_script),
8283
post_flash_script=_cast_to_path(config.option.post_flash_script),
8384
fixtures=config.option.fixtures,
85+
extra_test_args=config.option.extra_test_args
8486
)
8587

8688
devices.append(device_from_cli)

scripts/pylib/twister/twisterlib/harness.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,9 @@ def generate_command(self):
426426
for fixture in handler.options.fixture:
427427
command.append(f'--twister-fixture={fixture}')
428428

429+
if handler.options.extra_test_args and handler.type_str == 'native':
430+
command.append(f'--extra-test-args={shlex.join(handler.options.extra_test_args)}')
431+
429432
command.extend(pytest_args_yaml)
430433

431434
if handler.options.pytest_args:

scripts/tests/twister/pytest_integration/test_harness_pytest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def testinstance() -> TestInstance:
2828
testinstance.handler.options.verbose = 1
2929
testinstance.handler.options.fixture = ['fixture1:option1', 'fixture2']
3030
testinstance.handler.options.pytest_args = None
31+
testinstance.handler.options.extra_test_args = []
3132
testinstance.handler.type_str = 'native'
3233
return testinstance
3334

@@ -72,6 +73,15 @@ def test_pytest_command_extra_args(testinstance: TestInstance):
7273
assert c in command
7374

7475

76+
def test_pytest_command_extra_test_args(testinstance: TestInstance):
77+
pytest_harness = Pytest()
78+
extra_test_args = ['-stop_at=3', '-no-rt']
79+
testinstance.handler.options.extra_test_args = extra_test_args
80+
pytest_harness.configure(testinstance)
81+
command = pytest_harness.generate_command()
82+
assert f'--extra-test-args={extra_test_args[0]} {extra_test_args[1]}' in command
83+
84+
7585
def test_pytest_command_extra_args_in_options(testinstance: TestInstance):
7686
pytest_harness = Pytest()
7787
pytest_args_from_yaml = '--extra-option'

0 commit comments

Comments
 (0)