def test_shoot_at(): # This test greatly increases the testing duration because the tested # methods involve various sleeps hp = 10 a = Unit(hp, 100, 10, 1, 'Test', 'Alfa', '', BallisticPistol(), [], []) b = Unit(hp, 50, 10, 1, 'Test', 'Bravo', '', None, [], []) # randomness is difficult to test test_fulfilled = False while not test_fulfilled: if a.shoot_at(b): # the method reported success, so unit a should have hit assert b.hp < hp test_fulfilled = True # else: the method reported a miss, try again # reload the weapon; weapon.reload() is not used because it sleeps a.weapon.ammo = a.weapon.clip_size b.hp = hp a.aim = 0 test_fulfilled = False while not test_fulfilled: if a.shoot_at(b): # the method reported a hit, reset hp b.hp = hp # reload the weapon; weapon.reload() is not used because it sleeps a.weapon.ammo = a.weapon.clip_size else: assert b.hp == hp test_fulfilled = True
def test_death(): death_handler_called = False def death_handler(): nonlocal death_handler_called death_handler_called = True a = Unit(10, 10, 10, 1, 'Test', 'Alfa', '', BallisticPistol(), [], []) a._handle_death = death_handler # low hp is not death a.hp = 1 a.alive = True assert not a.check_death() assert not death_handler_called assert a.alive # 0 hp is death death_handler_called = False a.hp = 0 assert a.check_death() assert death_handler_called assert not a.alive # negative hp is death death_handler_called = False a.hp = -1 a.alive = True assert a.check_death() assert death_handler_called assert not a.alive # check that the death handler is only called once death_handler_called = False assert a.check_death() assert not death_handler_called assert not a.alive