Skip to content

Commit 1f6aab8

Browse files
authored
[Ellen's Alien Game] Test that an instance variable is used for the health, not a class variable. (#4273)
* [Ellen's Alien Game] Test that an instance variable is used for the health, not a class variable. * Update docstrings and strings (quotes, trailing spaces) * Drop unneeded test * Replace tab with spaces
1 parent ebaded9 commit 1f6aab8

1 file changed

Lines changed: 34 additions & 23 deletions

File tree

exercises/concept/ellens-alien-game/classes_test.py

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
except ImportError as import_fail:
88
# pylint: disable=raise-missing-from
99
raise ImportError("\n\nMISSING 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+
'your classes.py file, but could not find it. '
11+
'Did you misname or forget to create it?') from None
1212

1313
try:
1414
from classes import new_aliens_collection
1515
except ImportError as err:
16-
raise ImportError("\n\nMISSING 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+
raise ImportError('\n\nMISSING 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
2020

2121

2222
class ClassesTest(unittest.TestCase):
@@ -38,7 +38,7 @@ def test_alien_has_health(self):
3838
alien = Alien(0, 0)
3939
error_message = (f'Created a new Alien by calling Alien(0, 0). '
4040
f'The new Alien has a health of {alien.health}, '
41-
f'but the tests expect health = 3')
41+
f'but the tests expect health = 3.')
4242

4343
self.assertEqual(3, alien.health, msg=error_message)
4444

@@ -72,7 +72,6 @@ def test_alien_hit_method(self):
7272
There are two valid interpretations for this method/task.
7373
`self.health -= 1` and `self.health = max(0, self.health - 1)`
7474
The tests for this task reflect this ambiguity.
75-
7675
"""
7776

7877
test_data = [1, 2, 3, 4, 5, 6]
@@ -99,15 +98,16 @@ def test_alien_hit_method(self):
9998

10099
@pytest.mark.task(taskno=3)
101100
def test_alien_is_alive_method(self):
101+
"""Test the is_alive() method returns the expected values after a number of hits."""
102102
alien = Alien(0, 1)
103103

104104
alive_error = ('Created a new Alien and called hit(). '
105105
'The function is_alive() is returning False (dead) '
106106
'while alien.health is greater than 0.')
107107

108108
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.')
109+
'The function is_alive() is returning True (alive) '
110+
'while alien.health is less than or equal to 0.')
111111

112112
for _ in range(5):
113113
alien.hit()
@@ -118,6 +118,7 @@ def test_alien_is_alive_method(self):
118118

119119
@pytest.mark.task(taskno=4)
120120
def test_alien_teleport_method(self):
121+
"""Test the teleport method updates the alien's coordinates."""
121122
alien = Alien(0, 0)
122123
alien.teleport(-1, -4)
123124

@@ -130,11 +131,12 @@ def test_alien_teleport_method(self):
130131

131132
@pytest.mark.task(taskno=5)
132133
def test_alien_collision_detection_method(self):
134+
"""Test the collision_detection() method can be called and returns None."""
133135
alien = Alien(7, 3)
134136
error_message = ('Created a new Alien at (7,3) and called '
135137
'alien.collision_detection(Alien(7, 2)). '
136138
f'The method returned {alien.collision_detection(Alien(7, 2))}, '
137-
'but the tests expected None. ')
139+
'but the tests expected None.')
138140

139141
self.assertIsNone(alien.collision_detection(Alien(7, 2)), msg=error_message)
140142

@@ -144,26 +146,35 @@ def test_alien_class_variable(self):
144146
"""Test class attribute/variables are identical across instances."""
145147

146148
alien_one, alien_two = Alien(0, 2), Alien(-6, -1)
147-
Alien.health = 6
148149

149150
created_error_message = ('Created two new Aliens and requested the '
150151
'total_aliens_created attribute for each one. '
151152
f'Received {alien_one.total_aliens_created, alien_two.total_aliens_created} '
152153
f'for total_aliens_created, but the tests expect '
153-
f'the class attributes for each newly created Alien to be identical. ')
154-
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. ')
154+
f'the class attributes for each newly created Alien to be identical.')
159155

160156
self.assertEqual(alien_two.total_aliens_created,
161157
alien_one.total_aliens_created,
162158
msg=created_error_message)
163159

164-
self.assertEqual(alien_two.health,
165-
alien_one.health,
166-
msg=health_error_message)
160+
@pytest.mark.task(taskno=6)
161+
def test_alien_health_is_instance_variable(self):
162+
"""Test the health is an instance variable and not a class variable."""
163+
164+
alien_one, alien_two = Alien(0, 2), Alien(-6, -1)
165+
alien_one.hit()
166+
167+
error_message = ('Created two new Aliens and called hit() on one of them. '
168+
f'Received {alien_one.health, alien_two.health} for health, '
169+
'but the tests expect them to have different health as '
170+
'only one was hit. Are you using a class variable for the health?')
171+
172+
# This checks that a class attribute, Alien.health, is not being used.
173+
# If a class attribute is being used, hit() would update the health across
174+
# all instances of the class.
175+
self.assertNotEqual(alien_two.health,
176+
alien_one.health,
177+
msg=error_message)
167178

168179
@pytest.mark.task(taskno=6)
169180
def test_alien_total_aliens_created(self):
@@ -182,9 +193,9 @@ def test_alien_total_aliens_created(self):
182193
aliens.append(Alien(-5, -5))
183194

184195
def error_text(alien, variable):
185-
return ('Created two additional Aliens for the session.'
196+
return ('Created two additional Aliens for the session. '
186197
f"Alien number {alien}'s total_aliens_created variable "
187-
f"is equal to {variable}, but the tests expected all "
198+
f'is equal to {variable}, but the tests expected all '
188199
'total_aliens_created variables for all instances to be '
189200
'equal to number of alien instances created (i.e. 3).')
190201

0 commit comments

Comments
 (0)