Advertisement
horozov86

Hero_testing_after_base_class!!!

Jul 27th, 2023
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.78 KB | None | 0 0
  1. class Hero:
  2.     username: str
  3.     health: float
  4.     damage: float
  5.     level: int
  6.  
  7.     def __init__(self, username: str, level: int, health: float, damage: float):
  8.         self.username = username
  9.         self.level = level
  10.         self.health = health
  11.         self.damage = damage
  12.  
  13.     def battle(self, enemy_hero):
  14.         if enemy_hero.username == self.username:
  15.             raise Exception("You cannot fight yourself")
  16.  
  17.         if self.health <= 0:
  18.             raise ValueError("Your health is lower than or equal to 0. You need to rest")
  19.  
  20.         if enemy_hero.health <= 0:
  21.             raise ValueError(f"You cannot fight {enemy_hero.username}. He needs to rest")
  22.  
  23.         player_damage = self.damage * self.level
  24.         enemy_hero_damage = enemy_hero.damage * enemy_hero.level
  25.  
  26.         self.health -= enemy_hero_damage
  27.         enemy_hero.health -= player_damage
  28.  
  29.         if self.health <= 0 and enemy_hero.health <= 0:
  30.             return "Draw"
  31.  
  32.         if enemy_hero.health <= 0:
  33.             self.level += 1
  34.             self.health += 5
  35.             self.damage += 5
  36.             return "You win"
  37.  
  38.         enemy_hero.level += 1
  39.         enemy_hero.health += 5
  40.         enemy_hero.damage += 5
  41.         return "You lose"
  42.  
  43.     def __str__(self):
  44.         return f"Hero {self.username}: {self.level} lvl\n" \
  45.                f"Health: {self.health}\n" \
  46.                f"Damage: {self.damage}\n"
  47.                
  48. import unittest
  49.  
  50. class TestHero(unittest.TestCase):
  51.     def setUp(self):
  52.         self.hero = Hero('Hero', 1, 100, 100)
  53.         self.enemy = Hero('Enemy', 1, 50, 50)
  54.        
  55.        
  56.     def test_init(self):
  57.         self.assertEqual('Hero', self.hero.username)
  58.         self.assertEqual(1, self.hero.level)
  59.         self.assertEqual(100, self.hero.health)
  60.         self.assertEqual(100, self.hero.damage)
  61.        
  62.     def test_hero_is_the_same_as_enemy_raise_exception(self):
  63.         with self.assertRaises(Exception) as ex:
  64.             self.hero.battle(self.hero)
  65.         self.assertEqual("You cannot fight yourself", str(ex.exception))
  66.        
  67.     def test_hero_with_zero_energy(self):
  68.         self.hero.health = 0
  69.         with self.assertRaises(ValueError) as ex:
  70.             self.hero.battle(self.enemy)
  71.         self.assertEqual("Your health is lower than or equal to 0. You need to rest", str(ex.exception))
  72.        
  73.     def test_enemy_with_zero_energy(self):
  74.         self.enemy.health = 0
  75.         with self.assertRaises(ValueError) as ex:
  76.             self.hero.battle(self.enemy)
  77.         self.assertEqual("You cannot fight Enemy. He needs to rest", str(ex.exception))
  78.        
  79.     def test_battle_returns_draw(self):
  80.         self.hero.health = 50
  81.         result = self.hero.battle(self.enemy)
  82.        
  83.         self.assertEqual(0, self.hero.health)
  84.         self.assertEqual(-50, self.enemy.health)
  85.         self.assertEqual('Draw', result)
  86.        
  87.     def test_battle_enemy_and_win_expect(self):
  88.         result = self.hero.battle(self.enemy)
  89.         self.assertEqual(2, self.hero.level)
  90.         self.assertEqual(55, self.hero.health)
  91.         self.assertEqual(105, self.hero.damage)
  92.         self.assertEqual('You win', result)
  93.        
  94.     def test_battle_enemy_and_lose_expect_hero_stats_improve(self):
  95.         self.hero, self.enemy = self.enemy, self.hero
  96.         result = self.hero.battle(self.enemy)
  97.        
  98.         self.assertEqual(2, self.enemy.level)
  99.         self.assertEqual(55, self.enemy.health)
  100.         self.assertEqual(105, self.enemy.damage)
  101.         self.assertEqual("You lose", result)
  102.        
  103.    
  104.     def test_correct_str(self):
  105.         self.assertEqual(
  106.             f"Hero Hero: 1 lvl\n" \
  107.             f"Health: 100\n" \
  108.             f"Damage: 100\n", str(self.hero))
  109.        
  110. if __name__ == '__main__':
  111.     unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement