Python unittest – ValueError:在runTest中没有这样的测试方法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python unittest – ValueError:在runTest中没有这样的测试方法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1753字,纯文字阅读大概需要3分钟。
内容图文
![Python unittest – ValueError:在runTest中没有这样的测试方法](/upload/InfoBanner/zyjiaocheng/792/cabfd91e7b574b8084e25615b99f548b.jpg)
我有一个非常简单的设置,使用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 unittest – ValueError:在runTest中没有这样的测试方法全部内容,希望文章能够帮你解决Python unittest – ValueError:在runTest中没有这样的测试方法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。