Total Complexity | 11 |
Total Lines | 71 |
Duplicated Lines | 0 % |
1 | from plugin.core.helpers.variable import merge |
||
6 | class TestBase(object): |
||
7 | name = None |
||
8 | optional = False |
||
9 | |||
10 | @classmethod |
||
11 | def run(cls): |
||
12 | metadata = {} |
||
13 | |||
14 | # Retrieve names of test functions |
||
15 | names = [ |
||
16 | name for name in dir(cls) |
||
17 | if name.startswith('test_') |
||
18 | ] |
||
19 | |||
20 | if not names: |
||
21 | return cls.on_failure('No tests defined') |
||
22 | |||
23 | # Run tests |
||
24 | for name in names: |
||
25 | # Retrieve function by name |
||
26 | func = getattr(cls, name, None) |
||
27 | |||
28 | if not func: |
||
29 | return cls.on_failure('Unable to find function: %r' % name) |
||
30 | |||
31 | try: |
||
32 | # Run test function |
||
33 | result = func() |
||
34 | |||
35 | if not result: |
||
36 | continue |
||
37 | |||
38 | # Merge function result into `metadata` |
||
39 | merge(metadata, result, recursive=True) |
||
40 | except Exception, ex: |
||
41 | return cls.on_exception('Exception raised in %r: %s' % (name, ex)) |
||
42 | |||
43 | # Tests successful |
||
44 | return cls.on_success(metadata) |
||
45 | |||
46 | # |
||
47 | # Events |
||
48 | # |
||
49 | |||
50 | @classmethod |
||
51 | def on_exception(cls, message, exc_info=None): |
||
52 | if exc_info is None: |
||
53 | exc_info = sys.exc_info() |
||
54 | |||
55 | return cls.on_failure( |
||
56 | message, |
||
57 | exc_info=exc_info |
||
58 | ) |
||
59 | |||
60 | @classmethod |
||
61 | def on_failure(cls, message, **kwargs): |
||
62 | result = { |
||
63 | 'success': False, |
||
64 | 'message': message |
||
65 | } |
||
66 | |||
67 | # Merge extra attributes |
||
68 | merge(result, kwargs) |
||
69 | |||
70 | return result |
||
71 | |||
72 | @staticmethod |
||
73 | def on_success(metadata): |
||
74 | return { |
||
75 | 'success': True, |
||
76 | 'metadata': metadata |
||
77 | } |
||
78 |