Amazing-Python-Scripts
60 строк · 1.6 Кб
1import unittest2
3
4class Game:5def __init__(self):6self.is_running = False7self.score = 08
9def start(self):10self.is_running = True11return "Game started."12
13def play(self, action):14if self.is_running:15if action == "move_forward":16self.score += 1017elif action == "attack":18self.score += 2019elif action == "use_item":20self.score += 521return f"Performed action: {action}"22else:23return "Game is not running."24
25def quit(self):26self.is_running = False27return "Game quit."28
29
30class TestGame(unittest.TestCase):31@classmethod32def setUpClass(cls):33cls.game = Game()34cls.game.start()35
36def test_start(self):37self.assertTrue(self.game.is_running)38self.assertEqual(self.game.start(), "Game started.")39
40def test_actions(self):41actions = ["move_forward", "attack", "use_item"]42for action in actions:43with self.subTest(action=action):44result = self.game.play(action)45self.assertIn(action, result)46self.assertGreaterEqual(self.game.score, 0)47
48def test_quit(self):49self.assertTrue(self.game.is_running)50self.assertEqual(self.game.quit(), "Game quit.")51self.assertFalse(self.game.is_running)52
53def test_non_running_actions(self):54self.game.quit()55result = self.game.play("move_forward")56self.assertEqual(result, "Game is not running.")57
58
59if __name__ == "__main__":60unittest.main()61