Skip to content

Commit d52796f

Browse files
henrikbrixandersenanangl
authored andcommitted
[nrf fromtree] twister: harness: pytest: list available fixtures in device config
Pass the list of supported twister fixtures for a given platform to pytest via DeviceConfig. This allows for the pytest suites to use knowledge of the fixtures for test suite configuration. Signed-off-by: Henrik Brix Andersen <[email protected]> (cherry picked from commit a7d55cf)
1 parent 3198700 commit d52796f

File tree

5 files changed

+20
-1
lines changed

5 files changed

+20
-1
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ def pytest_addoption(parser: pytest.Parser):
117117
choices=('function', 'class', 'module', 'package', 'session'),
118118
help='The scope for which `dut` and `shell` fixtures are shared.'
119119
)
120+
twister_harness_group.addoption(
121+
'--twister-fixture', action='append', dest='fixtures', metavar='FIXTURE', default=[],
122+
help='Twister fixture supported by this platform. May be given multiple times.'
123+
)
120124

121125

122126
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
@@ -33,6 +33,7 @@ class DeviceConfig:
3333
pre_script: Path | None = None
3434
post_script: Path | None = None
3535
post_flash_script: Path | None = None
36+
fixtures: list[str] = None
3637
app_build_dir: Path | None = None
3738

3839
def __post_init__(self):
@@ -77,6 +78,7 @@ def create(cls, config: pytest.Config) -> TwisterHarnessConfig:
7778
pre_script=_cast_to_path(config.option.pre_script),
7879
post_script=_cast_to_path(config.option.post_script),
7980
post_flash_script=_cast_to_path(config.option.post_flash_script),
81+
fixtures=config.option.fixtures,
8082
)
8183

8284
devices.append(device_from_cli)

scripts/pylib/twister/twisterlib/harness.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,10 @@ def generate_command(self):
354354
else:
355355
raise PytestHarnessException(f'Support for handler {handler.type_str} not implemented yet')
356356

357+
if handler.type_str != 'device':
358+
for fixture in handler.options.fixture:
359+
command.append(f'--twister-fixture={fixture}')
360+
357361
if handler.options.pytest_args:
358362
command.extend(handler.options.pytest_args)
359363
if pytest_args_yaml:
@@ -411,6 +415,9 @@ def _generate_parameters_for_hardware(self, handler: Handler):
411415
if hardware.flash_before:
412416
command.append(f'--flash-before={hardware.flash_before}')
413417

418+
for fixture in hardware.fixtures:
419+
command.append(f'--twister-fixture={fixture}')
420+
414421
return command
415422

416423
def run_command(self, cmd, timeout):

scripts/tests/twister/pytest_integration/test_harness_pytest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def testinstance() -> TestInstance:
2626
testinstance.handler = mock.Mock()
2727
testinstance.handler.options = mock.Mock()
2828
testinstance.handler.options.verbose = 1
29+
testinstance.handler.options.fixture = ['fixture1:option1', 'fixture2']
2930
testinstance.handler.options.pytest_args = None
3031
testinstance.handler.type_str = 'native'
3132
return testinstance
@@ -42,7 +43,9 @@ def test_pytest_command(testinstance: TestInstance, device_type):
4243
'samples/hello/pytest',
4344
f'--build-dir={testinstance.build_dir}',
4445
f'--junit-xml={testinstance.build_dir}/report.xml',
45-
f'--device-type={device_type}'
46+
f'--device-type={device_type}',
47+
'--twister-fixture=fixture1:option1',
48+
'--twister-fixture=fixture2'
4649
]
4750

4851
command = pytest_harness.generate_command()

scripts/tests/twister/test_harness.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ def test_pytest__generate_parameters_for_hardware(tmp_path, pty_value, hardware_
345345
hardware.baud = 115200
346346
hardware.runner = "runner"
347347
hardware.runner_params = ["--runner-param1", "runner-param2"]
348+
hardware.fixtures = ['fixture1:option1', 'fixture2']
348349

349350
options = handler.options
350351
options.west_flash = "args"
@@ -386,6 +387,8 @@ def test_pytest__generate_parameters_for_hardware(tmp_path, pty_value, hardware_
386387
assert '--pre-script=pre_script' in command
387388
assert '--post-flash-script=post_flash_script' in command
388389
assert '--post-script=post_script' in command
390+
assert '--twister-fixture=fixture1:option1' in command
391+
assert '--twister-fixture=fixture2' in command
389392

390393

391394
def test__update_command_with_env_dependencies():

0 commit comments

Comments
 (0)