#!/usr/bin/env python3
# HIL test suite using pytest framework
# Run: pytest test_abs_suite.py -v --html=report.html
import pytest, time
# ---- Fixtures ----
@pytest.fixture(scope="session")
def hil():
"""Connect to HIL platform once per test session"""
from hil_interface import HILInterface
h = HILInterface("SCALEXIO_01")
h.load_model("VehicleDynamics_v2.3.sdf")
h.start()
yield h
h.stop()
@pytest.fixture(autouse=True)
def reset_to_nominal(hil):
"""Before each test: reset to known initial state"""
hil.set("Plant/Speed_kph", 0.0)
hil.set("Plant/Brakes/PedalPct", 0.0)
hil.clear_all_faults()
hil.wait_stable(timeout_s=2)
yield
hil.clear_all_faults() # cleanup after each test
# ---- Functional Tests ----
def test_abs_activates_on_panic_brake(hil):
"""REQ: ABS-SYS-002 - ABS shall activate within 100ms of wheel lockup"""
hil.set("Plant/Speed_kph", 100.0)
hil.wait(2.0)
hil.set("Plant/Brakes/PedalPct", 100.0)
abs_active = hil.wait_signal("ECU.ABS.Active", lambda v: v > 0.5,
timeout_ms=200)
assert abs_active, "ABS did not activate within 200 ms"
def test_abs_prevents_wheel_lockup(hil):
"""REQ: ABS-SYS-003 - Wheel speed shall remain > 5 km/h during ABS"""
hil.set("Plant/Speed_kph", 80.0)
hil.wait(2.0)
hil.set("Plant/Brakes/PedalPct", 100.0)
hil.wait(0.5) # let ABS activate
for _ in range(50): # monitor for 2.5 s
fl_speed = hil.get("Plant.Wheel.FL.SpeedKph")
assert fl_speed > 5.0, f"Wheel locked: FL={fl_speed:.1f} km/h"
time.sleep(0.05)Exercise 1: pytest-Based HIL Test Suite
Exercise 2: Parameterised Fault Injection Tests
#!/usr/bin/env python3
# Parameterised fault injection tests using pytest.mark.parametrize
import pytest
SENSOR_FAULT_CASES = [
# (sensor_channel, fault_type, expected_dtc, max_detection_ms)
("WSS_FL", "SHORT_GND", "C0035", 20),
("WSS_FR", "SHORT_GND", "C0040", 20),
("WSS_FL", "OPEN_CIRCUIT", "C0035", 20),
("WSS_FL", "STUCK_HIGH", "C0050", 50), # plausibility check
("BRAKE_PR", "SHORT_GND", "C0110", 50),
("BRAKE_PR", "SHORT_BAT", "C0115", 50),
]
@pytest.mark.parametrize("channel,fault,dtc,max_ms", SENSOR_FAULT_CASES)
def test_sensor_fault_detection(hil, ecu_monitor, channel, fault, dtc, max_ms):
f"""Fault {fault} on {channel} shall set DTC {dtc} within {max_ms} ms"""
hil.set("Plant/Speed_kph", 50.0)
hil.wait(2.0)
hil.inject_fault(channel, fault)
time.sleep(max_ms / 1000.0 + 0.05) # wait FHTI + margin
dtcs = ecu_monitor.read_dtcs()
assert dtc in dtcs, f"DTC {dtc} not set after {fault} on {channel}"Summary
A well-structured pytest HIL test suite provides the foundation for continuous integration: the same test suite that engineers run manually during development can be triggered automatically by Jenkins or GitLab CI on every ECU software build. The fixture pattern (session-scoped HIL connection + function-scoped reset) ensures test isolation without reconnecting to the HIL platform for each test - reducing total test suite execution time by 10-50x. Parameterised fault injection tests make it easy to add new fault-DTC pairs to the test matrix with a single line of data, keeping test coverage growing as the fault catalogue expands.
🔬 Deep Dive — Core Concepts Expanded
This section builds on the foundational concepts covered above with additional technical depth, edge cases, and configuration nuances that separate competent engineers from experts. When working on production ECU projects, the details covered here are the ones most commonly responsible for integration delays and late-phase defects.
Key principles to reinforce:
- Configuration over coding: In AUTOSAR and automotive middleware environments, correctness is largely determined by ARXML configuration, not application code. A correctly implemented algorithm can produce wrong results due to a single misconfigured parameter.
- Traceability as a first-class concern: Every configuration decision should be traceable to a requirement, safety goal, or architecture decision. Undocumented configuration choices are a common source of regression defects when ECUs are updated.
- Cross-module dependencies: In tightly integrated automotive software stacks, changing one module's configuration often requires corresponding updates in dependent modules. Always perform a dependency impact analysis before submitting configuration changes.
🏭 How This Topic Appears in Production Projects
- Project integration phase: The concepts covered in this lesson are most commonly encountered during ECU integration testing — when multiple software components from different teams are combined for the first time. Issues that were invisible in unit tests frequently surface at this stage.
- Supplier/OEM interface: This is a topic that frequently appears in technical discussions between Tier-1 ECU suppliers and OEM system integrators. Engineers who can speak fluently about these details earn credibility and are often brought into critical design review meetings.
- Automotive tool ecosystem: Vector CANoe/CANalyzer, dSPACE tools, and ETAS INCA are the standard tools used to validate and measure the correct behaviour of the systems described in this lesson. Familiarity with these tools alongside the conceptual knowledge dramatically accelerates debugging in real projects.
⚠️ Common Mistakes and How to Avoid Them
- Assuming default configuration is correct: Automotive software tools ship with default configurations that are designed to compile and link, not to meet project-specific requirements. Every configuration parameter needs to be consciously set. 'It compiled' is not the same as 'it is correctly configured'.
- Skipping documentation of configuration rationale: In a 3-year ECU project with team turnover, undocumented configuration choices become tribal knowledge that disappears when engineers leave. Document why a parameter is set to a specific value, not just what it is set to.
- Testing only the happy path: Automotive ECUs must behave correctly under fault conditions, voltage variations, and communication errors. Always test the error handling paths as rigorously as the nominal operation. Many production escapes originate in untested error branches.
- Version mismatches between teams: In a multi-team project, the BSW team, SWC team, and system integration team may use different versions of the same ARXML file. Version management of all ARXML files in a shared repository is mandatory, not optional.
📊 Industry Note
Engineers who master both the theoretical concepts and the practical toolchain skills covered in this course are among the most sought-after professionals in the automotive software industry. The combination of AUTOSAR standards knowledge, safety engineering understanding, and hands-on configuration experience commands premium salaries at OEMs and Tier-1 suppliers globally.