Python 中的属性错误
我正在尝试向 Python 中的对象添加单元测试属性,
class Boy:
def run(self, args):
print("Hello")
class BoyTest(unittest.TestCase)
def test(self)
self.assertEqual('2' , '2')
def self_test():
suite = unittest.TestSuite()
loader = unittest.TestLoader()
suite.addTest(loader.loadTestsFromTestCase(Boy.BoyTest))
return suite
但是,每当我调用 self_test() 时,我都会收到“AttributeError: class Boy has no attribute 'BoyTest'” >。为什么?
I'm trying to add a unittest attribute to an object in Python
class Boy:
def run(self, args):
print("Hello")
class BoyTest(unittest.TestCase)
def test(self)
self.assertEqual('2' , '2')
def self_test():
suite = unittest.TestSuite()
loader = unittest.TestLoader()
suite.addTest(loader.loadTestsFromTestCase(Boy.BoyTest))
return suite
However, I keep getting "AttributeError: class Boy has no attribute 'BoyTest'"
whenever I call self_test()
. Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
作为
loadTestsFromTestCase
的参数,您尝试访问Boy.BoyTest
,即类对象Boy< 的
BoyTest
属性/code>,它根本不存在,正如错误消息告诉您的那样。为什么不直接使用BoyTest
呢?As the argument of
loadTestsFromTestCase
, you're trying to accessBoy.BoyTest
, i.e., theBoyTest
attribute of class objectBoy
, which just doesn't exist, as the error msg is telling you. Why don't you just useBoyTest
there instead?正如 Alex 所说,您正在尝试使用 BoyTest 作为 Boy 的属性:
请注意更改:
至:
这可以解决您的问题吗?
As Alex has stated you are trying to use BoyTest as an attibute of Boy:
Note the change:
to:
Does this solve your problem?