refactor(test): remove duplicated code in conftest.py (#804)

This commit is contained in:
Steven Palma 2025-03-04 10:49:44 +01:00 committed by GitHub
parent b299cfea8a
commit 3827974b58
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 21 additions and 56 deletions

View File

@ -36,51 +36,27 @@ def pytest_collection_finish():
print(f"\nTesting with {DEVICE=}") print(f"\nTesting with {DEVICE=}")
@pytest.fixture def _check_component_availability(component_type, available_components, make_component):
def is_robot_available(robot_type): """Generic helper to check if a hardware component is available"""
if robot_type not in available_robots: if component_type not in available_components:
raise ValueError( raise ValueError(
f"The robot type '{robot_type}' is not valid. Expected one of these '{available_robots}" f"The {component_type} type is not valid. Expected one of these '{available_components}'"
) )
try: try:
robot = make_robot(robot_type) component = make_component(component_type)
robot.connect() component.connect()
del robot del component
return True return True
except Exception as e: except Exception as e:
print(f"\nA {robot_type} robot is not available.") print(f"\nA {component_type} is not available.")
if isinstance(e, ModuleNotFoundError): if isinstance(e, ModuleNotFoundError):
print(f"\nInstall module '{e.name}'") print(f"\nInstall module '{e.name}'")
elif isinstance(e, SerialException): elif isinstance(e, SerialException):
print("\nNo physical motors bus detected.") print("\nNo physical device detected.")
else: elif isinstance(e, ValueError) and "camera_index" in str(e):
traceback.print_exc()
return False
@pytest.fixture
def is_camera_available(camera_type):
if camera_type not in available_cameras:
raise ValueError(
f"The camera type '{camera_type}' is not valid. Expected one of these '{available_cameras}"
)
try:
camera = make_camera(camera_type)
camera.connect()
del camera
return True
except Exception as e:
print(f"\nA {camera_type} camera is not available.")
if isinstance(e, ModuleNotFoundError):
print(f"\nInstall module '{e.name}'")
elif isinstance(e, ValueError) and "camera_index" in e.args[0]:
print("\nNo physical camera detected.") print("\nNo physical camera detected.")
else: else:
traceback.print_exc() traceback.print_exc()
@ -88,30 +64,19 @@ def is_camera_available(camera_type):
return False return False
@pytest.fixture
def is_robot_available(robot_type):
return _check_component_availability(robot_type, available_robots, make_robot)
@pytest.fixture
def is_camera_available(camera_type):
return _check_component_availability(camera_type, available_cameras, make_camera)
@pytest.fixture @pytest.fixture
def is_motor_available(motor_type): def is_motor_available(motor_type):
if motor_type not in available_motors: return _check_component_availability(motor_type, available_motors, make_motors_bus)
raise ValueError(
f"The motor type '{motor_type}' is not valid. Expected one of these '{available_motors}"
)
try:
motors_bus = make_motors_bus(motor_type)
motors_bus.connect()
del motors_bus
return True
except Exception as e:
print(f"\nA {motor_type} motor is not available.")
if isinstance(e, ModuleNotFoundError):
print(f"\nInstall module '{e.name}'")
elif isinstance(e, SerialException):
print("\nNo physical motors bus detected.")
else:
traceback.print_exc()
return False
@pytest.fixture @pytest.fixture