diff --git a/tests/conftest.py b/tests/conftest.py index dc746142..7eec94bf 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -36,51 +36,27 @@ def pytest_collection_finish(): print(f"\nTesting with {DEVICE=}") -@pytest.fixture -def is_robot_available(robot_type): - if robot_type not in available_robots: +def _check_component_availability(component_type, available_components, make_component): + """Generic helper to check if a hardware component is available""" + if component_type not in available_components: 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: - robot = make_robot(robot_type) - robot.connect() - del robot + component = make_component(component_type) + component.connect() + del component return True 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): print(f"\nInstall module '{e.name}'") elif isinstance(e, SerialException): - print("\nNo physical motors bus detected.") - else: - 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 device detected.") + elif isinstance(e, ValueError) and "camera_index" in str(e): print("\nNo physical camera detected.") else: traceback.print_exc() @@ -88,30 +64,19 @@ def is_camera_available(camera_type): 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 def is_motor_available(motor_type): - if motor_type not in available_motors: - 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 + return _check_component_availability(motor_type, available_motors, make_motors_bus) @pytest.fixture