题目:
编写一个名为Employee 的类,其方法__init__() 接受名、姓和年薪,并将它们都存储在属性中。编写一个名为give_raise() 的方法,它默认将年薪增加5000美元,但也能够接受其他的年薪增加量。
为Employee 编写一个测试用例,其中包含两个测试方法:test_give_default_raise() 和test_give_custom_raise() 。使用方法setUp() ,以免在每个测试方法中都创建新的雇员实例。运行这个测试用例,确认两个测试都通过了。
employ.py
待测试的类
class Employee():
def __init__(self,first_name,last_name,salary):
self.first_name=first_name
self.last_name=last_name
self.salary=salary
def give_raise(self,default=5000):
return int(self.salary)+default
test_employ.py
测试类
# coding=utf-8
import unittest
from employ import Employee
class TestEmploy(unittest.TestCase):
def setUp(self):
self.people=Employee("ZHU","Fangya",20000)
self.salary=[25000,30000]
def test_give_default_raise(self):
self.assertEqual(self.people.give_raise(),self.salary[0])
def test_give_custome_raise(self):
self.default=10000
self.assertEqual(self.people.give_raise(default=10000),self.salary[1])
if __name__=="__main__":
unittest.main()
运行结果
Done:2 of 2 (0.137s)
C:\Python27\python.exe "C:\Program Files (x86)\JetBrains\PyCharm 4.0.6\helpers\pycharm\utrunner.py" C:\Users\waiwai\PycharmProjects\untitled2\test_employ.py true
Testing started at 16:03 ...
Process finished with exit code 0