コード例 #1
0
ファイル: test_hero.py プロジェクト: borko81/SU_OOP_2021
 def test_battle_helth_is_less_thjen_zero(self):
     hero_test_with_zero = Hero('ZeroName', 1, 0, 100)
     with self.assertRaises(ValueError) as ex:
         hero_test_with_zero.battle(self.user3)
     self.assertEqual(
         str(ex.exception),
         "Your health is lower than or equal to 0. You need to rest")
コード例 #2
0
ファイル: test_hero.py プロジェクト: borko81/SU_OOP_2021
class TestHero(unittest.TestCase):
    def setUp(self):
        self.hero = Hero('Hero1', 2, 100, 50)
        self.user2 = Hero('Hero1', 2, 100, 50)
        self.user3 = Hero('Hero2', 2, 100, 50)
        self.user4 = Hero('Lost', 2, 100, 40)

    def test_all_set_up(self):
        self.assertEqual(self.hero.username, 'Hero1')
        self.assertEqual(self.hero.level, 2)
        self.assertEqual(self.hero.health, 100)
        self.assertEqual(self.hero.damage, 50)

    def test_battle_with_same_name_raise_exception(self):
        with self.assertRaises(Exception) as ex:
            self.hero.battle(self.user2)
        self.assertEqual(str(ex.exception), "You cannot fight yourself")

    def test_battle_helth_is_less_thjen_zero(self):
        hero_test_with_zero = Hero('ZeroName', 1, 0, 100)
        with self.assertRaises(ValueError) as ex:
            hero_test_with_zero.battle(self.user3)
        self.assertEqual(
            str(ex.exception),
            "Your health is lower than or equal to 0. You need to rest")

    def test_battle_intruder_has_zero_health(self):
        hero_test_with_zero = Hero('ZeroName', 1, 10, 100)
        intruder_hero = Hero('ZeroName2', 1, 0, 100)
        with self.assertRaises(ValueError) as ex:
            hero_test_with_zero.battle((intruder_hero))
        self.assertEqual(
            str(ex.exception),
            f"You cannot fight {intruder_hero.username}. He needs to rest")

    def test_hero_and_intruder_has_less_then_zero(self):
        acquire = self.hero.battle(self.user3)
        self.assertEqual(acquire, 'Draw')

    def test_enemy_health_is_zero_after_battle(self):
        acquire = self.hero.battle(self.user4)
        self.assertEqual(acquire, 'You win')
        self.assertEqual(self.hero.level, 3)
        self.assertEqual(self.hero.health, 25)
        self.assertEqual(self.hero.damage, 55)

    def test_hero_health_is_zero_after_battle(self):
        acquire = self.user4.battle(self.hero)
        self.assertEqual(acquire, 'You lose')
        self.assertEqual(self.hero.level, 3)
        self.assertEqual(self.hero.health, 25)
        self.assertEqual(self.hero.damage, 55)

    def test_represent_method(self):
        acquire = f"Hero {self.hero.username}: {self.hero.level} lvl\n" \
               f"Health: {self.hero.health}\n" \
               f"Damage: {self.hero.damage}\n"

        self.assertEqual(str(self.hero), acquire)
コード例 #3
0
ファイル: test_hero.py プロジェクト: borko81/SU_OOP_2021
 def test_battle_intruder_has_zero_health(self):
     hero_test_with_zero = Hero('ZeroName', 1, 10, 100)
     intruder_hero = Hero('ZeroName2', 1, 0, 100)
     with self.assertRaises(ValueError) as ex:
         hero_test_with_zero.battle((intruder_hero))
     self.assertEqual(
         str(ex.exception),
         f"You cannot fight {intruder_hero.username}. He needs to rest")
コード例 #4
0
ファイル: test_hero.py プロジェクト: MiroVatov/Python-SoftUni
    def test_both_enemies_fight_with_the_same_name_raises_exception(self):
        self.hero = Hero('Miro', 100, 850, 5)
        self.enemy_hero = Hero('Prodigy', 90, 830, 5)
        self.enemy_hero.username = self.hero.username

        if check_if_both_names_are_the_same(self.hero.username, self.enemy_hero.username):
            with self.assertRaises(Exception):
                Hero.battle(self.hero, self.enemy_hero)
コード例 #5
0
class TestHero(unittest.TestCase):
    def setUp(self):
        self.hero = Hero("Wizard", 70, 6000.00, 1500.00)
        self.enemy = Hero("Barbarian", 80, 10000.00, 2000.00)
        self.mario = Hero("Mario", 1, 100.00, 100.00)

    def test_init(self):
        self.assertEqual(self.hero.username, "Wizard")
        self.assertEqual(self.hero.level, 70)
        self.assertEqual(self.hero.health, 6000.00)
        self.assertEqual(self.hero.damage, 1500.00)

    def test_str(self):
        self.assertEqual(
            self.hero.__str__(), f"Hero Wizard: {self.hero.level} lvl\n"
            f"Health: {self.hero.health}\nDamage: {self.hero.damage}\n")

    def test_fight_with_yourself(self):
        with self.assertRaises(Exception) as ex:
            self.hero.battle(self.hero)
        self.assertEqual("You cannot fight yourself", str(ex.exception))

    def test_hero_with_zero_health(self):
        self.hero.health = 0
        with self.assertRaises(Exception) as ex:
            self.hero.battle(self.enemy)
        self.assertEqual(
            "Your health is lower than or equal to 0. You need to rest",
            str(ex.exception))

    def test_enemy_with_zero_health(self):
        self.enemy.health = 0
        with self.assertRaises(Exception) as ex:
            self.hero.battle(self.enemy)
        self.assertEqual(
            f"You cannot fight {self.enemy.username}. He needs to rest",
            str(ex.exception))

    def test_fight_draw_game(self):
        self.hero.battle(self.enemy)
        self.assertEqual(self.hero.health, -154000.00)
        self.assertEqual(self.enemy.health, -95000.00)
        self.assertRaises(Exception, "Draw")

    def test_fight_for_win(self):
        fight = self.hero.battle(self.mario)
        self.assertEqual(self.hero.level, 71)
        self.assertEqual(self.hero.damage, 1505.00)
        self.assertEqual(self.hero.health, 5905.00)
        self.assertEqual(fight, "You win")

    def test_fight_for_loose(self):
        self.hero = Hero("Wizard", 70, 6000.00, 10.00)
        fight = self.hero.battle(self.enemy)
        self.assertEqual(self.enemy.level, 81)
        self.assertEqual(self.enemy.damage, 2005.00)
        self.assertEqual(self.enemy.health, 9305.00)
        self.assertEqual(fight, "You lose")
コード例 #6
0
ファイル: test_hero.py プロジェクト: MiroVatov/Python-SoftUni
 def test_if_enemy_health_is_above_zero_after_battle_and_hero_loses_the_battle(self):
     self.hero = Hero('Miro', 100, 850, 5)
     self.enemy_hero = Hero('Prodigy', 90, 830, 5)
     result = Hero.battle(self.hero, self.enemy_hero)
     self.assertEqual(self.enemy_hero.level, 91)
     self.assertEqual(self.enemy_hero.health, 335)
     self.assertEqual(self.enemy_hero.damage, 10)
     self.assertEqual(result, "You lose")
コード例 #7
0
ファイル: test_hero.py プロジェクト: MiroVatov/Python-SoftUni
 def test_if_enemy_hero_health_drops_to_zero_or_under_zero_and_we_win_the_battle(self):
     self.hero = Hero('Miro', 100, 850, 5)
     self.enemy_hero = Hero('Prodigy', 90, 330, 5)
     result = Hero.battle(self.hero, self.enemy_hero)
     self.assertEqual(self.hero.level, 101)
     self.assertEqual(self.hero.health, 405)
     self.assertEqual(self.hero.damage, 10)
     self.assertEqual(result, 'You win')
