@@ -36,82 +36,47 @@ def pytest_collection_finish():
36
36
print (f"\n Testing with { DEVICE = } " )
37
37
38
38
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 :
42
42
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 } ' "
44
44
)
45
45
46
46
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
50
50
return True
51
51
52
52
except Exception as e :
53
- print (f"\n A { robot_type } robot is not available." )
53
+ print (f"\n A { component_type } is not available." )
54
54
55
55
if isinstance (e , ModuleNotFoundError ):
56
56
print (f"\n Install module '{ e .name } '" )
57
57
elif isinstance (e , SerialException ):
58
- print ("\n No physical motors bus detected." )
58
+ print ("\n No physical device detected." )
59
+ elif isinstance (e , ValueError ) and "camera_index" in str (e ):
60
+ print ("\n No physical camera detected." )
59
61
else :
60
62
traceback .print_exc ()
61
63
62
64
return False
63
65
64
66
65
67
@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 )
77
70
78
- except Exception as e :
79
- print (f"\n A { camera_type } camera is not available." )
80
71
81
- if isinstance (e , ModuleNotFoundError ):
82
- print (f"\n Install module '{ e .name } '" )
83
- elif isinstance (e , ValueError ) and "camera_index" in e .args [0 ]:
84
- print ("\n No 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 )
89
75
90
76
91
77
@pytest .fixture
92
78
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"\n A { motor_type } motor is not available." )
106
-
107
- if isinstance (e , ModuleNotFoundError ):
108
- print (f"\n Install module '{ e .name } '" )
109
- elif isinstance (e , SerialException ):
110
- print ("\n No 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 )
115
80
116
81
117
82
@pytest .fixture
0 commit comments