Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的单元测试_Python_Testing_Subclass - Fatal编程技术网

Python中的单元测试

Python中的单元测试,python,testing,subclass,Python,Testing,Subclass,可能重复: 当我从另一个文件调用上面的类时,我得到以下错误。请让我知道我错在哪里 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "testsuite/test_se.py", line 10, in __init__ super(unittest.Testcase,self).__init__() File "/usr/lib/python2.7/unitt

可能重复:

当我从另一个文件调用上面的类时,我得到以下错误。请让我知道我错在哪里

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "testsuite/test_se.py", line 10, in __init__
    super(unittest.Testcase,self).__init__()
File "/usr/lib/python2.7/unittest/case.py", line 184, in __init__
    (self.__class__, methodName))
ValueError: no such test method in <class 'testsuite.test_se.BztTestSe'>: runTest
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“testsuite/test_se.py”,第10行,在__
super(unittest.Testcase,self)。\uu初始化
文件“/usr/lib/python2.7/unittest/case.py”,第184行,在__
(self.\uuuuu类\uuuuuu,方法名))
ValueError:在:runTest中没有此类测试方法

让我们试着回到简单的话题。在使用unittest时,有几种方法可以执行测试用例,但最简单的方法是在包含unittests的文件中有一个主函数

例如:

import unittest

class TestSomething(unittest.TestCase):

    def setUp(self):
        self.message = "does this work"

    def test_message_is_expected(self):
        self.assertEquals("does this work", self.message)


if __name__ == '__main__':
     unittest.main()
请注意您的测试用例(类)子类unittest.TestCase,然后您可以使用setUp方法为您的测试用例设置任何状态,最后您将需要一些前缀为test的方法。。。测试运行程序将执行的

如果将上述文件保存到test_something.py中,然后在控制台中运行python test_something.py,您将看到控制台的测试输出结果

如果您可以使用此模式而不是您使用的继承层次结构将示例重新转换为更清晰的内容,那么您可能能够执行测试


我意识到这更多的是一个评论而不是一个答案,但我还不能发表评论。

“给上面的同学打电话”?这是什么意思?对不起,我是python的新手。我从另一个文件调用了这个类,但它抛出了这个错误。请向我们展示您在另一个文件中调用该类的确切代码。无论如何,注释有点长:)
import unittest

class TestSomething(unittest.TestCase):

    def setUp(self):
        self.message = "does this work"

    def test_message_is_expected(self):
        self.assertEquals("does this work", self.message)


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