
python 中的 attributeerror 问题
在学习 python 时,您可能会遇到以下错误消息:
attributeerror: 'testemployee' object has no attribute 'employee'
这通常是因为在测试方法中使用了错误的方法名称。
错误原因
立即学习“Python免费学习笔记(深入)”;
在测试类中,setup 方法必须以小写字母开头的下划线 _ 开头,例如 setup。
解决方案
要解决此问题,请将 setup 方法名称更正为 setup:
from employee import Employee
class TestEmployee(unittest.TestCase):
def setUp(self):
self.employee = Employee('taylor', 'swift', 20000)
def test_give_default_raise(self):
self.employee.give_raise()
self.assertEqual(self.employee.pay, 25000)
unittest.main()










