Skip to content

Commit

Permalink
[김원경] 12주차 미션 제출 (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
secureWKkim authored Jul 3, 2024
1 parent 0eb3441 commit 23edfb2
Showing 1 changed file with 206 additions and 0 deletions.
206 changes: 206 additions & 0 deletions 8th_members/김원경/12주차.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
# unittest를 이용해 나만의 Test Case 만들기
디렉토리 구조는 아래와 같이 구성했습니다.
<img width="244" alt="image" src="https://github.com/Pseudo-Lab/CPython-Guide/assets/48075848/f9d7af1f-a2e3-4c9b-b902-74d409adae0e">
<br><br>

각 코드는 아래와 같습니다.<br><br>

### game.py
```python
from abc import ABC, abstractmethod


class Unit(ABC):
def __init__(self, _type, _name, _price):
self.type = _type
self.name = _name
self.price = _price

@abstractmethod
def action(self):
pass

@abstractmethod
def print(self):
pass

def get_type(self):
return self.type

def get_name(self):
return self.name

def get_price(self):
return self.price


class Attacker(Unit):
def __init__(self, type, name, price, _type, _name, _price, _attack_point):
super().__init__(type, name, price)
self.at_ty = _type
self.at_name = _name
self.at_price = _price
self.attack_point = _attack_point

def action(self):
return self.attack_point

def print(self):
print(f"[Attacker] Name: {self.at_name} (Attack point: {self.attack_point}, Price: {self.at_price})")


class Miner(Unit):
def __init__(self, type, name, price, _type, _name, _price, _mining_point):
super().__init__(type, name, price)
self.mi_ty = _type
self.mi_name = _name
self.mi_price = _price
self.mining_point = _mining_point

def action(self):
return self.mining_point

def print(self):
print(f"[Miner] Name: {self.mi_name} (Mining point: {self.mining_point}, Price: {self.mi_price})")


class ApplicationType:
def __init__(self):
self.unit_list_in_barracks = []
self.unit_list = []
self.curr_turn = 1
self.MAX_turn = 50
self.gold = 1000
self.enemy_hp = 500
self.flag_status = 0

def __del__(self):
for elem in self.unit_list_in_barracks:
del elem
for elem in self.unit_list:
del elem

def increase_turn(self):
self.curr_turn += 1
if self.curr_turn == self.MAX_turn:
self.flag_status = 1
print("You lose")

def print_unit_list_in_barracks(self):
for elem in self.unit_list_in_barracks:
elem.print()

def print_unit_list(self):
for elem in self.unit_list:
elem.print()

def run(self):
while self.flag_status == 0:
sOption = self.get_command()
if sOption == "1":
self.hire_unit()
elif sOption == "2":
self.attack_to_enemy()
elif sOption == "3":
self.gather_money()
elif sOption == "4":
self.print_unit_list()
elif sOption == "5":
break
print("Exit the program...")

def get_command(self):
print(f"Turn: {self.curr_turn}")
print(f"Gold: {self.gold}")
print(f"Enemy HP: {self.enemy_hp}\n")
print("1. Hire Unit\n2. Attack to Enemy\n3. Gather Money\n4. Print Units\nInput>", end='')
cmd = input()
return cmd

def hire_unit(self):
pass

def attack_to_enemy(self):
pass

def gather_money(self):
pass

```
<br><br>

### test.py
```python
import unittest
from unittest.mock import patch
from io import StringIO

from game import Attacker, Miner, ApplicationType


class TestAttacker(unittest.TestCase):
def test_attacker_action(self):
attacker = Attacker(1, "Attacker1", 100, 1, "Attacker1", 100, 50)
self.assertEqual(attacker.action(), 50)

def test_attacker_print(self):
attacker = Attacker(1, "Attacker1", 100, 1, "Attacker1", 100, 50)
with patch('sys.stdout', new=StringIO()) as fake_out:
attacker.print()
self.assertEqual(fake_out.getvalue().strip(), "[Attacker] Name: Attacker1 (Attack point: 50, Price: 100)")


class TestMiner(unittest.TestCase):
def test_miner_action(self):
miner = Miner(1, "Miner1", 100, 1, "Miner1", 100, 30)
self.assertEqual(miner.action(), 30)

def test_miner_print(self):
miner = Miner(1, "Miner1", 100, 1, "Miner1", 100, 30)
with patch('sys.stdout', new=StringIO()) as fake_out:
miner.print()
self.assertEqual(fake_out.getvalue().strip(), "[Miner] Name: Miner1 (Mining point: 30, Price: 100)")


class TestApplicationType(unittest.TestCase):
def setUp(self):
self.app = ApplicationType()

def test_initial_values(self):
self.assertEqual(self.app.curr_turn, 1)
self.assertEqual(self.app.MAX_turn, 50)
self.assertEqual(self.app.gold, 1000)
self.assertEqual(self.app.enemy_hp, 500)
self.assertEqual(self.app.flag_status, 0)

@patch('builtins.input', side_effect=['5'])
def test_run(self):
with patch('sys.stdout', new=StringIO()) as fake_out:
self.app.run()
self.assertIn("Exit the program...", fake_out.getvalue())

def test_increase_turn(self):
self.app.increase_turn()
self.assertEqual(self.app.curr_turn, 2)
self.app.curr_turn = 49
self.app.increase_turn()
self.assertEqual(self.app.flag_status, 1)

@patch('sys.stdout', new_callable=StringIO)
def test_print_unit_list_in_barracks(self, mock_stdout):
miner = Miner(1, "Miner1", 100, 1, "Miner1", 100, 30)
self.app.unit_list_in_barracks.append(miner)
self.app.print_unit_list_in_barracks()
self.assertIn("[Miner] Name: Miner1 (Mining point: 30, Price: 100)", mock_stdout.getvalue().strip())

@patch('sys.stdout', new_callable=StringIO)
def test_print_unit_list(self, mock_stdout):
attacker = Attacker(1, "Attacker1", 100, 1, "Attacker1", 100, 50)
self.app.unit_list.append(attacker)
self.app.print_unit_list()
self.assertIn("[Attacker] Name: Attacker1 (Attack point: 50, Price: 100)", mock_stdout.getvalue().strip())
```
<br><br>

실행 결과는 아래와 같습니다.
<img width="811" alt="image" src="https://github.com/Pseudo-Lab/CPython-Guide/assets/48075848/b4c40833-2b6b-4557-bfd6-ad95582505c8">

0 comments on commit 23edfb2

Please sign in to comment.