Amazing-Python-Scripts

Форк
0
60 строк · 1.6 Кб
1
import unittest
2

3

4
class Game:
5
    def __init__(self):
6
        self.is_running = False
7
        self.score = 0
8

9
    def start(self):
10
        self.is_running = True
11
        return "Game started."
12

13
    def play(self, action):
14
        if self.is_running:
15
            if action == "move_forward":
16
                self.score += 10
17
            elif action == "attack":
18
                self.score += 20
19
            elif action == "use_item":
20
                self.score += 5
21
            return f"Performed action: {action}"
22
        else:
23
            return "Game is not running."
24

25
    def quit(self):
26
        self.is_running = False
27
        return "Game quit."
28

29

30
class TestGame(unittest.TestCase):
31
    @classmethod
32
    def setUpClass(cls):
33
        cls.game = Game()
34
        cls.game.start()
35

36
    def test_start(self):
37
        self.assertTrue(self.game.is_running)
38
        self.assertEqual(self.game.start(), "Game started.")
39

40
    def test_actions(self):
41
        actions = ["move_forward", "attack", "use_item"]
42
        for action in actions:
43
            with self.subTest(action=action):
44
                result = self.game.play(action)
45
                self.assertIn(action, result)
46
                self.assertGreaterEqual(self.game.score, 0)
47

48
    def test_quit(self):
49
        self.assertTrue(self.game.is_running)
50
        self.assertEqual(self.game.quit(), "Game quit.")
51
        self.assertFalse(self.game.is_running)
52

53
    def test_non_running_actions(self):
54
        self.game.quit()
55
        result = self.game.play("move_forward")
56
        self.assertEqual(result, "Game is not running.")
57

58

59
if __name__ == "__main__":
60
    unittest.main()
61

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.