11-3 雇员:编写一个名为Employee 的类,其方法__init__()接受名、姓和年薪,并
将它们都存储在属性中。编写一个名为give_raise()的方法,它默认将年薪增加5000
美元,但也能够接受其他的年薪增加量。
为Employee 编写一个测试用例,其中包含两个测试方法:test_give_default_
raise()和test_give_custom_raise()。使用方法setUp(),以免在每个测试方法中都创
建新的雇员实例。运行这个测试用例,确认两个测试都通过了。
感觉最后setUp()用的有点多此一举。。。
employee_information.py
class Employee():
def __init__(self, first_name, last_name, annual_salary ):
self.first_name = first_name
self.last_name = last_name
self.annual_salary = annual_salary
def give_raise(self, added_annual_salary= 5000):
self.annual_salary += added_annual_salary
test_employee_imformation.py
mport unittest
from employee_information import Employee
class Test_employee_information(unittest.TestCase):
def setUp(self) :
self.maoyibu = Employee('mao','yibu',5000)
self.annual_salary = [0, 1000]
def test_give_default_raise(self):
self.maoyibu.give_raise(self.annual_salary[0])
self.assertEqual(self.maoyibu.annual_salary, self.maoyibu.annual_salary+5000)
def test_give_custom_raise(self):
self.maoyibu.give_raise(self.annual_salary[1])
self.assertEqual(self.maoyibu.annual_salary, self.maoyibu.annual_salary + 5000 +self.annual_salary[1])
unittest.main()