Skip to content

Commit 3827974

Browse files
refactor(test): remove duplicated code in conftest.py (#804)
1 parent b299cfe commit 3827974

File tree

1 file changed

+17
-52
lines changed

1 file changed

+17
-52
lines changed

tests/conftest.py

+17-52
Original file line numberDiff line numberDiff line change
@@ -36,82 +36,47 @@ def pytest_collection_finish():
3636
print(f"\nTesting with {DEVICE=}")
3737

3838

39-
@pytest.fixture
40-
def is_robot_available(robot_type):
41-
if robot_type not in available_robots:
39+
def _check_component_availability(component_type, available_components, make_component):
40+
"""Generic helper to check if a hardware component is available"""
41+
if component_type not in available_components:
4242
raise ValueError(
43-
f"The robot type '{robot_type}' is not valid. Expected one of these '{available_robots}"
43+
f"The {component_type} type is not valid. Expected one of these '{available_components}'"
4444
)
4545

4646
try:
47-
robot = make_robot(robot_type)
48-
robot.connect()
49-
del robot
47+
component = make_component(component_type)
48+
component.connect()
49+
del component
5050
return True
5151

5252
except Exception as e:
53-
print(f"\nA {robot_type} robot is not available.")
53+
print(f"\nA {component_type} is not available.")
5454

5555
if isinstance(e, ModuleNotFoundError):
5656
print(f"\nInstall module '{e.name}'")
5757
elif isinstance(e, SerialException):
58-
print("\nNo physical motors bus detected.")
58+
print("\nNo physical device detected.")
59+
elif isinstance(e, ValueError) and "camera_index" in str(e):
60+
print("\nNo physical camera detected.")
5961
else:
6062
traceback.print_exc()
6163

6264
return False
6365

6466

6567
@pytest.fixture
66-
def is_camera_available(camera_type):
67-
if camera_type not in available_cameras:
68-
raise ValueError(
69-
f"The camera type '{camera_type}' is not valid. Expected one of these '{available_cameras}"
70-
)
71-
72-
try:
73-
camera = make_camera(camera_type)
74-
camera.connect()
75-
del camera
76-
return True
68+
def is_robot_available(robot_type):
69+
return _check_component_availability(robot_type, available_robots, make_robot)
7770

78-
except Exception as e:
79-
print(f"\nA {camera_type} camera is not available.")
8071

81-
if isinstance(e, ModuleNotFoundError):
82-
print(f"\nInstall module '{e.name}'")
83-
elif isinstance(e, ValueError) and "camera_index" in e.args[0]:
84-
print("\nNo physical camera detected.")
85-
else:
86-
traceback.print_exc()
87-
88-
return False
72+
@pytest.fixture
73+
def is_camera_available(camera_type):
74+
return _check_component_availability(camera_type, available_cameras, make_camera)
8975

9076

9177
@pytest.fixture
9278
def is_motor_available(motor_type):
93-
if motor_type not in available_motors:
94-
raise ValueError(
95-
f"The motor type '{motor_type}' is not valid. Expected one of these '{available_motors}"
96-
)
97-
98-
try:
99-
motors_bus = make_motors_bus(motor_type)
100-
motors_bus.connect()
101-
del motors_bus
102-
return True
103-
104-
except Exception as e:
105-
print(f"\nA {motor_type} motor is not available.")
106-
107-
if isinstance(e, ModuleNotFoundError):
108-
print(f"\nInstall module '{e.name}'")
109-
elif isinstance(e, SerialException):
110-
print("\nNo physical motors bus detected.")
111-
else:
112-
traceback.print_exc()
113-
114-
return False
79+
return _check_component_availability(motor_type, available_motors, make_motors_bus)
11580

11681

11782
@pytest.fixture

0 commit comments

Comments
 (0)