Python unittest.TestCase 对象没有属性“runTest" [英] Python unittest.TestCase object has no attribute 'runTest'
本文介绍了Python unittest.TestCase 对象没有属性“runTest"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于以下代码:
import unittest
class Test(unittest.TestCase):
def test1(self):
assert(True == True)
if __name__ == "__main__":
suite = unittest.TestSuite()
suite.addTest(Test())
unittest.TextTestRunner().run(suite)
使用Python 3执行,出现如下错误:
Using Python 3 to execute it, the following error is raised:
Traceback (most recent call last):
File "test.py", line 10, in <module>
unittest.TextTestRunner().run(suite)
File "/usr/lib/python3.2/unittest/runner.py", line 168, in run
test(result)
File "/usr/lib/python3.2/unittest/suite.py", line 67, in __call__
return self.run(*args, **kwds)
File "/usr/lib/python3.2/unittest/suite.py", line 105, in run
test(result)
File "/usr/lib/python3.2/unittest/case.py", line 477, in __call__
return self.run(*args, **kwds)
File "/usr/lib/python3.2/unittest/case.py", line 408, in run
testMethod = getattr(self, self._testMethodName)
AttributeError: 'Test' object has no attribute 'runTest'
但是 unittest.main()
有效.
推荐答案
你需要调用一个 TestLoader
:
You need to invoke a TestLoader
:
if __name__ == "__main__":
suite = unittest.defaultTestLoader.loadTestsFromTestCase(Test)
unittest.TextTestRunner().run(suite)
这篇关于Python unittest.TestCase 对象没有属性“runTest"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文