- 测试
- employee.py
class employee(): def __init__(self, fname, lname, salary): self.first_name = fname self.last_name = lname self.salary = salary def give_raise(self, add_num = 5000): self.salary += add_num
2.test_employee
from employee import employeeimport unittestclass test_employee(unittest.TestCase): def setUp(self): self.em = employee("hao", "kai", 10000) self.salary_1 = 15000 self.salary_2 = 20000 self.add = 10000 def test_give_raise(self): self.em.give_raise() self.assertEqual(self.em.salary, self.salary_1) def test_give_custom_raise(self): self.em.give_raise(self.add) self.assertEqual(self.em.salary, self.salary_2)
测试类功能是否正常。其中setUp()函数初始化类对象,便于类中其他方法使用。