Total Complexity | 12 |
Total Lines | 71 |
Duplicated Lines | 0 % |
Coverage | 67.74% |
1 | 1 | from plugin.core.helpers.variable import merge |
|
6 | 1 | class TestBase(object): |
|
7 | 1 | name = None |
|
8 | 1 | optional = False |
|
9 | |||
10 | 1 | @classmethod |
|
11 | def run(cls): |
||
12 | 1 | metadata = {} |
|
13 | |||
14 | # Retrieve names of test functions |
||
15 | 1 | names = [ |
|
16 | name for name in dir(cls) |
||
17 | if name.startswith('test_') |
||
18 | ] |
||
19 | |||
20 | 1 | if not names: |
|
21 | return cls.on_failure('No tests defined') |
||
22 | |||
23 | # Run tests |
||
24 | 1 | for name in names: |
|
25 | # Retrieve function by name |
||
26 | 1 | func = getattr(cls, name, None) |
|
27 | |||
28 | 1 | if not func: |
|
29 | return cls.on_failure('Unable to find function: %r' % name) |
||
30 | |||
31 | 1 | try: |
|
32 | # Run test function |
||
33 | 1 | result = func() |
|
34 | |||
35 | 1 | if not result: |
|
36 | 1 | continue |
|
37 | |||
38 | # Merge function result into `metadata` |
||
39 | 1 | 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 | 1 | return cls.on_success(metadata) |
|
45 | |||
46 | # |
||
47 | # Events |
||
48 | # |
||
49 | |||
50 | 1 | @classmethod |
|
51 | 1 | 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 | 1 | @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 | 1 | @staticmethod |
|
73 | def on_success(metadata): |
||
74 | 1 | return { |
|
75 | 'success': True, |
||
76 | 'metadata': metadata |
||
77 | } |
||
78 |