1# Copyright: (c)  2025, Intel Corporation
2import json
3import logging
4
5import pytest
6from twister_harness import DeviceAdapter
7
8
9def pytest_addoption(parser):
10    parser.addoption('--testdata')
11
12
13@pytest.fixture
14def probe_class(request, probe_path):
15    path = probe_path  # Get path of power device
16    if request.param == 'stm_powershield':
17        from stm32l562e_dk.PowerShield import PowerShield
18
19        probe = PowerShield()  # Instantiate the power monitor probe
20        probe.connect(path)
21        probe.init()
22
23    yield probe
24
25    if request.param == 'stm_powershield':
26        probe.disconnect()
27
28
29@pytest.fixture(name='probe_path', scope='session')
30def fixture_probe_path(request, dut: DeviceAdapter):
31    for fixture in dut.device_config.fixtures:
32        if fixture.startswith('pm_probe'):
33            probe_port = fixture.split(':')[1]
34    return probe_port
35
36
37@pytest.fixture(name='test_data', scope='session')
38def fixture_test_data(request):
39    # Get test data from the configuration and parse it into a dictionary
40    measurements = request.config.getoption("--testdata")
41    measurements = measurements.replace("'", '"')  # Ensure the data is properly formatted as JSON
42    measurements_dict = json.loads(measurements)
43
44    # Data validation
45    required_keys = [
46        'elements_to_trim',
47        'min_peak_distance',
48        'min_peak_height',
49        'peak_padding',
50        'measurement_duration',
51        'num_of_transitions',
52        'expected_rms_values',
53        'tolerance_percentage',
54    ]
55
56    for key in required_keys:
57        if key not in measurements_dict:
58            logging.error(f"Missing required test data key: {key}")
59            pytest.fail(f"Missing required test data key: {key}")
60
61    return measurements_dict
62