python unittest:不能调用修饰测试 [英] python unittest: can't call decorated test

查看:73
本文介绍了python unittest:不能调用修饰测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当大的测试套件,并且装饰了一些test_ *函数。现在我无法通过来调用它们。/test.pyMySqlTestCase.test_foo_double ,python3.2抱怨: ValueError:<中没有这样的测试方法; class'__main __。MySqlTestCase'> ;:结果。我的装饰代码如下:

I have a pretty large test suite and I decorated some of the test_* functions. Now I can't call them by ./test.py MySqlTestCase.test_foo_double, python3.2 complains that: ValueError: no such test method in <class '__main__.MySqlTestCase'>: result. My decorator code looks like this:

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')])


推荐答案

我认为问题在于修饰的函数名称不同,同样,它也不满足被视为测试方法的模式。

I think the problem is that the decorated function doesn't have the same name and, also, it doesn't satisfy the pattern to be considered a test method.

使用 functools.wrap 进行装饰装饰器应该可以解决您的问题。详细信息此处

Using functools.wrap to decorate decorator should fix your problem. More information here.

这篇关于python unittest:不能调用修饰测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