Python 中的 AttributeError: ‘TestEmployee’ 对象没有 ’employee’ 属性,怎么办?

python 中的 attributeerror: 'testemployee' 对象没有 'employee' 属性,怎么办?

python 中的 attributeerror 问题

在学习 python 时,您可能会遇到以下错误消息:

attributeerror: 'testemployee' object has no attribute 'employee'

这通常是因为在测试方法中使用了错误的方法名称。

错误原因

立即学习“Python免费学习笔记(深入)”;

在测试类中,setup 方法必须以小写字母开头的下划线 _ 开头,例如 setup。

解决方案

要解决此问题,请将 setup 方法名称更正为 setup:

from employee import Employeeclass 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()

以上就是Python 中的 AttributeError: ‘TestEmployee’ 对象没有 ’employee’ 属性,怎么办?的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1351066.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月13日 15:37:48
下一篇 2025年12月13日 15:37:55

相关推荐

发表回复

登录后才能评论
关注微信