Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

twister: Pass extra test args to pytest #82620

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def launch(self) -> None:

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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -81,6 +82,7 @@ def create(cls, config: pytest.Config) -> 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)
Expand Down
3 changes: 3 additions & 0 deletions scripts/pylib/twister/twisterlib/harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,9 @@ def generate_command(self):
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:
Expand Down
10 changes: 10 additions & 0 deletions scripts/tests/twister/pytest_integration/test_harness_pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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'
Expand Down
Loading