Tests welded to the simulator would need a rewrite for a real board.
Change
I2C, UART, GPIO and power become Protocols. The simulator is one implementation.
framework/vlab/hal/i2c.py · I2CBus
classI2CBus(Protocol):
"""Register reads and writes on a 7-bit address.
On bench 1 the target is the sensor (a sensor emulator in the lab): the
bench writes the value the DUT reads. Registers auto-increment, so a read
or write of n bytes covers registers reg to reg + n - 1.
Raises DriverError when no target answers at `addr`, and ValueError for an
address, register or length out of range.
"""defread(self, addr: int, reg: int, n: int) -> bytes: ...
defwrite(self, addr: int, reg: int, data: bytes) -> None: ...
Step 2 / 9
A bench is built from YAML
Problem
Wiring devices by hand in every test file drifts and breaks quietly.
Change
BenchFactory builds the devices from bench.yaml in dependency order and opens their drivers in one ExitStack.
defstep(self, deadline_ms: int) -> None:
"""Run what is due now. If nothing is, move to the next event or the deadline."""if deadline_ms < self._now:
raise ClockError(f"deadline {deadline_ms} ms is before now, {self._now} ms")
if self._run_due():
return
nxt = self.next_event_ms()
self._now = deadline_ms if nxt isNoneelse min(nxt, deadline_ms)
self._run_due()
Step 4 / 9
Device model
Problem
Tests full of register writes and sleeps say nothing about the requirement.
Change
Tests talk to TempMonitor only. Waits take their timeout from the spec.
defwait_led(self, on: bool, *, within_ms: int | None = None) -> int:
"""Wait for the LED to be `on`, by default within the Q16 limit. Return the time seen."""
limit = self.timeouts.led_ms if within_ms isNoneelse within_ms
state = "on"if on else"off"return wait_for(
self._clock,
lambda: self.now_ms() if self.led_on() is on elseNone,
within_ms=limit,
what=f"LED {state}",
reason=f"Q16 assumed: the LED follows within {self.timeouts.led_ms} ms",
log=self.waits,
last_seen=lambda: f"LED {'on'if self.led_on() else'off'}",
)
Step 5 / 9
STD markers
Problem
Nothing links the code to the STD. Coverage is a guess.
Change
Every test carries its STD ID in a marker. The plugin checks the ID.
framework/vlab/plugin/markers.py · tc
deftc(case_id: str) -> pytest.MarkDecorator:
"""The STD case this test runs."""return pytest.mark.tc(check_tc(case_id))
Step 6 / 9
Points from data
Problem
Values typed by hand drift away from the documents.
Change
Points load from std.yaml, the same file this site shows.
framework/vlab/plugin/markers.py · each
defeach(*names: str) -> pytest.MarkDecorator:
"""Run each value of the named point sets and test conditions of the card as its own test."""ifnot names ornot all(isinstance(n, str) and n.strip() for n in names):
raise ValueError(f"@each{names!r}: name at least one point set or test condition")
if len(set(names)) != len(names):
raise ValueError(f"@each{names!r}: a name repeats")
return pytest.mark.each(*names)
Step 7 / 9
Fault injection API
Problem
Every fault needed its own hand-made setup.
Change
One call injects a fault on any target that supports it. A target without it raises a precondition error, so the test carries @requires.
definject_fault(self, fault: Fault | str, *, count: int | None = None) -> None:
"""Make the sensor fail the next `count` reads by the DUT, or every read until cleared.
A target that can't inject `fault` raises PreconditionError: the case
should carry `@requires(fault)` and be skipped there (ADR-005).
"""
name = str(fault)
if self._faults isNoneor name notin self._faults.capabilities:
raise PreconditionError(
f"this target can't inject {name!r}, mark the test @requires({name!r}). "f"It can: {', '.join(sorted(self.capabilities))}"
)
faults = self._faults
self._call("i2c", f"inject {name}", lambda: faults.inject(name, count=count))
self._changed_ms = self.now_ms()
Step 8 / 9
Artifacts on failure
Problem
A red test without logs costs an hour of guessing.
Change
Every record that did not pass gets a text file: traceback, output and waits.