def test_alien_is_alive_method(self): alien = Alien(0, 1) alive_error = "Alien is dead while health is greater than 0." dead_error = "Alien is alive while health is less than or equal to 0." for _ in range(5): alien.hit() if alien.health > 0: self.assertTrue(alien.is_alive(), msg=alive_error) else: self.assertFalse(alien.is_alive(), msg=dead_error)
def test_alien_hit_method(self): # There are two valid interpretations for this method/task. # `self.health -= 1` and `self.health = max(0, self.health - 1)` # The tests for this task reflect this ambiguity. data = [(1, (2,)), (2, (1,)), (3, (0,)), (4, (0, -1)), (5, (0, -2)), (6, (0, -3))] for variant, (iterations, result) in enumerate(data, 1): alien = Alien(2, 2) with self.subTest(f'variation #{variant}', input=iterations, output=result): error = ("Expected hit method to decrement health by 1. " f"Health is {alien.health} when it should be {result}.") for _ in range(iterations): alien.hit() self.assertIn(alien.health, result, msg=error)