我使用unittest在Python中进行一些测试。我对FunctionTestCase感兴趣。从文档中,这是示例用法:
testcase = unittest.FunctionTestCase(testSomething,
setUp=makeSomethingDB,
tearDown=deleteSomethingDB)
testSomething定义为
def testSomething():
something = makeSomething()
assert something.name is not None
# ...
但是,我有兴趣将一个参数传递给testSomething(),如下所示。你知道吗
def testSomething(input):
something = makeSomething(input)
assert something.name is not None
# ...
当我运行它时,已经构建了它,如上所述:
runner= unittest.TextTestRunner()
runner.run(testcase)
我明白了
ERROR: unittest.case.FunctionTestCase (test_meth4)
----------------------------------------------------------------------
TypeError: testSomething() missing 1 required positional argument: 'input'
----------------------------------------------------------------------
Ran 1 tests in 0.007s
有没有方法传入参数,或者需要使用闭包/外部函数之类的函数?谢谢
看起来您不能将参数传递给正在测试的函数。在python2和python3中,
FunctionTestCase.runTest()
只包含一行:self._testFunc()
,没有传递参数的规定。您可以将FunctionTestCase
子类化并向其__init__()
添加参数,然后让runTest()
将参数传递给正在测试的函数:我鼓励您在https://bugs.python.org请求此功能和/或更多关于此功能的文档。这似乎是一个很大的差距,而且几乎没有任何官方文档。你知道吗
相关问题 更多 >
编程相关推荐