首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >runTest :在<class 'mytestcase.MyTestCase'>:Python中没有这样的测试方法

runTest :在<class 'mytestcase.MyTestCase'>:Python中没有这样的测试方法
EN

Stack Overflow用户
提问于 2013-01-17 04:26:20
回答 2查看 5.6K关注 0票数 4

我有一个使用unittest的非常简单的设置,但我得到了一个我不理解的错误。

代码语言:javascript
复制
# 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()

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

代码语言:javascript
复制
> 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

代码语言:javascript
复制
# 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

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

代码语言:javascript
复制
> 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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-17 04:30:57

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

代码语言:javascript
复制
def initialize():
    return MyTestCase
票数 4
EN

Stack Overflow用户

发布于 2019-01-24 19:29:30

谢谢,它就像你建议的那样工作。

代码语言:javascript
复制
from mytestcase import MyTestCase
import unittest
test_suite = unittest.TestLoader().loadTestsFromTestCase(MyTestCase)
test_suite_result = unittest.TestResult()
test_suite.run(test_suite_result)
for err in test_suite_result.errors:
    print("hello")
for fail in test_suite_result.failures:
    print("no hello")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14367182

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档