Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python unittest: can't call decorated test

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

and the test method:

@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')])
like image 457
gruszczy Avatar asked Jun 10 '11 21:06

gruszczy


2 Answers

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.

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

like image 172
jcollado Avatar answered Oct 14 '22 05:10

jcollado


This is a simple way of solving this issue.

from functools import wraps

def your_decorator(func):
    @wraps(func)
    def wrapper(self):
        #Do Whatever
        return func(self)
    return wrapper

class YourTest(APITestCase):
    @your_decorator
    def example(self):
        #Do your thing
like image 33
Reez0 Avatar answered Oct 14 '22 04:10

Reez0