3
3
4
4
5
5
try :
6
- from classes import new_aliens_collection
7
- except ImportError as err :
8
- raise ImportError ("We tried to import the new_aliens_collection() function, "
9
- "but could not find it. Did you remember to create it?" ) from err
6
+ from classes import Alien
7
+ except ImportError as import_fail :
8
+ # pylint: disable=raise-missing-from
9
+ raise ImportError ("\n \n MISSING CLASS --> We tried to import the 'Alien' class from "
10
+ "your classes.py file, but could not find it."
11
+ "Did you misname or forget to create it?" ) from None
10
12
11
13
try :
12
- from classes import Alien
14
+ from classes import new_aliens_collection
13
15
except ImportError as err :
14
- raise ImportError ("We tried to import the 'Alien' class from the classes.py file, but could not find it. "
15
- "Did you remember to create it?" ) from err
16
+ raise ImportError ("\n \n MISSING FUNCTION --> We tried to import the "
17
+ "new_aliens_collection() function "
18
+ "from your classes.py file, but could not find it. "
19
+ "Did you misname or forget to create it?" ) from None
16
20
17
21
18
22
class ClassesTest (unittest .TestCase ):
19
- # Test Alien class exists and correctly initialised.
23
+
20
24
@pytest .mark .task (taskno = 1 )
21
25
def test_alien_has_correct_initial_coordinates (self ):
26
+ """Test thst the Alien class gets correctly initialised."""
27
+
22
28
alien = Alien (2 , - 1 )
23
- error = ("Expected object to be at position (2, -1) but instead "
24
- f"found it initialized to position { (alien .x_coordinate , alien .y_coordinate )} ." )
29
+ error_message = (f'Created a new Alien by calling Alien(2, -1). '
30
+ f'The Alien was initialized to position '
31
+ f'{ (alien .x_coordinate , alien .y_coordinate )} , but the tests expected '
32
+ f'the object to be at position (2, -1)' )
25
33
26
- self .assertEqual ((2 , - 1 ), (alien .x_coordinate , alien .y_coordinate ), msg = error )
34
+ self .assertEqual ((2 , - 1 ), (alien .x_coordinate , alien .y_coordinate ), msg = error_message )
27
35
28
36
@pytest .mark .task (taskno = 1 )
29
37
def test_alien_has_health (self ):
30
38
alien = Alien (0 , 0 )
31
- error = ("Expected object's health to be 3 but instead found "
32
- f"it had a health of { alien .health } ." )
39
+ error_message = (f'Created a new Alien by calling Alien(0, 0). '
40
+ f'The new Alien has a health of { alien .health } , '
41
+ f'but the tests expect health = 3' )
33
42
34
- self .assertEqual (3 , alien .health , msg = error )
43
+ self .assertEqual (3 , alien .health , msg = error_message )
35
44
36
- # Test instance variables are unique to specific instances.
37
45
@pytest .mark .task (taskno = 1 )
38
46
def test_alien_instance_variables (self ):
47
+ """Test instance variables are unique to specific instances."""
48
+
39
49
alien_one = Alien (- 8 , - 1 )
40
50
alien_two = Alien (2 , 5 )
41
51
42
- coord_x_error = ("Expected alien_one and alien_two to have different x "
43
- f"positions. Instead both x's were: { alien_two .x_coordinate } ." )
44
- coord_y_error = ("Expected alien_one and alien_two to have different y "
45
- f"positions. Instead both y's were: { alien_two .y_coordinate } ." )
52
+ coord_x_error = (f'Created two new Aliens by assigning '
53
+ f'alien_one = Alien(-8, -1) and alien_two = Alien(2, 5). '
54
+ f'Both Aliens x coordinates were { alien_two .x_coordinate } , '
55
+ f'but the tests expect alien_one and alien_two to have '
56
+ f'different x positions.' )
57
+
58
+ coord_y_error = (f'Created two new Aliens by assigning '
59
+ f'alien_one = Alien(-8, -1) and alien_two = Alien(2, 5). '
60
+ f'Both Aliens y coordinates were { alien_two .y_coordinate } , '
61
+ f'but the tests expect alien_one and alien_two to have '
62
+ f'different y positions.' )
46
63
47
64
self .assertFalse (alien_one .x_coordinate == alien_two .x_coordinate , msg = coord_x_error )
48
65
self .assertFalse (alien_one .y_coordinate == alien_two .y_coordinate , msg = coord_y_error )
49
66
50
- # Test class methods work as specified.
67
+
51
68
@pytest .mark .task (taskno = 2 )
52
69
def test_alien_hit_method (self ):
53
- #There are two valid interpretations for this method/task.
54
- #`self.health -= 1` and `self.health = max(0, self.health - 1)`
55
- #The tests for this task reflect this ambiguity.
70
+ """Test class methods work as specified.
71
+
72
+ There are two valid interpretations for this method/task.
73
+ `self.health -= 1` and `self.health = max(0, self.health - 1)`
74
+ The tests for this task reflect this ambiguity.
56
75
57
- data = [(1 , (2 ,)), (2 , (1 ,)), (3 , (0 ,)), (4 , (0 , - 1 )), (5 , (0 , - 2 )), (6 , (0 , - 3 ))]
58
- for variant , (iterations , result ) in enumerate (data , 1 ):
76
+ """
77
+
78
+ test_data = [1 , 2 , 3 , 4 , 5 , 6 ]
79
+ result_data = [(2 ,), (1 ,), (0 ,), (0 , - 1 ), (0 , - 2 ), (0 , - 3 )]
80
+
81
+ for variant , (iterations , expected ) in enumerate (zip (test_data , result_data ), start = 1 ):
59
82
alien = Alien (2 , 2 )
60
- with self .subTest (f'variation #{ variant } ' , input = iterations , output = result ):
61
- error = ("Expected hit method to decrement health by 1. "
62
- f"Health is { alien .health } when it should be { result } ." )
83
+
84
+ with self .subTest (f'variation #{ variant } ' ,
85
+ iterations = iterations ,
86
+ expected = expected ):
87
+
63
88
for _ in range (iterations ):
64
89
alien .hit ()
65
- self .assertIn (alien .health , result , msg = error )
90
+
91
+ error_message = (f'Called hit() { iterations } time(s) '
92
+ f'on a newly created Alien. The Aliens health '
93
+ f'is now { alien .health } , but the tests expected '
94
+ f'it to be in { expected } after decrementing 1 health '
95
+ f'point { iterations } time(s).' )
96
+
97
+ self .assertIn (alien .health , expected , msg = error_message )
66
98
67
99
68
100
@pytest .mark .task (taskno = 3 )
69
101
def test_alien_is_alive_method (self ):
70
102
alien = Alien (0 , 1 )
71
- alive_error = "Alien is dead while health is greater than 0."
72
- dead_error = "Alien is alive while health is less than or equal to 0."
103
+
104
+ alive_error = ('Created a new Alien and called hit(). '
105
+ 'The function is_alive() is returning False (dead) '
106
+ 'while alien.health is greater than 0.' )
107
+
108
+ dead_error = ('Created a new Alien and called hit(). '
109
+ 'The function is_alive() is returning True (alive) '
110
+ 'while alien.health is less than or equal to 0.' )
73
111
74
112
for _ in range (5 ):
75
113
alien .hit ()
@@ -83,70 +121,92 @@ def test_alien_teleport_method(self):
83
121
alien = Alien (0 , 0 )
84
122
alien .teleport (- 1 , - 4 )
85
123
86
- error = (
87
- "Expected alien to be at position (-1, -4) but "
88
- f"instead found it in position { (alien .x_coordinate , alien .y_coordinate )} ." )
124
+ error_message = ('Called alien.teleport(-1,-4) on a newly created Alien. '
125
+ 'The Alien was found at position '
126
+ f'{ (alien .x_coordinate , alien .y_coordinate )} , but the '
127
+ 'tests expected it at position (-1, -4).' )
89
128
90
- self .assertEqual ((- 1 , - 4 ), (alien .x_coordinate , alien .y_coordinate ), msg = error )
129
+ self .assertEqual ((- 1 , - 4 ), (alien .x_coordinate , alien .y_coordinate ), msg = error_message )
91
130
92
131
@pytest .mark .task (taskno = 5 )
93
132
def test_alien_collision_detection_method (self ):
94
133
alien = Alien (7 , 3 )
95
- error = "Expected collision_detection method to not be implemented."
134
+ error_message = ('Created a new Alien at (7,3) and called '
135
+ 'alien.collision_detection(Alien(7, 2)). '
136
+ f'The method returned { alien .collision_detection (Alien (7 , 2 ))} , '
137
+ 'but the tests expected None. ' )
138
+
139
+ self .assertIsNone (alien .collision_detection (Alien (7 , 2 )), msg = error_message )
96
140
97
- self .assertIsNone (alien .collision_detection (Alien (7 , 2 )), msg = error )
98
141
99
- # Test class variables are identical across instances
100
142
@pytest .mark .task (taskno = 6 )
101
143
def test_alien_class_variable (self ):
102
- alien_one = Alien (0 , 2 )
103
- alien_two = Alien (- 6 , - 1 )
104
- Alien .total_aliens_created = - 2
144
+ """Test class attribute/variables are identical across instances."""
145
+
146
+ alien_one , alien_two = Alien (0 , 2 ), Alien (- 6 , - 1 )
147
+ Alien .health = 6
105
148
106
- error_one = "Expected the total_aliens_created variable to be identical."
107
- error_two = "Expected the health variable to be identical."
149
+ created_error_message = ('Created two new Aliens and requested the '
150
+ 'total_aliens_created attribute for each one. '
151
+ f'Received { alien_one .total_aliens_created , alien_two .total_aliens_created } '
152
+ f'for total_aliens_created, but the tests expect '
153
+ f'the class attributes for each newly created Alien to be identical. ' )
108
154
109
- self .assertEqual (alien_two .total_aliens_created , alien_one .total_aliens_created , msg = error_one )
110
- self .assertEqual (alien_two .health , alien_one .health , msg = error_two )
155
+ health_error_message = ('Created two new Aliens and requested the '
156
+ f'health attribute for each one. Received { alien_one .health , alien_two .health } '
157
+ 'for health, but the tests expect the class '
158
+ 'attributes for each newly created Alien to be identical. ' )
159
+
160
+ self .assertEqual (alien_two .total_aliens_created ,
161
+ alien_one .total_aliens_created ,
162
+ msg = created_error_message )
163
+
164
+ self .assertEqual (alien_two .health ,
165
+ alien_one .health ,
166
+ msg = health_error_message )
111
167
112
- # Test total_aliens_created increments upon object instantiation
113
168
@pytest .mark .task (taskno = 6 )
114
169
def test_alien_total_aliens_created (self ):
170
+ """Test total_aliens_created class variable increments upon object instantiation."""
171
+
115
172
Alien .total_aliens_created = 0
116
173
aliens = [Alien (- 2 , 6 )]
117
- error = ("Expected total_aliens_created to equal 1. Instead "
118
- f"it equals: { aliens [0 ].total_aliens_created } ." )
119
174
120
- self .assertEqual (1 , aliens [0 ].total_aliens_created , msg = error )
175
+ error_message = ('Created a new Alien and called total_aliens_created for it. '
176
+ f'{ aliens [0 ].total_aliens_created } was returned, but '
177
+ 'the tests expected that total_aliens_created would equal 1.' )
178
+
179
+ self .assertEqual (1 , aliens [0 ].total_aliens_created , msg = error_message )
121
180
122
181
aliens .append (Alien (3 , 5 ))
123
182
aliens .append (Alien (- 5 , - 5 ))
124
183
125
184
def error_text (alien , variable ):
126
- return (
127
- "Expected all total_aliens_created variables to be "
128
- " equal to number of alien instances (i.e. 3). Alien "
129
- f"number { alien } 's total_aliens_created variable "
130
- f"is equal to { variable } ." )
185
+ return ('Created two additional Aliens for the session.'
186
+ f"Alien number { alien } 's total_aliens_created variable "
187
+ f"is equal to { variable } , but the tests expected all "
188
+ ' total_aliens_created variables for all instances to be '
189
+ ' equal to number of alien instances created (i.e. 3).' )
131
190
132
- tac_list = [alien .total_aliens_created for alien in aliens ]
191
+ self .assertEqual (3 , aliens [0 ].total_aliens_created , msg = error_text (1 , aliens [0 ]))
192
+ self .assertEqual (3 , aliens [1 ].total_aliens_created , msg = error_text (2 , aliens [1 ]))
193
+ self .assertEqual (3 , aliens [2 ].total_aliens_created , msg = error_text (3 , aliens [2 ]))
133
194
134
- self .assertEqual (3 , tac_list [0 ], msg = error_text (1 , tac_list [0 ]))
135
- self .assertEqual (3 , tac_list [1 ], msg = error_text (2 , tac_list [1 ]))
136
- self .assertEqual (3 , tac_list [2 ], msg = error_text (3 , tac_list [2 ]))
137
-
138
- # Test that the user knows how to create objects themselves
139
195
@pytest .mark .task (taskno = 7 )
140
196
def test_new_aliens_collection (self ):
141
- position_data = [(- 2 , 6 ), (1 , 5 ), (- 4 , - 3 )]
142
- obj_list = new_aliens_collection (position_data )
143
- obj_error = "new_aliens_collection must return a list of Alien objects."
197
+ """Test that the user knows how to create objects themselves."""
198
+
199
+ test_data = [(- 2 , 6 ), (1 , 5 ), (- 4 , - 3 )]
200
+ actual_result = new_aliens_collection (test_data )
201
+
202
+ error_message = "new_aliens_collection() must return a list of Alien objects."
144
203
145
- for obj , position in zip ( obj_list , position_data ) :
146
- self .assertIsInstance (obj , Alien , msg = obj_error )
204
+ for obj in actual_result :
205
+ self .assertIsInstance (obj , Alien , msg = error_message )
147
206
148
- pos_error = (
149
- f"Expected object to be at position { position } but "
150
- f"instead found it initialized to position { (obj .x_coordinate , obj .y_coordinate )} ." )
207
+ for position , obj in zip (test_data , actual_result ):
208
+ position_error = (f'After calling new_aliens_collection({ test_data } ), '
209
+ f'found { obj } initialized to position { (obj .x_coordinate , obj .y_coordinate )} , '
210
+ f'but the tests expected { obj } to be at position { position } instead.' )
151
211
152
- self .assertEqual (position , (obj .x_coordinate , obj .y_coordinate ), msg = pos_error )
212
+ self .assertEqual (position , (obj .x_coordinate , obj .y_coordinate ), msg = position_error )
0 commit comments