-
Notifications
You must be signed in to change notification settings - Fork 0
/
execution.py
192 lines (123 loc) · 5.56 KB
/
execution.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import datetime
import inspect
import logging
import os
import sys
import traceback
from reporting import *
from test import *
####################################################################
#
# Execution Framework
#
####################################################################
class TinyTester(object):
def __init__(self, level = 'INFO'):
self.__fileHandler = None
self.__formatter = logging.Formatter('%(levelname)s: %(message)s')
self.log = logging.getLogger(self.__class__.__name__)
for handler in self.log.handlers:
self.log.removeHandler(handler)
consoleHandler = logging.StreamHandler(sys.stdout)
consoleHandler.setFormatter(self.__formatter)
self.log.addHandler(consoleHandler)
self.log.setLevel(level)
self.log.propagate = False
def addFileHandler(self, cls):
assert not self.__fileHandler, 'File handler already exists.'
testFile = inspect.getfile(cls)
testDir = os.path.dirname(testFile)
resultDir = os.path.join(testDir, 'results')
path = os.path.join(resultDir, '{0}.log'.format(cls.__name__))
if not os.path.exists(resultDir):
os.makedirs(resultDir)
self.__fileHandler = logging.FileHandler(path, 'w')
self.__fileHandler.setFormatter(self.__formatter)
self.log.addHandler(self.__fileHandler)
def removeFileHandler(self):
assert self.__fileHandler, 'No file handler exists.'
self.log.removeHandler(self.__fileHandler)
self.__fileHandler = None
self.log.debug('Result file closed.')
def run(self, classes):
assert isinstance(classes, list), 'Classes must be provided as a list'
suiteStart = datetime.datetime.now()
totalCompleted = 0
totalPassed = 0
totalSkipped = 0
for cls in classes:
if not issubclass(cls, TestClass):
self.log.error('{0} does not inherit from TestClass'.format(cls))
continue
self.addFileHandler(cls)
self.log.info('[ {0} ]'.format(cls.description))
instance = cls()
setup = None
cleanup = None
tests = []
for name in dir(cls):
method = getattr(cls, name)
if not hasattr(method, 'fixture'):
continue
fixture = getattr(method, 'fixture')
if fixture == TestClass.Fixtures.SETUP:
setup = method
elif fixture == TestClass.Fixtures.CLEANUP:
cleanup = method
elif fixture == TestClass.Fixtures.TEST:
tests.append(method)
assert tests, 'No tests found.'
if hasattr(cls, TestClass.Fixtures.SKIP) and getattr(cls, TestClass.Fixtures.SKIP):
totalSkipped += len(tests)
continue
report = Report()
report.initialize([test.description for test in tests])
testStart = datetime.datetime.now()
for test in tests:
if hasattr(test, TestClass.Fixtures.SKIP) and getattr(test, TestClass.Fixtures.SKIP):
continue
self.log.info('[ {0} should {1} ]'.format(cls.description, test.description))
status = Status.INCOMPLETE
try:
if setup:
self.log.debug('[ Setup ]')
setup(instance)
try:
self.log.debug('[ Test ]')
test(instance)
status = Status.PASS
except AssertionError as assertion:
self.log.error(traceback.format_exc())
report.addFailure(assertion.message)
status = Status.FAIL
print 'test error: {0}'.format(assertion.message)
finally:
if cleanup:
self.log.debug('[ Cleanup ]')
cleanup(instance)
except Exception as exception:
self.log.error(traceback.format_exc())
report.addFailure(str(exception))
status = Status.BLOCKED
finally:
report.update(test.description, status)
completed = report.completed
duration = (datetime.datetime.now() - testStart).total_seconds()
passed = report.passes
skipped = len(tests) - completed
self.log.info('=============================================')
self.log.info(cls.description)
self.log.info('---------------------------------------------')
results = report.results
if not results:
self.log.warning('No results')
for result in results:
self.log.info(result)
self.log.info('=============================================')
self.log.info('Passed {0}/{1}. Skipped {2}. Run time: {3} seconds.'.format(passed, completed, skipped, duration))
totalCompleted += completed
totalPassed += passed
totalSkipped += skipped
self.removeFileHandler()
duration = (datetime.datetime.now() - suiteStart).total_seconds()
self.log.info('Passed {0}/{1}. Skipped {2}. Run time: {3} seconds.'.format(totalPassed, totalCompleted, totalSkipped, duration))