gpt4 book ai didi

Python 单元测试 - ValueError : no such test method in : runTest

转载 作者:太空狗 更新时间:2023-10-30 03:04:49 25 4
gpt4 key购买 nike

我有一个使用 unittest 的非常简单的设置,但我收到了一个我不明白的错误。

# mytestcase.py
import unittest

class MyTestCase(unittest.TestCase):
def test_one(self):
self.assertTrue(True)
def test_two(self):
self.assertTrue(False)


def initialize():
return MyTestCase()

if __name__ == '__main__':
unittest.main()

如果我执行上面的文件,我会得到以下结果,这是我所期望和理解的:

> python mytestcase.py
.F
======================================================================
FAIL: test_two (__main__.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "mytestcase.py", line 7, in test_two
self.assertTrue(False)
AssertionError: False is not true

----------------------------------------------------------------------
Ran 2 tests in 0.000s

FAILED (failures=1)

但我想以另一种方式运行这些测试,来自 my_test_manager.py:

# my_test_manager.py
import mytestcase

test_case = mytestcase.initialize()
test_suite = unittest.TestLoader().loadTestsFromTestCase(test_case)
test_suite_result = unittest.TestResult()
test_suite.run(test_suite_result)
for err in test_suite_result.errors:
print err
for fail in test_suite_result.failures:
print fail

但是如果我尝试运行这个文件,它会崩溃,如下所示:

> python my_test_manager.py 
Traceback (most recent call last):
File "my_test_manager.py", line 3, in <module>
test_case = mytestcase.initialize()
File "/Users/Jon/dev/test-tools/practice/mytestcase.py", line 11, in initialize
return MyTestCase()
File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 191, in __init__
(self.__class__, methodName))
ValueError: no such test method in <class 'mytestcase.MyTestCase'>: runTest

最佳答案

您不需要创建实例;返回 MyTestCase 类本身:

def initialize():
return MyTestCase

关于Python 单元测试 - ValueError : no such test method in <class 'mytestcase.MyTestCase' >: runTest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14367182/

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