繁体   English   中英

Python的unittest框架TestCase很好奇

[英]Python's unittest framework TestCase curiousity

我正在尝试在python 3.3.2中运行一个TestCase,它有几个测试方法:

class ttt(unittest.TestCase):
    def setUp(self):
        ...

    def tearDown(self):
        ...

    def test_test1(self):
        ...      

    def test_test2(self):
        ...



if __name__ == "__main__":
    instance = ttt()
    instance.run()

文档说明如下:

每个TestCase实例都将运行一个基本方法:名为methodName的方法。 但是,默认methodName,runTest()的标准实现将以test作为单独测试开始运行每个方法,并相应地计算成功和失败。 因此,在TestCase的大多数用法中,既不会更改methodName,也不会重新实现默认的runTest()方法。

但是,当我运行代码时,我得到以下内容:

'ttt' object has no attribute 'runTest'

我想问:这是一个错误吗? 如果不是为什么没有runTest方法呢? 难道我做错了什么?

当单元测试框架运行测试用例时,它会为每个测试创建一个测试类的实例。

即模拟单元测试框架需要做什么:

if __name__ == "__main__":
    for testname in ["test_test1", "test_test2"]:
        instance = ttt(testname)
        instance.run()

在模块中运行单元测试的正确方法是:

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

......但我猜你已经知道了。

关于runTestunittest.TestCase.__init__签名和docstring是:

def __init__(self, methodName='runTest'):
    """Create an instance of the class that will use the named test
       method when executed. Raises a ValueError if the instance does
       not have a method with the specified name.
    """

这意味着如果未在构造函数中指定测试名称,则默认为runTest

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM