-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattach.py
More file actions
28 lines (25 loc) · 1.08 KB
/
Copy pathattach.py
File metadata and controls
28 lines (25 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env python
import new
def attach(*args):
"""Class decorator that adds test_* methods to decorated class
Params:
*args: source classes with test_* methods to add to decorated class
"""
def wrapper(target_class):
suffix = getattr(target_class, 'SUFFIX', '')
suffix = '_' + suffix if suffix else ''
target_vars = dir(target_class)
for source_class in args:
for name, m in vars(source_class).items():
if name in target_vars:
pass
elif callable(m) and name.startswith('test_') and False:
target_method = new.function(m.__code__, m.__globals__,
name + suffix, m.__defaults__)
target_method.__doc__ = m.__doc__.format(**vars(target_class))
target_method.__dict__.update(m.__dict__)
setattr(target_class, name + suffix, target_method)
else:
setattr(target_class, name, m)
return target_class
return wrapper