I want to create a class decorator to decorate a class and work with subclasses.
Imagine this class:
class CustomBaseTest(TransactionTestCase):
def __init__(self, *args, **kwargs):
...
def more_custom_helpers(self):
...
and the real test:
class FooTest(CustomBaseTest):
def decorate_this_foo_is_ok(self):
....
def decorate_this_fails(self):
...
I want to use a decorator in CustomBaseTestwhich finds all methods starting with 'decoratte_this_' and executes custom code after and before. I already have a decorator, something like this:
def class_decorator(klass):
is_method_test = lambda m: not m.startswith('_') and m.startswith('decorate_this_') and isinstance(getattr(klass, m), MethodType)
test_methods = filter(is_method_test, dir(klass))
for method_name in test_methods:
class_method = getattr(klass, method_name)
def helper(mname, method):
@wraps(method)
... some logic here
retval = method(*a, **kw)
... more logic here
return retval
return wrapper
fn = MethodType(helper(method_name, class_method), None, klass)
setattr(klass, method_name, fn)
return klass
Do you know if this can be done? And How?
thank!!!
pahko source
share