From a83e89c6f2bd9122bedfb13664bf06537f2a7d5f Mon Sep 17 00:00:00 2001 From: Grzegorz Chwierut Date: Thu, 5 Dec 2024 17:43:42 +0100 Subject: [PATCH] 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 --- .../src/twister_harness/device/device_adapter.py | 2 ++ .../src/twister_harness/plugin.py | 4 ++++ .../src/twister_harness/twister_harness_config.py | 2 ++ scripts/pylib/twister/twisterlib/harness.py | 3 +++ .../twister/pytest_integration/test_harness_pytest.py | 10 ++++++++++ 5 files changed, 21 insertions(+) diff --git a/scripts/pylib/pytest-twister-harness/src/twister_harness/device/device_adapter.py b/scripts/pylib/pytest-twister-harness/src/twister_harness/device/device_adapter.py index 3f73045f899fee..fd97972c74af51 100644 --- a/scripts/pylib/pytest-twister-harness/src/twister_harness/device/device_adapter.py +++ b/scripts/pylib/pytest-twister-harness/src/twister_harness/device/device_adapter.py @@ -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() diff --git a/scripts/pylib/pytest-twister-harness/src/twister_harness/plugin.py b/scripts/pylib/pytest-twister-harness/src/twister_harness/plugin.py index ef8a1fb6dc1971..ec212db98bde6d 100644 --- a/scripts/pylib/pytest-twister-harness/src/twister_harness/plugin.py +++ b/scripts/pylib/pytest-twister-harness/src/twister_harness/plugin.py @@ -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): diff --git a/scripts/pylib/pytest-twister-harness/src/twister_harness/twister_harness_config.py b/scripts/pylib/pytest-twister-harness/src/twister_harness/twister_harness_config.py index c635f62973a8b5..65aa87d8635bd3 100644 --- a/scripts/pylib/pytest-twister-harness/src/twister_harness/twister_harness_config.py +++ b/scripts/pylib/pytest-twister-harness/src/twister_harness/twister_harness_config.py @@ -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 @@ 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) diff --git a/scripts/pylib/twister/twisterlib/harness.py b/scripts/pylib/twister/twisterlib/harness.py index f7d0e51fae4fe9..f772c3410af29c 100644 --- a/scripts/pylib/twister/twisterlib/harness.py +++ b/scripts/pylib/twister/twisterlib/harness.py @@ -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: diff --git a/scripts/tests/twister/pytest_integration/test_harness_pytest.py b/scripts/tests/twister/pytest_integration/test_harness_pytest.py index 5e3d92535f2385..6ffb928c63a42d 100644 --- a/scripts/tests/twister/pytest_integration/test_harness_pytest.py +++ b/scripts/tests/twister/pytest_integration/test_harness_pytest.py @@ -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'