コード例 #8
0
class HeroTest(unittest.TestCase):

    def setUp(self) -> None:
        self.hero = Hero("Bobby", 25, 98, 2)

    def test_constructor(self):
        self.assertEqual("Bobby", self.hero.username)
        self.assertEqual(25, self.hero.level)
        self.assertEqual(98, self.hero.health)
        self.assertEqual(2, self.hero.damage)

    def test_enemy_hero_and_hero_are_equal(self):
        with self.assertRaises(Exception):
            enemy_hero = Hero("Bobby", 25, 98, 2)
            self.hero.battle(enemy_hero)

    def test_battle_hero_health_less_than_zro(self):
        enemy_hero = Hero("Borko", 25, 98, 2)
        self.hero.health = 0
        with self.assertRaises(Exception):
            self.hero.battle(enemy_hero)

    def test_battle_enemy_health_less_than_zro(self):
        enemy_hero = Hero("Borko", 25, 0, 2)
        with self.assertRaises(Exception):
            self.hero.battle(enemy_hero)

    def test_player_health_when_payer_loss(self):
        enemy_hero = Hero("Borko", 25, 75, 2)
        self.assertEqual("You lose", self.hero.battle(enemy_hero) )
        self.assertEqual(48, self.hero.health)
        self.assertEqual(30, enemy_hero.health)
        self.assertEqual(26, enemy_hero.level)
        self.assertEqual(7, enemy_hero.damage)

    def test_player_health_if_payer_win(self):
        enemy_hero = Hero("Borko", 25, 50, 2)
        self.assertEqual("You win", self.hero.battle(enemy_hero))
        self.assertEqual(53, self.hero.health)
        self.assertEqual(0, enemy_hero.health)
        self.assertEqual(26, self.hero.level)
        self.assertEqual(7, self.hero.damage)

    def test_if_hero_and_enemy_are_equal_to_zero(self):
        enemy_hero = Hero("Borko", 49, 50, 2)
        self.assertEqual("Draw", self.hero.battle(enemy_hero))

    def test_repr_hero(self):
        expect = f"Hero Bobby: 25 lvl\nHealth: 98\nDamage: 2\n"
        self.assertEqual(expect, self.hero.__str__())
コード例 #9
0
ファイル: test_hero.py プロジェクト: nkolew/Softuni-Python
class HeroTests(unittest.TestCase):
    def setUp(self) -> None:
        self.hero = Hero(username=DEFAULT_USERNAME,
                         level=DEFAULT_LEVEL,
                         health=DEFAULT_HEALTH,
                         damage=DEFAULT_DAMAGE)

    def test_hero_ok_init(self):
        self.assertEqual(DEFAULT_USERNAME, self.hero.username)
        self.assertEqual(DEFAULT_LEVEL, self.hero.level)
        self.assertEqual(DEFAULT_HEALTH, self.hero.health)
        self.assertEqual(DEFAULT_DAMAGE, self.hero.damage)

    def test_hero_battling_himself_raises(self):
        h = Hero('Test', 2, 50, 30)
        with self.assertRaises(Exception):
            self.hero.battle(h)

    def test_hero_battle_when_hero_health_negative_raises(self):
        self.hero.health = -5
        h = Hero('Test1', 2, 50, 30)
        with self.assertRaises(ValueError):
            self.hero.battle(h)

    def test_hero_battle_when_hero_health_zero_raises(self):
        self.hero.health = 0
        h = Hero('Test1', 2, 50, 30)
        with self.assertRaises(ValueError):
            self.hero.battle(h)

    def test_hero_battle_when_enemy_health_negative_raises(self):
        h = Hero('Test1', 2, 50, 30)
        h.health = -5
        with self.assertRaises(ValueError):
            self.hero.battle(h)

    def test_hero_battle_when_enemy_health_zero_raises(self):
        h = Hero('Test1', 2, 50, 30)
        h.health = 0
        with self.assertRaises(ValueError):
            self.hero.battle(h)

    def test_hero_battle_damage(self):
        h = Hero('Test1', 2, 50, 10)
        enemy_health = 45
        self.hero.battle(h)
        self.assertEqual(enemy_health, h.health)

    def test_hero_battle_hero_win(self):
        h = Hero('Test1', 2, 10, 2)
        rv = self.hero.battle(h)
        self.assertTrue(h.health <= 0)
        self.assertEqual('You win', rv)
        self.assertEqual(2, self.hero.level)
        self.assertEqual(15, self.hero.damage)
        self.assertEqual(101, self.hero.health)

    def test_hero_battle_enemy_win(self):
        h = Hero('Test1', 23, 100, 200)
        rv = self.hero.battle(h)
        self.assertTrue(self.hero.health <= 0)
        self.assertEqual('You lose', rv)
        self.assertEqual(24, h.level)
        self.assertEqual(205, h.damage)
        self.assertEqual(95, h.health)

    def test_hero_battle_draw(self):
        h = Hero('Test1', 1, 2, 200)
        rv = self.hero.battle(h)
        self.assertTrue(self.hero.health <= 0)
        self.assertTrue(h.health <= 0)
        self.assertEqual('Draw', rv)

    def test_hero_str_representation(self):
        self.assertEqual(HERO_REPRESENTATION, str(self.hero))
