
python 中的 attributeerror 问题
在 python 中使用 unittest 框架进行测试时,有时可能会遇到 attributeerror 异常。此异常通常是由于测试方法中引用了未定义的属性造成的。
问题描述
例如,在下面的代码中,test_give_default_raise 方法中引用的 employee 属性未在 setup 方法中定义,因此会出现 attributeerror 异常:
立即学习“Python免费学习笔记(深入)”;
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.self.pay, 25000)
解决方法
要解决此问题,需要确保在 setup 方法中正确定义了 employee 属性。具体来说,应将 setup 方法更正为:
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)
以上就是Python 中的 unittest 框架测试时遇到 AttributeError 异常怎么办?的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1351130.html
微信扫一扫
支付宝扫一扫