gpt4 book ai didi

Python unittest.TestCase 对象没有属性 'runTest'

转载 作者:IT老高 更新时间:2023-10-28 21:09:48 25 4
gpt4 key购买 nike

对于以下代码:

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执行,出现如下错误:

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 :

if __name__ == "__main__":
suite = unittest.defaultTestLoader.loadTestsFromTestCase(Test)
unittest.TextTestRunner().run(suite)

关于Python unittest.TestCase 对象没有属性 'runTest',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19087189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com