コード例 #10
0
ファイル: test_hero.py プロジェクト: MiroVatov/Python-SoftUni
 def test_enemy_hero_health_is_zero_or_below_zero_raises_exception(self):
     self.hero = Hero('Miro', 100, 850, 5)
     self.enemy_hero = Hero('Prodigy', 90, 830, 5)
     self.enemy_hero.health = 0
     with self.assertRaises(ValueError):`
         Hero.battle(self.hero, self.enemy_hero)
コード例 #11
0
ファイル: test_hero.py プロジェクト: MiroVatov/Python-SoftUni
 def test_if_both_enemies_health_is_zero_or_blow_zero_finish_draw(self):
     self.hero = Hero('Miro', 100, 350, 5)
     self.enemy_hero = Hero('Prodigy', 90, 330, 5)
     result = Hero.battle(self.hero, self.enemy_hero)
     self.assertEqual(result, 'Draw')
コード例 #12
0
ファイル: test_hero.py プロジェクト: PetkoAndreev/Python-OOP
class HeroTest(unittest.TestCase):
    def setUp(self):
        self.hero = Hero('Blue Mage', 10, 1000.1, 100.2)

    def test_hero_init_method_attributes(self):
        self.assertEqual('Blue Mage', self.hero.username)
        self.assertEqual(10, self.hero.level)
        self.assertEqual(1000.1, self.hero.health)
        self.assertEqual(100.2, self.hero.damage)

    def test_hero_str_method(self):
        expected_result = f"Hero Blue Mage: 10 lvl\nHealth: 1000.1\nDamage: 100.2\n"
        actual_result = self.hero.__str__()
        self.assertEqual(expected_result, actual_result)

    def test_hero_battle_when_enemy_hero_name_same_as_hero_name(self):
        enemy_hero = self.hero
        with self.assertRaises(Exception) as context:
            self.hero.battle(enemy_hero)
        self.assertEqual("You cannot fight yourself", str(context.exception))

    def test_hero_battle_when_hero_health_is_0(self):
        enemy_hero = Hero('Hero', 5, 100, 3)
        self.hero.health = 0
        with self.assertRaises(ValueError) as context:
            self.hero.battle(enemy_hero)
        self.assertEqual(
            "Your health is lower than or equal to 0. You need to rest",
            str(context.exception))

    def test_hero_battle_when_hero_health_is_less_than_0(self):
        enemy_hero = Hero('Hero', 5, 100, 3)
        self.hero.health = -5
        with self.assertRaises(ValueError) as context:
            self.hero.battle(enemy_hero)
        self.assertEqual(
            "Your health is lower than or equal to 0. You need to rest",
            str(context.exception))

    def test_hero_battle_when_enemy_hero_health_is_0(self):
        enemy_hero = Hero('Knight', 5, 0, 3)
        with self.assertRaises(ValueError) as context:
            self.hero.battle(enemy_hero)
        self.assertEqual("You cannot fight Knight. He needs to rest",
                         str(context.exception))

    def test_hero_battle_when_enemy_hero_health_is_less_than_0(self):
        enemy_hero = Hero('Knight', 5, -100, 3)
        with self.assertRaises(ValueError) as context:
            self.hero.battle(enemy_hero)
        self.assertEqual("You cannot fight Knight. He needs to rest",
                         str(context.exception))

    def test_hero_battle_when_battle_is_draw(self):
        enemy_hero = Hero('Knight', 10, 1000.1, 100.2)
        expected_result = "Draw"
        actual_result = self.hero.battle(enemy_hero)
        self.assertEqual(expected_result, actual_result)

    def test_hero_battle_when_hero_wins(self):
        enemy_hero = Hero('Knight', 5, 100, 3)
        expected_result = "You win"
        actual_result = self.hero.battle(enemy_hero)
        self.assertEqual(expected_result, actual_result)
        self.assertEqual(11, self.hero.level)
        self.assertEqual(990.1, self.hero.health)
        self.assertEqual(105.2, self.hero.damage)

    def test_hero_battle_when_enemy_hero_wins(self):
        enemy_hero = Hero('Knight', 11, 10000, 110)
        expected_result = "You lose"
        actual_result = self.hero.battle(enemy_hero)
        self.assertEqual(expected_result, actual_result)
        self.assertEqual(12, enemy_hero.level)
        self.assertEqual(9003, enemy_hero.health)
        self.assertEqual(115, enemy_hero.damage)
コード例 #13
0
ファイル: test_hero.py プロジェクト: Tarantulata/Portfolio
class TestHero(TestCase):
    def setUp(self):
        self.hero = Hero("Test", 10, 100, 20)
        self.enemy = Hero("EnemyTest", 5, 100, 10)

    def test_initialized_all_attributes(self):
        self.assertEqual("Test", self.hero.username)
        self.assertEqual(10, self.hero.level)
        self.assertEqual(100, self.hero.health)
        self.assertEqual(20, self.hero.damage)

    def test_battle_with_yourself_raises(self):
        with self.assertRaises(Exception) as ex:
            self.hero.battle(self.hero)

        self.assertEqual("You cannot fight yourself", str(ex.exception))

    def test_battle_with_zero_health(self):
        self.hero.health = 0
        with self.assertRaises(ValueError) as ex:
            self.hero.battle(self.enemy)

        self.assertEqual(
            "Your health is lower than or equal to 0. You need to rest",
            str(ex.exception))

    def test_battle_if_enemy_hero_with_zero_health(self):
        self.enemy.health = 0
        with self.assertRaises(ValueError) as ex:
            self.hero.battle(self.enemy)

        self.assertEqual(
            f"You cannot fight {self.enemy.username}. He needs to rest",
            str(ex.exception))

    def test_battle_draw(self):
        self.hero.health = 50
        result = self.hero.battle(self.enemy)

        self.assertEqual("Draw", result)

    def test_battle_win(self):
        result = self.hero.battle(self.enemy)

        self.assertEqual(55, self.hero.health)
        self.assertEqual(11, self.hero.level)
        self.assertEqual(25, self.hero.damage)
        self.assertEqual("You win", result)

    def test_battle_lose(self):
        self.hero.damage = 1
        self.hero.health = 1
        result = self.hero.battle(self.enemy)

        self.assertEqual(95, self.enemy.health)
        self.assertEqual(6, self.enemy.level)
        self.assertEqual(15, self.enemy.damage)
        self.assertEqual("You lose", result)

    def test_str_representation(self):
        self.assertEqual(
            f"Hero Test: 10 lvl\n"
            f"Health: 100\n"
            f"Damage: 20\n", str(self.hero))
コード例 #14
0
class HeroTests(unittest.TestCase):
    def setUp(self):
        self.hero = Hero(username='******', level=80, health=12.5, damage=1.0)
        self.enemy = Hero(username='******', level=10, health=2, damage=1.0)

    def test_hero_if_all_attributes_are_set(self):
        self.assertEqual('Bogdan', self.hero.username)
        self.assertEqual(80, self.hero.level)
        self.assertEqual(12.5, self.hero.health)
        self.assertEqual(1.0, self.hero.damage)

    def test__hero__battle_if_name_equals_username_raise_exception(self):
        with self.assertRaises(Exception) as ex:
            self.hero.battle(self.hero)
        self.assertEqual("You cannot fight yourself", str(ex.exception))

    def test_hero__battle_if_hero_health_is_equal_or_less_than_zero_raise_error(
            self):
        self.hero.health = 0
        with self.assertRaises(Exception) as ex:
            self.hero.battle(self.enemy)
        self.assertEqual(
            "Your health is lower than or equal to 0. You need to rest",
            str(ex.exception))

    def test_hero__battle_if_enemy_health_is_equal_or_less_than_zero_raise_error(
            self):
        self.enemy.health = -1
        with self.assertRaises(Exception) as ex:
            self.hero.battle(self.enemy)
        self.assertEqual("You cannot fight Vicky. He needs to rest",
                         str(ex.exception))

    def test_hero_battle__if_both_players_health_is_equal_or_less_than_zero_draw(
            self):
        self.hero.health = 0.5
        self.enemy.health = 0.5
        result = self.hero.battle(self.enemy)
        expected_result = 'Draw'
        self.assertEqual(expected_result, result)

    def test_hero_battle__if_enemy_health_equal_or_less_than_zero_hero_wins(
            self):
        self.hero.health = 100.5
        result = self.hero.battle(self.enemy)
        expected_result = 'You win'
        self.assertEqual(expected_result, result)

    def test_hero_battle__if_hero_health_equal_or_less_than_zero_hero_loose(
            self):
        self.hero.health = 12.5
        self.hero.damage = 0
        result = self.hero.battle(self.enemy)
        expected_result = 'You lose'
        self.assertEqual(expected_result, result)
        self.assertEqual(11, self.enemy.level)
        self.assertEqual(7, self.enemy.health)
        self.assertEqual(6.0, self.enemy.damage)

    def test_hero_printing_the_summary_of_the_class(self):
        self.hero.battle(self.enemy)
        result = self.hero.__str__()
        expected_result = f"Hero Bogdan: 81 lvl\n" \
                          f"Health: 7.5\n" \
                          f"Damage: 6.0\n"
        self.assertEqual(expected_result, result)
コード例 #15
0
ファイル: test_hero.py プロジェクト: beats4myvan/py_oop
class TestHero(TestCase):
    username = '******'
    health = 100
    damage = 5
    level = 5

    def setUp(self):
        self.hero = Hero(self.username, self.level, self.health, self.damage)

    def test_hero_init_method_attributes(self):
        self.assertEqual('MadMax', self.hero.username)
        self.assertEqual(5, self.hero.level)
        self.assertEqual(100, self.hero.health)
        self.assertEqual(5, self.hero.damage)

    def test_battle_exception_test(self):
        with self.assertRaises(Exception) as context:
            enemy_hero = self.hero
            self.hero.battle(enemy_hero)

        self.assertEqual('You cannot fight yourself', str(context.exception))

    def test_battle_exception_if_hero_lower_health(self):
        with self.assertRaises(Exception) as context:
            self.hero.health = 0
            enemy_hero = Hero('Hero', 5, 100, 3)
            self.hero.battle(enemy_hero)

        self.assertEqual(
            'Your health is lower than or equal to 0. You need to rest',
            str(context.exception))

    def test_battle_exception_if_enemy_hero_has_lower_health(self):
        with self.assertRaises(Exception) as context:
            enemy_hero = Hero('Enemy Hero Pesho', 5, 0, 3)
            self.hero.battle(enemy_hero)

        self.assertEqual(
            f'You cannot fight {enemy_hero.username}. He needs to rest',
            str(context.exception))

    def test_battle_results(self):
        enemy_hero = Hero('Enemy Hero Pesho', 5, 100, 3)
        self.hero.battle(enemy_hero)
        self.assertEqual(self.hero.health, 85)
        self.assertEqual(enemy_hero.health, 80)

    def test_you_win_if__healt_0(self):
        enemy_hero = Hero('Enemy Hero Pesho', 5, 10, 3)
        expected_message = 'You win'
        actual_message = self.hero.battle(enemy_hero)
        self.assertEqual(expected_message, actual_message)

    def test_you_loose_if_self_health_under_0(self):
        enemy_hero = Hero('Enemy Hero Pesho', 50, 110, 30)
        expected_message = 'You lose'
        actual_message = self.hero.battle(enemy_hero)
        self.assertEqual(expected_message, actual_message)

    def test__rerp_method(self):
        expected = f"Hero MadMax: 5 lvl\n" \
                   f"Health: 100\n" \
                   f"Damage: 5\n"
        actual_result = self.hero.__str__()

        self.assertEqual(expected, actual_result)

    def test_if_health_of_both_hero_is_0(self):
        enemy_hero = Hero('Enemy Hero Pesho', 50, 1, 30)
        expected_result = 'Draw'
        result = self.hero.battle(enemy_hero)
        self.assertEqual(result, expected_result)
コード例 #16
0
ファイル: test_hero.py プロジェクト: zhyordanova/Python-OOP
class TestHero(TestCase):

    def setUp(self):
        self.main_hero = Hero("Main hero", 5, 100, 10)
        self.enemy_hero = Hero("Enemy hero", 10, 80, 20)

    def test_check_instance_attr_are_set(self):
        self.assertEqual('Main hero', self.main_hero.username)
        self.assertEqual(5, self.main_hero.level)
        self.assertEqual(100, self.main_hero.health)
        self.assertEqual(10, self.main_hero.damage)

    def test_battle__when_fight_yourself(self):
        hero_self = Hero("Main hero", 6, 80, 10)
        with self.assertRaises(Exception) as ex:
            self.main_hero.battle(hero_self)
        self.assertEqual('You cannot fight yourself', str(ex.exception))

    def test_battle__when_main_hero_health_less_than_0__expect_value_error(self):
        self.main_hero.health = -1
        # self.assertEqual(-1, self.main_hero.health)
        with self.assertRaises(ValueError) as ex:
            self.main_hero.battle(self.enemy_hero)
        self.assertEqual('Your health is lower than or equal to 0. You need to rest', str(ex.exception))

    def test_battle__when_main_hero_health_equal_to_0__expect_value_error(self):
        self.main_hero.health = 0
        # self.assertEqual(0, self.main_hero.health)
        with self.assertRaises(ValueError) as ex:
            self.main_hero.battle(self.enemy_hero)
        self.assertEqual('Your health is lower than or equal to 0. You need to rest', str(ex.exception))

    def test_battle__when_enemy_hero_health_less_than_0__expect_value_error(self):
        self.enemy_hero.health = -1
        # self.assertEqual(-1, self.enemy_hero.health)
        with self.assertRaises(ValueError) as ex:
            self.main_hero.battle(self.enemy_hero)
        self.assertEqual('You cannot fight Enemy hero. He needs to rest', str(ex.exception))

    def test_battle__when_enemy_hero_health_equal_to_0__expect_value_error(self):
        self.enemy_hero.health = 0
        # self.assertEqual(0, self.enemy_hero.health)
        with self.assertRaises(ValueError) as ex:
            self.main_hero.battle(self.enemy_hero)
        self.assertEqual('You cannot fight Enemy hero. He needs to rest', str(ex.exception))

    def test_draw_case__when_main_health_is_less_than_0_and_enemy_health_equal_to_0__expect_draw(self):
        self.enemy_hero.health = 50
        result = self.main_hero.battle(self.enemy_hero)
        self.assertEqual('Draw', result)

    def test_draw_case__when_main_health_equal_to_0_and_enemy_health_less_than_0__expect_draw(self):
        self.main_hero.health = 200
        self.enemy_hero.health = 30
        result = self.main_hero.battle(self.enemy_hero)
        self.assertEqual('Draw', result)

    def test_draw_case__when_main_health_equal_to_0_and_enemy_health_equal_to_0__expect_draw(self):
        self.main_hero.health = 200
        self.enemy_hero.health = 50
        result = self.main_hero.battle(self.enemy_hero)
        self.assertEqual('Draw', result)

    def test_draw_case__when_main_health_less_than_0_and_enemy_health_less_than_0__expect_draw(self):
        self.enemy_hero.health = 30
        result = self.main_hero.battle(self.enemy_hero)
        self.assertEqual('Draw', result)

    def test_main_hero_win(self):
        self.main_hero.damage = 200
        self.main_hero.health = 300

        result = self.main_hero.battle(self.enemy_hero)
        self.assertEqual('You win', result)
        self.assertEqual(205, self.main_hero.damage)
        self.assertEqual(6, self.main_hero.level)
        self.assertEqual(105, self.main_hero.health)

    def test_main_hero_lose(self):
        result = self.main_hero.battle(self.enemy_hero)
        self.assertEqual('You lose', result)
        self.assertEqual(25, self.enemy_hero.damage)
        self.assertEqual(11, self.enemy_hero.level)
        self.assertEqual(35, self.enemy_hero.health)

        self.assertTrue(self.main_hero.health < 0)

    def test_str_representation(self):
        self.assertEqual('Hero Main hero: 5 lvl\nHealth: 100\nDamage: 10\n', str(self.main_hero))
コード例 #17
0
class TestHero(TestCase):
    def setUp(self):
        self.hero1 = Hero("Tamaraw", 5, 100, 10)

    def test_init_attr(self):
        self.assertEqual("Tamaraw", self.hero1.username)
        self.assertEqual(5, self.hero1.level)
        self.assertEqual(100, self.hero1.health)
        self.assertEqual(10, self.hero1.damage)

    def test_battle_with_hero_with_same_name_raises_exception(self):
        hero2 = Hero("Tamaraw", 5, 100, 10)
        with self.assertRaises(Exception) as ex:
            self.hero1.battle(hero2)
        self.assertEqual("You cannot fight yourself", str(ex.exception))

    def test_battle_health_below_zero_raises_value_error(self):
        self.hero1.health = -50
        hero2 = Hero("Manol", 5, 100, 10)
        with self.assertRaises(ValueError) as ex:
            self.hero1.battle(hero2)
        self.assertEqual(
            "Your health is lower than or equal to 0. You need to rest",
            str(ex.exception))

    def test_battle_health_equal_to_zero_raises_value_error(self):
        self.hero1.health = 0
        hero2 = Hero("Manol", 5, 100, 10)
        with self.assertRaises(ValueError) as ex:
            self.hero1.battle(hero2)
        self.assertEqual(
            "Your health is lower than or equal to 0. You need to rest",
            str(ex.exception))

    def test_battle_enemy_health_below_zero_raises_value_error(self):
        hero2 = Hero("Manol", 10, -50, 10)
        with self.assertRaises(ValueError) as ex:
            self.hero1.battle(hero2)
        self.assertEqual("You cannot fight Manol. He needs to rest",
                         str(ex.exception))

    def test_battle_enemy_health_equal_to_zero_raises_value_error(self):
        hero2 = Hero("Manol", 10, 0, 10)
        with self.assertRaises(ValueError) as ex:
            self.hero1.battle(hero2)
        self.assertEqual("You cannot fight Manol. He needs to rest",
                         str(ex.exception))

    def test_battle_success_both_healths_above_zero(self):
        hero2 = Hero("Manol", 5, 100, 5)
        self.hero1.battle(hero2)
        self.assertEqual(75, self.hero1.health)
        self.assertEqual(55, hero2.health)

    def test_battle_both_healths_are_zero_draw(self):
        self.hero1.level = 10
        hero2 = Hero("Manol", 10, 100, 10)
        self.assertEqual("Draw", self.hero1.battle(hero2))

    def test_battle_both_healths_are_below_zero_draw(self):
        self.hero1.level = 15
        hero2 = Hero("Manol", 15, 100, 10)
        self.assertEqual("Draw", self.hero1.battle(hero2))

    def test_battle_only_enemy_health_is_below_zero(self):
        hero2 = Hero("Manol", 5, 40, 10)
        result = self.hero1.battle(hero2)
        self.assertEqual(6, self.hero1.level)
        self.assertEqual(55, self.hero1.health)
        self.assertEqual(15, self.hero1.damage)
        self.assertEqual("You win", result)

    def test_battle_only_your_health_is_below_zero(self):
        self.hero1.health = 40
        hero2 = Hero("Manol", 5, 100, 10)
        result = self.hero1.battle(hero2)
        self.assertEqual(6, hero2.level)
        self.assertEqual(55, hero2.health)
        self.assertEqual(15, hero2.damage)
        self.assertEqual("You lose", result)

    def test_battle_only_enemy_health_is_zero(self):
        hero2 = Hero("Manol", 5, 50, 10)
        result = self.hero1.battle(hero2)
        self.assertEqual(6, self.hero1.level)
        self.assertEqual(55, self.hero1.health)
        self.assertEqual(15, self.hero1.damage)
        self.assertEqual("You win", result)

    def test_battle_only_your_health_is_zero(self):
        self.hero1.health = 50
        hero2 = Hero("Manol", 5, 200, 10)
        result = self.hero1.battle(hero2)
        self.assertEqual(6, hero2.level)
        self.assertEqual(155, hero2.health)
        self.assertEqual(15, hero2.damage)
        self.assertEqual("You lose", result)

    def test_str_hero(self):
        result = str(self.hero1)
        self.assertEqual("Hero Tamaraw: 5 lvl\nHealth: 100\nDamage: 10\n",
                         result)
コード例 #18
0
class TestHero(unittest.TestCase):
    def setUp(self):
        self.hero = Hero('a', 1, 100.0, 10.0)
        self.enemy = Hero('b', 1, 100.0, 10.0)

    def test_constructor(self):
        self.assertEqual(self.hero.username, 'a')
        self.assertEqual(self.hero.health, 100.0)
        self.assertEqual(self.hero.level, 1)
        self.assertEqual(self.hero.damage, 10.0)

    def test_battle_cannot_fight_yourself(self):
        self.enemy.username = '******'
        with self.assertRaises(Exception) as exc:
            self.hero.battle(self.enemy)
        self.assertEqual('You cannot fight yourself', str(exc.exception))

    def test_battle_health_is_too_low_need_to_rest_below_0(self):
        self.hero.health = 0
        with self.assertRaises(Exception) as exc:
            self.hero.battle(self.enemy)
        self.assertEqual(
            'Your health is lower than or equal to 0. You need to rest',
            str(exc.exception))
        self.hero.health = -10
        with self.assertRaises(Exception) as exc:
            self.hero.battle(self.enemy)
        self.assertEqual(
            'Your health is lower than or equal to 0. You need to rest',
            str(exc.exception))

    def test_battle_enemy_hero_below_zero_cannot_fight(self):
        self.enemy.health = 0
        with self.assertRaises(ValueError) as exc:
            self.hero.battle(self.enemy)
        self.assertEqual(
            f"You cannot fight {self.enemy.username}. He needs to rest",
            str(exc.exception))
        self.enemy.health = -10
        with self.assertRaises(ValueError) as exc:
            self.hero.battle(self.enemy)
        self.assertEqual(
            f"You cannot fight {self.enemy.username}. He needs to rest",
            str(exc.exception))

    def test_battle_no_exceptions_draw(self):
        self.hero.health = 10.0
        self.enemy.health = 10.0
        self.assertEqual('Draw', self.hero.battle(self.enemy))

    def test_battle_no_exception_win(self):
        self.enemy.health = 1
        self.assertEqual('You win', self.hero.battle(self.enemy))
        self.assertEqual(2, self.hero.level)
        self.assertEqual(95.0, self.hero.health)
        self.assertEqual(15.0, self.hero.damage)

    def test_battle_no_exception_loss(self):
        self.hero.health = 1
        self.assertEqual('You lose', self.hero.battle(self.enemy))
        self.assertEqual(2, self.enemy.level)
        self.assertEqual(95.0, self.enemy.health)
        self.assertEqual(15.0, self.enemy.damage)

    def test__str__returns_correct(self):
        expected = f"Hero {self.hero.username}: {self.hero.level} lvl\n" \
               f"Health: {self.hero.health}\n" \
               f"Damage: {self.hero.damage}\n"
        self.assertEqual(expected, str(self.hero))
コード例 #19
0
class HeroTests(unittest.TestCase):
    username = "******"
    level = 1
    health = 100.0
    damage = 10.0

    def setUp(self):
        self.h = Hero(self.username, self.level, self.health, self.damage)
        self.eh = Hero(self.username, self.level, self.health, self.damage)

    def test_h_correct_init(self):
        self.assertEqual(self.username, self.h.username)
        self.assertEqual(self.level, self.h.level)
        self.assertEqual(self.health, self.h.health)
        self.assertEqual(self.damage, self.h.damage)

    def test_eh_correct_init(self):
        self.assertEqual(self.username, self.eh.username)
        self.assertEqual(self.level, self.eh.level)
        self.assertEqual(self.health, self.eh.health)
        self.assertEqual(self.damage, self.eh.damage)

    def test_battle_when_username_and_enemy_username_is_same_shoud_raise_exception(
        self, ):
        with self.assertRaises(Exception) as contex:
            self.h.battle(self.eh)
        self.assertEqual("You cannot fight yourself", str(contex.exception))

    def test_battle_when_self_self_health_is_equal_to_zero_should_raise_value_error(
        self, ):
        self.eh.username = "******"
        self.h.health = 0
        with self.assertRaises(ValueError) as contex:
            self.h.battle(self.eh)

        self.assertEqual(
            f"Your health is lower than or equal to 0. You need to rest",
            str(contex.exception),
        )

    def test_battle_when_self_self_health_is_negative_shoud_raise_value_error(
            self):
        self.eh.username = "******"
        self.h.health = -1
        with self.assertRaises(ValueError) as contex:
            self.h.battle(self.eh)

        self.assertEqual(
            f"Your health is lower than or equal to 0. You need to rest",
            str(contex.exception),
        )

    def test_battle_when_self_enemy_health_is_equal_to_zero_should_raise_value_error(
        self, ):
        self.eh.username = "******"
        self.eh.health = 0
        with self.assertRaises(ValueError) as contex:
            self.h.battle(self.eh)

        self.assertEqual(
            f"You cannot fight {self.eh.username}. He needs to rest",
            str(contex.exception),
        )

    def test_battle_when_self_enemy_health_is_negative_shoud_raise_value_error(
            self):
        self.eh.username = "******"
        self.eh.health = -1
        with self.assertRaises(ValueError) as contex:
            self.h.battle(self.eh)

        self.assertEqual(
            f"You cannot fight {self.eh.username}. He needs to rest",
            str(contex.exception),
        )

    def test_battle_if_self_and_enemy_health_below_and_equal_zero_retrun_draw(
            self):
        self.eh.username = "******"
        self.h.damage = 100
        self.eh.damage = 100
        self.assertEqual("Draw", self.h.battle(self.eh))

    def test_battle_if_enemy_health_below_and_equal_zero_retrun_you_win(self):
        self.eh.username = "******"
        self.h.damage = 100
        self.eh.damage = 0
        self.assertEqual("You win", self.h.battle(self.eh))
        self.assertEqual(2, self.h.level)
        self.assertEqual(105.0, self.h.health)
        self.assertEqual(105, self.h.damage)

    def test_battle_if_self_health_below_and_equal_zero_retrun_you_win(self):
        self.eh.username = "******"
        self.h.damage = 0
        self.eh.damage = 100
        self.assertEqual("You lose", self.h.battle(self.eh))
        self.assertEqual(2, self.eh.level)
        self.assertEqual(105.0, self.eh.health)
        self.assertEqual(105, self.eh.damage)

    def test__str__representation(self):
        expected = (f"Hero {self.h.username}: {self.h.level} lvl\n"
                    f"Health: {self.h.health}\n"
                    f"Damage: {self.h.damage}\n")
        self.assertEqual(expected, str(self.h))
コード例 #20
0
class TestHero(unittest.TestCase):
    def setUp(self):
        self.my_instance_hero = Hero('username', 1, 100, 20)
        self.another_hero = Hero('name', 10, 20, 30)

    def test__init__hero(self):
        self.assertEqual(self.my_instance_hero.username, 'username')
        self.assertEqual(self.my_instance_hero.level, 1)
        self.assertEqual(self.my_instance_hero.damage, 20)
        self.assertEqual(self.my_instance_hero.health, 100)

    """
    test battle function cases
    """

    def test_both_objects_with_same_name__expected_exception(self):
        self.my_instance_hero.username = '******'
        with self.assertRaises(Exception) as ex:
            self.my_instance_hero.battle(self.another_hero)
        self.assertEqual("You cannot fight yourself", str(ex.exception))

    def test_instance_hero_health_less_than__zero__expected_value_error(self):
        with self.assertRaises(ValueError) as ex:
            self.my_instance_hero.health = -5
            self.my_instance_hero.battle(self.another_hero)
        self.assertEqual('Your health is lower than or equal to 0. You need to rest', str(ex.exception))
        self.assertEqual(self.my_instance_hero.health, -5)

    def test_instance_hero_health_equal_to_zero__expected_value_error(self):
        with self.assertRaises(ValueError) as ex:
            self.my_instance_hero.health = 0
            self.my_instance_hero.battle(self.another_hero)
        self.assertEqual('Your health is lower than or equal to 0. You need to rest', str(ex.exception))
        self.assertEqual(self.my_instance_hero.health, 0)

    def test_another_hero_health_equal_to_zero_expected_value_error(self):
        with self.assertRaises(ValueError) as ex:
            self.another_hero.health = -5
            self.my_instance_hero.battle(self.another_hero)
        self.assertEqual(f"You cannot fight name. He needs to rest", str(ex.exception))
        self.assertEqual(self.another_hero.health, -5)

    def test_another_hero_object_health_less_than_zero__expected_value_error(self):
        with self.assertRaises(ValueError) as ex:
            self.another_hero.health = 0
            self.my_instance_hero.battle(self.another_hero)
        self.assertEqual(f"You cannot fight name. He needs to rest", str(ex.exception))
        self.assertEqual(self.another_hero.health, 0)

    def test_both_heroes_under_or_equal_to_zero_expected_draw(self):
        result = self.my_instance_hero.battle(self.another_hero)
        self.assertEqual(result, 'Draw')
        self.assertTrue(self.my_instance_hero.health <= 0 and self.another_hero.health <= 0)

    def test_instance_hero_win(self):
        self.another_hero.damage = 1
        result = self.my_instance_hero.battle(self.another_hero)
        self.assertEqual(result, 'You win')
        self.assertEqual(self.my_instance_hero.health, 95)
        self.assertEqual(self.my_instance_hero.damage, 25)
        self.assertEqual(self.my_instance_hero.level, 2)

    def test_instance_hero_lose(self):
        self.another_hero.health = 1000
        result = self.my_instance_hero.battle(self.another_hero)
        self.assertEqual(result, 'You lose')
        self.assertEqual(self.another_hero.health, 985)
        self.assertEqual(self.another_hero.damage, 35)
        self.assertEqual(self.another_hero.level, 11)

    def test_str_method(self):
        massive = self.my_instance_hero.__str__().split('\n')
        self.assertEqual(massive[0], f"Hero username: 1 lvl")
        self.assertEqual(massive[1], f"Health: 100")
        self.assertEqual(massive[2], f"Damage: 20")
コード例 #21
0
class HeroTests(unittest.TestCase):
    def setUp(self) -> None:
        self.hero = Hero("TestName", 3, 100, 3)
        self.enemy = Hero("Enemy", 3, 100, 3)

    def test_check_attrs_are_set(self):
        self.assertEqual("TestName", self.hero.username)
        self.assertEqual(3, self.hero.level)
        self.assertEqual(100, self.hero.health)
        self.assertEqual(3, self.hero.damage)

# before_battle_tests

    def test_hero_enemy_name__when_enemy_name_is_same_as_hero_name_expect_exception(
            self):
        enemy = Hero("TestName", 99, 100, 999)
        with self.assertRaises(Exception) as context:
            self.hero.battle(enemy)
        expected = "You cannot fight yourself"
        self.assertEqual(expected, str(context.exception))

    def test_hero_health_in_battle__if_zero__expect_ValueError(self):
        enemy = Hero("Enemy", 99, 100, 999)
        self.hero.health = 0
        with self.assertRaises(ValueError) as context:
            self.hero.battle(enemy)
        expected = "Your health is lower than or equal to 0. You need to rest"
        self.assertEqual(expected, str(context.exception))

    def test_hero_health_in_battle__if_below__expect_ValueError(self):
        enemy = Hero("Enemy", 99, 100, 999)
        self.hero.health = -1
        with self.assertRaises(ValueError) as context:
            self.hero.battle(enemy)
        expected = "Your health is lower than or equal to 0. You need to rest"
        self.assertEqual(expected, str(context.exception))

    def test_hero_enemy_health_in_battle__if_zero__expect_ValueError(self):
        enemy = Hero("Enemy", 99, 0, 999)
        with self.assertRaises(ValueError) as context:
            self.hero.battle(enemy)
        expected = f"You cannot fight Enemy. He needs to rest"
        self.assertEqual(expected, str(context.exception))

    def test_hero_enemy_health_in_battle__if_below__expect_ValueError(self):
        enemy = Hero("Enemy", 99, -1, 999)
        with self.assertRaises(ValueError) as context:
            self.hero.battle(enemy)
        expected = f"You cannot fight Enemy. He needs to rest"
        self.assertEqual(expected, str(context.exception))

### damage_calculation_tests

# def test_hero_inflicting_damage_upon_enemy(self):
#     enemy = Hero("Enemy", 99, 100, 999)
#     self.hero.battle(enemy)
#     expected_damage = 9
#     actual = self.hero.damage * self.hero.level
#     self.assertEqual(expected_damage, actual)
#
# def test_hero_enemy_inflicting_damage_upon_hero(self):
#     enemy = Hero("Enemy", 3, 100, 3)
#     self.hero.battle(enemy)
#     expected_damage = 32
#     actual = enemy.damage * enemy.level
#     self.assertEqual(expected_damage, actual)

## damage_inflict_tests

    def test_hero_own_health_under_enemy_damage(self):
        enemy = Hero("Enemy", 3, 100, 3)
        self.hero.battle(enemy)
        expected_health = 91
        self.assertEqual(expected_health, self.hero.health)

    def test_hero_enemy_health_under_hero_damage(self):
        enemy = Hero("Enemy", 2, 100, 2)
        self.hero.battle(enemy)
        expected = 96
        actual = enemy.health
        self.assertEqual(expected, actual)

## draw_tests

    def test_hero_health__when_hero_health_goes_below_zero_after_battle__return_msg(
            self):
        enemy = Hero("Enemy", 2, 2, 2)
        self.hero.health = 2
        expected_msg = "Draw"
        actual_msg = f"{self.hero.battle(enemy)}"
        self.assertEqual(expected_msg, actual_msg)

    def test_hero_health__when_hero_health_goes_to_zero_after_battle__return_msg(
            self):
        enemy = Hero("Enemy", 2, 9, 2)
        self.hero.health = 4
        expected_msg = "Draw"
        actual_msg = f"{self.hero.battle(enemy)}"
        self.assertEqual(expected_msg, actual_msg)

##main_hero_win_tests

    def test_hero_battle__when_enemy_health_drop_below_zero_after_battle__return_msg(
            self):
        enemy = Hero("Enemy", 2, 2, 2)
        expected_msg = "You win"
        actual_msg = f"{self.hero.battle(enemy)}"
        self.assertEqual(expected_msg, actual_msg)

    def test_hero_battle__when_enemy_health_drop_to_zero_after_battle__return_msg(
            self):
        enemy = Hero("Enemy", 2, 9, 2)
        expected_msg = "You win"
        actual_msg = f"{self.hero.battle(enemy)}"
        self.assertEqual(expected_msg, actual_msg)

###enemy_hero_win_tests

    def test_hero_battle__when_hero_health_drop_below_zero_after_battle__return_msg(
            self):
        enemy = Hero("Enemy", 2, 100, 2)
        self.hero.health = 1
        expected_msg = "You lose"
        actual_msg = f"{self.hero.battle(enemy)}"
        self.assertEqual(expected_msg, actual_msg)

    def test_hero_battle__when_hero_health_drop_to_zero_after_battle__return_msg(
            self):
        enemy = Hero("Enemy", 2, 100, 2)
        self.hero.health = 4
        expected_msg = "You lose"
        actual_msg = f"{self.hero.battle(enemy)}"
        self.assertEqual(expected_msg, actual_msg)


# test_repr

    def test_hero_str_representation(self):
        expected_msg = f"Hero TestName: 3 lvl\n" \
               f"Health: 100\n" \
               f"Damage: 3\n"
        actual_msg = f"{self.hero.__str__()}"
        self.assertEqual(expected_msg, actual_msg)
コード例 #22
0
class TestHero(unittest.TestCase):
    def setUp(self):
        self.darlex = Hero("Darlex", 10, 100.0, 5.0)
        self.nartius = Hero("Nartius", 10, 50.0, 10.0)

    def test_if_init_is_properly_initialized(self):
        self.assertEqual(self.darlex.username, "Darlex")
        self.assertEqual(self.darlex.level, 10)
        self.assertEqual(self.darlex.health, 100.0)
        self.assertEqual(self.darlex.damage, 5.0)

    def test_if_hero_cannot_fight_self(self):
        other_darlex = Hero("Darlex", 1, 1.0, 1.0)
        with self.assertRaises(Exception) as context_manager:
            self.darlex.battle(other_darlex)
        self.assertEqual(str(context_manager.exception),
                         "You cannot fight yourself")

    def test_if_self_health_is_less_than_or_equal_to_zero(self):
        self.darlex.health = 0
        with self.assertRaises(ValueError) as context_manager:
            self.darlex.battle(self.nartius)
        self.assertEqual(
            str(context_manager.exception), "Your health is lower than "
            "or equal to 0. You need to rest")
        self.darlex.health = -5
        with self.assertRaises(ValueError) as context_manager:
            self.darlex.battle(self.nartius)
        self.assertEqual(
            str(context_manager.exception), "Your health is lower than "
            "or equal to 0. You need to rest")

    def test_if_enemy_health_is_less_than_or_equal_to_zero(self):
        self.nartius.health = 0
        with self.assertRaises(ValueError) as context_manager:
            self.darlex.battle(self.nartius)
        self.assertEqual(
            str(context_manager.exception), f"You cannot fight"
            f" {self.nartius.username}. "
            f"He needs to rest")

        self.nartius.health = -5
        with self.assertRaises(ValueError) as context_manager:
            self.darlex.battle(self.nartius)
        self.assertEqual(
            str(context_manager.exception), f"You cannot fight"
            f" {self.nartius.username}. "
            f"He needs to rest")

    def test_if_player_damage_is_properly_calculated(self):
        self.darlex.battle(self.nartius)
        expected = 50.0
        actual = self.darlex.damage * self.darlex.level
        self.assertEqual(actual, expected)

    def test_if_enemy_hero_damage_is_properly_calculated(self):
        self.darlex.battle(self.nartius)
        expected = 100.0
        actual = self.nartius.damage * self.nartius.level
        self.assertEqual(actual, expected)

    def test_if_str_method_returns_correct_information(self):
        result = str(self.darlex)
        expected = f"Hero Darlex: 10 lvl\n" \
               f"Health: 100.0\n" \
               f"Damage: 5.0\n"
        self.assertEqual(result, expected)

    def test_if_battle_ends_in_draw(self):
        self.darlex.health = 1
        self.nartius.health = 1
        actual = self.darlex.battle(self.nartius)
        expected = "Draw"
        self.assertEqual(actual, expected)

    def test_if_enemy_hero_loses_and_if_main_hero_stats_are_increased(self):
        self.nartius.health = 1
        self.darlex.health = 101
        expected_level = self.darlex.level + 1
        expected_damage = self.darlex.damage + 5
        expected_health = 6.0
        actual = self.darlex.battle(self.nartius)
        expected_message = 'You win'
        self.assertEqual(actual, expected_message)
        self.assertEqual(self.darlex.level, expected_level)
        self.assertEqual(self.darlex.health, expected_health)
        self.assertEqual(self.darlex.damage, expected_damage)

    def test_if_main_hero_loses_and_if_enemy_hero_stats_are_increased(self):
        self.nartius.health = 101
        self.darlex.health = 1
        expected_level = self.nartius.level + 1
        expected_damage = self.nartius.damage + 5
        expected_health = 56.0
        actual = self.darlex.battle(self.nartius)
        expected_message = 'You lose'
        self.assertEqual(actual, expected_message)
        self.assertEqual(self.nartius.level, expected_level)
        self.assertEqual(self.nartius.damage, expected_damage)
        self.assertEqual(self.nartius.health, expected_health)
コード例 #23
0
class TestHero(TestCase):
    def setUp(self):
        self.main_hero = Hero("Main Hero", 5, 100.0, 10.0)
        self.second_hero = Hero("Second Hero", 10, 75.0, 22.0)

    def test_check_instance_attributes_are_set(self):
        self.assertEqual("Main Hero", self.main_hero.username)
        self.assertEqual(5, self.main_hero.level)
        self.assertEqual(100.0, self.main_hero.health)
        self.assertEqual(10.0, self.main_hero.damage)

    def test_battle_if_fight_with_yourself(self):
        hero_self = Hero("Main Hero", 6, 80.0, 15.0)
        with self.assertRaises(Exception) as ex:
            self.main_hero.battle(hero_self)
        self.assertEqual("You cannot fight yourself", str(ex.exception))

    def test_health_is_zero_raises(self):
        self.main_hero.health = 0
        self.assertEqual(0, self.main_hero.health)
        with self.assertRaises(ValueError) as ex:
            self.main_hero.battle(self.second_hero)
        self.assertEqual("Your health is lower than or equal to 0. You need to rest", str(ex.exception))

    def test_health_is_less_than_zero_raises(self):
        self.main_hero.health = -5
        self.assertEqual(-5, self.main_hero.health)
        with self.assertRaises(ValueError) as ex:
            self.main_hero.battle(self.second_hero)
        self.assertEqual("Your health is lower than or equal to 0. You need to rest", str(ex.exception))

    def test_health_enemy_is_zero_raises(self):
        self.second_hero.health = 0
        self.assertEqual(0, self.second_hero.health)
        self.assertEqual(100, self.main_hero.health)
        with self.assertRaises(ValueError) as ex:
            self.main_hero.battle(self.second_hero)
        self.assertEqual("You cannot fight Second Hero. He needs to rest", str(ex.exception))

    def test_health_enemy_is_less_than_zero_raises(self):
        self.second_hero.health = -5
        self.assertEqual(-5, self.second_hero.health)
        self.assertEqual(100, self.main_hero.health)
        with self.assertRaises(ValueError) as ex:
            self.main_hero.battle(self.second_hero)
        self.assertEqual("You cannot fight Second Hero. He needs to rest", str(ex.exception))

    def test_fight_draw_case(self):
        self.second_hero.health = 50

        result = self.main_hero.battle(self.second_hero)
        self.assertEqual("Draw", result)

    def test_main_hero_wins(self):
        self.main_hero.damage = 200
        self.main_hero.health = 300

        result = self.main_hero.battle(self.second_hero)
        self.assertEqual("You win", result)
        self.assertEqual(205, self.main_hero.damage)
        self.assertEqual(6, self.main_hero.level)
        self.assertEqual(85, self.main_hero.health)

    def test_enemy_wins(self):
        result = self.main_hero.battle(self.second_hero)
        self.assertEqual("You lose", result)
        self.assertEqual(27, self.second_hero.damage)
        self.assertEqual(11, self.second_hero.level)
        self.assertEqual(30, self.second_hero.health)

        self.assertTrue(self.main_hero.health < 0)

    def test_string_represent(self):
        self.assertEqual("Hero Main Hero: 5 lvl\nHealth: 100.0\nDamage: 10.0\n", str(self.main_hero))
コード例 #24
0
class HeroTest(unittest.TestCase):
    username = '******'
    level = 2
    health = 100.0
    damage = 10.0

    def setUp(self) -> None:
        self.hero = Hero(username=self.username,
                         level=self.level,
                         health=self.health,
                         damage=self.damage)

    def test_hero_init(self):
        self.assertEqual(self.username, self.hero.username)
        self.assertEqual(self.health, self.hero.health)
        self.assertEqual(self.damage, self.hero.damage)
        self.assertEqual(self.level, self.hero.level)

    def test_hero_string_representation(self):
        expected = f"Hero {self.username}: {self.level} lvl\n" \
                   f"Health: {self.health}\n" \
                   f"Damage: {self.damage}\n"
        actual = self.hero.__str__()
        self.assertEqual(expected, actual)

    def test_battle__when_self_fight__should_raise_exception(self):
        enemy = self.hero
        with self.assertRaises(Exception) as exc:
            self.hero.battle(enemy)
        msg = "You cannot fight yourself"
        self.assertEqual(msg, str(exc.exception))

    def test_battle__when_hero_health_is_0__should_raise_value_error(self):
        enemy = Hero('enemy', 1, 100.0, 10.0)
        self.hero.health = 0
        with self.assertRaises(ValueError) as exc:
            self.hero.battle(enemy)
        msg = "Your health is lower than or equal to 0. You need to rest"
        self.assertEqual(msg, str(exc.exception))

    def test_battle__when_hero_health_is_negative__should_raise_value_error(
            self):
        enemy = Hero('enemy', 1, 100.0, 10.0)
        self.hero.health = -1
        with self.assertRaises(ValueError) as exc:
            self.hero.battle(enemy)
        msg = "Your health is lower than or equal to 0. You need to rest"
        self.assertEqual(msg, str(exc.exception))

    def test_battle__when_enemy_health_is_0__should_raise_value_error(self):
        enemy = Hero('enemy', 1, 0.0, 10.0)
        with self.assertRaises(ValueError) as exc:
            self.hero.battle(enemy)
        msg = f"You cannot fight {enemy.username}. He needs to rest"
        self.assertEqual(msg, str(exc.exception))

    def test_battle__when_enemy_health_is_negative__should_raise_value_error(
            self):
        enemy = Hero('enemy', 1, -1.0, 10.0)
        with self.assertRaises(ValueError) as exc:
            self.hero.battle(enemy)
        msg = f"You cannot fight {enemy.username}. He needs to rest"
        self.assertEqual(msg, str(exc.exception))

    def test_battle__hero_health_is_reduced_by_enemy_dmg(self):
        enemy = Hero(username='******', level=2, health=100.0, damage=10.0)
        self.hero.battle(enemy)
        self.assertEqual(80.0, self.hero.health)

    def test_battle__enemy_health_is_reduced_by_hero_dmg(self):
        enemy = Hero(username='******', level=2, health=100.0, damage=10.0)
        self.hero.battle(enemy)
        self.assertEqual(85.0, enemy.health)

    def test_battle__when_health_is_negative__should_return_draw(self):
        self.hero.damage = 100
        enemy = Hero(username='******', level=2, health=100.0, damage=100.0)
        expected = 'Draw'
        actual = self.hero.battle(enemy)
        self.assertEqual(expected, actual)

    def test_battle__when_health_is_0__should_return_draw(self):
        self.hero.damage = 50
        enemy = Hero(username='******', level=2, health=100.0, damage=50.0)
        expected = 'Draw'
        actual = self.hero.battle(enemy)
        self.assertEqual(expected, actual)

    def test_battle__when_hero_health_is_less_than_enemy__should_return_lose(
            self):
        enemy = Hero(username='******', level=2, health=100.0, damage=50.0)
        expected = 'You lose'
        actual = self.hero.battle(enemy)
        self.assertEqual(expected, actual)

    def test_battle__when_enemy_health_is_0__should_return_win(self):
        enemy = Hero(username='******', level=1, health=20.0, damage=10.0)
        expected = 'You win'
        actual = self.hero.battle(enemy)
        self.assertEqual(expected, actual)

    def test_battle__when_enemy_health_is_negative__should_return_win(self):
        enemy = Hero(username='******', level=1, health=10.0, damage=10.0)
        expected = 'You win'
        actual = self.hero.battle(enemy)
        self.assertEqual(expected, actual)

    def test_battle__when_win__should_increase_hero_stats(self):
        enemy = Hero(username='******', level=1, health=10.0, damage=0.0)
        self.hero.battle(enemy)
        self.assertEqual(3, self.hero.level)
        self.assertEqual(105.0, self.hero.health)
        self.assertEqual(15.0, self.hero.damage)

    def test_battle__when_lose__should_increase_enemy_stats(self):
        enemy = Hero(username='******', level=10, health=100.0, damage=100.0)
        self.hero.battle(enemy)
        self.assertEqual(11, enemy.level)
        self.assertEqual(85.0, enemy.health)
        self.assertEqual(105.0, enemy.damage)
コード例 #25
0
class TestHero(TestCase):
    username = '******'
    health = 100.00
    damage = 20.00
    level = 12

    enemy_username = '******'
    enemy_health = 240
    enemy_damage = 10.00
    enemy_level = 10

    def setUp(self):
        self.hero = Hero(self.username, self.level, self.health, self.damage)
        self.enemy = Hero(self.enemy_username, self.enemy_level, self.enemy_health, self.enemy_damage)

    def test_initialize_correct_values(self):
        self.assertEqual(self.username, self.hero.username)
        self.assertEqual(self.health, self.hero.health)
        self.assertEqual(self.damage, self.hero.damage)
        self.assertEqual(self.level, self.hero.level)

    def test_can_not_fight_yourself(self):
        with self.assertRaises(Exception):
            self.hero.battle(self.hero)

    def test_can_not_fight_when_hero_health_is_zero(self):
        self.hero.health = 0
        with self.assertRaises(ValueError):
            self.hero.battle(self.enemy)

        # self.assertEqual("Your health is lower than or equal to 0. You need to rest", str(ex.exception))

    def test_can_not_fight_when_enemy_health_is_zero(self):
        self.enemy.health = 0
        with self.assertRaises(ValueError):
            self.hero.battle(self.enemy)

        # self.assertEqual(f"You cannot fight {self.enemy_username}. He needs to rest", str(ex.exception))

    def test_hero_and_enemy_health_is_zero(self):
        self.assertEqual('Draw', self.hero.battle(self.enemy))

    def test_hero_wins_after_battle_and_values_increase(self):
        self.hero.health += 100

        self.assertEqual('You win', self.hero.battle(self.enemy))
        self.assertEqual(self.level + 1, self.hero.level)
        self.assertEqual(self.health + 5, self.hero.health)
        self.assertEqual(self.damage + 5, self.hero.damage)

        self.assertTrue(self.enemy.health <= 0)

    def test_enemy_wins_after_battle_and_values_increase(self):
        self.enemy.health += 240

        self.assertEqual('You lose', self.hero.battle(self.enemy))
        self.assertEqual(self.enemy_level + 1, self.enemy.level)
        self.assertEqual(self.enemy_health + 5, self.enemy.health)
        self.assertEqual(self.enemy_damage + 5, self.enemy.damage)

        self.assertTrue(self.hero.health <= 0)

    def test_hero_string_representation(self):
        self.assertEqual(f"Hero {self.username}: {self.level} lvl\nHealth: {self.health}\nDamage: {self.damage}\n",
                         self.hero.__str__())

    def test_enemy_string_representation(self):
        self.assertEqual(
            f"Hero {self.enemy_username}: {self.enemy_level} lvl\nHealth: {self.enemy_health}\nDamage: {self.enemy_damage}\n",
            self.enemy.__str__())
コード例 #26
0
ファイル: test_hero.py プロジェクト: PeterM358/python_OOP
class TestHero(TestCase):
    def setUp(self):
        self.main_hero = Hero('Goku', 2, 100.0, 10.0)
        self.enemy_hero = Hero('Freizer', 1, 80.0, 5.0)

    def test_init(self):
        self.assertEqual('Goku', self.main_hero.username)
        self.assertEqual(2, self.main_hero.level)
        self.assertEqual(100.0, self.main_hero.health)
        self.assertEqual(10.0, self.main_hero.damage)

    def test_battle__when_attack_same_hero__expect_exception(self):
        hero_self = Hero('Goku', 6, 80.0, 60.0)
        with self.assertRaises(Exception) as ex:
            self.main_hero.battle(hero_self)
        self.assertEqual("You cannot fight yourself", str(ex.exception))

    def test_battle__when_self_health_is_0__expect_exception(self):
        self.main_hero.health = 0
        self.assertEqual(0, self.main_hero.health)
        with self.assertRaises(ValueError) as ex:
            self.main_hero.battle(self.enemy_hero)
        self.assertEqual(
            'Your health is lower than or equal to 0. You need to rest',
            str(ex.exception))

    def test_battle__when_self_health_is_negative__expect_exception(self):
        self.main_hero.health = -1
        self.assertEqual(-1, self.main_hero.health)
        with self.assertRaises(ValueError) as ex:
            self.main_hero.battle(self.enemy_hero)
        self.assertEqual(
            'Your health is lower than or equal to 0. You need to rest',
            str(ex.exception))

    def test_battle__when_enemy_health_is_0__expect_exception(self):
        self.enemy_hero.health = 0
        self.assertEqual(0, self.enemy_hero.health)
        with self.assertRaises(ValueError) as ex:
            self.main_hero.battle(self.enemy_hero)
        self.assertEqual('You cannot fight Freizer. He needs to rest',
                         str(ex.exception))

    def test_battle__when_enemy_health_is_negative__expect_exception(self):
        self.enemy_hero.health = -1
        self.assertEqual(-1, self.enemy_hero.health)
        with self.assertRaises(ValueError) as ex:
            self.main_hero.battle(self.enemy_hero)
        self.assertEqual('You cannot fight Freizer. He needs to rest',
                         str(ex.exception))

    def test_battle__when_both_heroes_health_below_or_0__expect_exception(
            self):
        self.main_hero.damage = 40.0
        self.enemy_hero.damage = 110.0

        result = self.main_hero.battle(self.enemy_hero)
        self.assertEqual('Draw', result)

    def test_battle__when_main_hero_wins_battle__expect_main_hero_attrs_increase(
            self):
        self.main_hero.damage = 41.0

        result = self.main_hero.battle(self.enemy_hero)
        self.assertEqual('You win', result)
        self.assertEqual(3, self.main_hero.level)
        self.assertEqual(100.0, self.main_hero.health)
        self.assertEqual(46.0, self.main_hero.damage)
        self.assertTrue(self.enemy_hero.health <= 0)

    def test_battle__when_enemy_hero_wins_battle__expect_enemy_hero_attrs_increase(
            self):
        self.enemy_hero.damage = 101.0

        result = self.main_hero.battle(self.enemy_hero)
        self.assertEqual('You lose', result)
        self.assertEqual(2, self.enemy_hero.level)
        self.assertEqual(65.0, self.enemy_hero.health)
        self.assertEqual(106.0, self.enemy_hero.damage)
        self.assertTrue(self.main_hero.health <= 0)

    def test_str_represent__when_called__expect_correct_data(self):
        self.assertEqual("Hero Goku: 2 lvl\nHealth: 100.0\nDamage: 10.0\n",
                         str(self.main_hero))