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')])
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With