Skip to content

Commit dad559a

Browse files
committed
tests: Add mock_pins and hardware_init support to test framework.
1 parent 5cff376 commit dad559a

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

tests/runner/mpremote_bridge.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,15 @@ def _driver_dir(self, driver_name):
6767
def call_method(
6868
self, driver_name, driver_class, i2c_config, method,
6969
args=None, i2c_address=None, module_name=None,
70+
hardware_init=None,
7071
):
7172
"""Call a method on a driver instance and return the result."""
7273
mod = module_name or driver_name
7374
args_str = ", ".join(repr(a) for a in (args or []))
7475
i2c_init = _i2c_init_code(i2c_config)
75-
if i2c_address is not None:
76+
if hardware_init is not None:
77+
dev_init = hardware_init + "\n"
78+
elif i2c_address is not None:
7679
dev_init = f"dev = {driver_class}(i2c, address={i2c_address!r})\n"
7780
else:
7881
dev_init = f"dev = {driver_class}(i2c)\n"

tests/test_scenarios.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pytest
77

88
from tests.fake_machine.i2c import FakeI2C
9+
from tests.fake_machine.pin import FakePin
910
from tests.runner.executor import (
1011
check_result,
1112
cleanup_driver,
@@ -59,8 +60,13 @@ def make_mock_instance(scenario):
5960
fake_i2c = FakeI2C(registers=registers, address=address)
6061
driver_module, _ = load_driver_mock(driver_name, fake_i2c, module_name=module_name)
6162

63+
# Build extra constructor kwargs from mock_pins
64+
extra_kwargs = {}
65+
for pin_name, pin_id in scenario.get("mock_pins", {}).items():
66+
extra_kwargs[pin_name] = FakePin(pin_id)
67+
6268
cls = getattr(driver_module, driver_class)
63-
instance = cls(fake_i2c, address=address)
69+
instance = cls(fake_i2c, address=address, **extra_kwargs)
6470
return instance, driver_name
6571

6672

@@ -92,6 +98,7 @@ def test_scenario(scenario, test, mode, port):
9298
display["method"],
9399
display.get("args"),
94100
module_name=scenario.get("module_name"),
101+
hardware_init=scenario.get("hardware_init"),
95102
)
96103
label = display.get("label", display["method"])
97104
unit = display.get("unit", "")
@@ -110,6 +117,7 @@ def test_scenario(scenario, test, mode, port):
110117
test["method"],
111118
test.get("args"),
112119
module_name=scenario.get("module_name"),
120+
hardware_init=scenario.get("hardware_init"),
113121
)
114122
elif action == "read_register":
115123
result = bridge.read_register(

0 commit comments

Comments
 (0)