gpt4 book ai didi

python 单元测试: can't call decorated test

转载 作者:太空狗 更新时间:2023-10-29 22:27:47 25 4
gpt4 key购买 nike

我有一个相当大的测试套件,我装饰了一些 test_* 函数。现在我不能通过 ./test.py MySqlTestCase.test_foo_double 给他们打电话了, python3.2 提示:ValueError: no such test method in <class '__main__.MySqlTestCase'>: result .我的装饰器代码如下所示:

def procedure_test(procedure_name, arguments_count, returns):

'''Decorator for procedure tests, that simplifies testing whether procedure
with given name is available, whether it has given number of arguments
and returns given value.'''

def decorator(test):
def result(self):
procedure = self.db.procedures[self.case(procedure_name)]
self.assertEqual(len(procedure.arguments), arguments_count)
self.assertEqual(procedure.returns,
None if returns is None else self.case(returns))
test(self, procedure)
return result
return decorator

及测试方法:

@procedure_test('foo_double', 0, 'integer')
def test_foo_double(self, procedure):
self.assertEqual(procedure.database, self.db)
self.assertEqual(procedure.sql, 'RETURN 2 * value')
self.assertArguments(procedure, [('value', 'int4')])

最佳答案

我认为问题在于装饰函数没有相同的名称,而且它不满足被视为测试方法的模式。

使用 functools.wrap 装饰 decorator 应该可以解决您的问题。更多信息 here .

关于 python 单元测试: can't call decorated test,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6312167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com