forked from tfio/pytest-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
428 lines (346 loc) · 13 KB
/
model.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
from datetime import datetime
from events import EventSource
import sys
class ModelLoadError(Exception):
def __init__(self, trace):
super(ModelLoadError, self).__init__()
self.trace = trace
class TestMethod(EventSource):
STATUS_PASS = 100
STATUS_SKIP = 200
STATUS_EXPECTED_FAIL = 300
STATUS_UNEXPECTED_SUCCESS = 400
STATUS_FAIL = 500
STATUS_ERROR = 600
FAILING_STATES = (STATUS_FAIL, STATUS_UNEXPECTED_SUCCESS, STATUS_ERROR)
STATUS_LABELS = {
STATUS_PASS: 'passed',
STATUS_SKIP: 'skipped',
STATUS_FAIL: 'failures',
STATUS_EXPECTED_FAIL: 'expected failures',
STATUS_UNEXPECTED_SUCCESS: 'unexpected successes',
STATUS_ERROR: 'errors',
}
def __init__(self, name, testCase):
self.name = name
self.description = ''
self._active = True
self._result = None
# Set the parent of the TestMethod
self.parent = testCase
self.parent[name] = self
self.parent._update_active()
# Announce that there is a new test method
self.emit('new')
def __repr__(self):
return u'TestMethod %s' % self.path
@property
def path(self):
"The dotted-path name that identifies this test method to the test runner"
return u'%s.%s' % (self.parent.path, self.name)
@property
def active(self):
"Is this test method currently active?"
return self._active
def set_active(self, is_active, cascade=True):
"Explicitly set the active state of the test method"
if self._active:
if not is_active:
self._active = False
self.emit('inactive')
if cascade:
self.parent._update_active()
else:
if is_active:
self._active = True
self.emit('active')
if cascade:
self.parent._update_active()
def toggle_active(self):
"Toggle the current active status of this test method"
self.set_active(not self.active)
@property
def status(self):
try:
return self._result['status']
except TypeError:
return None
@property
def output(self):
try:
return self._result['output']
except TypeError:
return None
@property
def error(self):
try:
return self._result['error']
except TypeError:
return None
@property
def duration(self):
try:
return self._result['duration']
except TypeError:
return None
def set_result(self, status, output, error, duration):
self._result = {
'status': status,
'output': output,
'error': error,
'duration': duration,
}
self.emit('status_update')
class TestCase(dict, EventSource):
def __init__(self, name, testApp):
super(TestCase, self).__init__()
self.name = name
self._active = True
# Set the parent of the TestCase
self.parent = testApp
self.parent[name] = self
self.parent._update_active()
# Announce that there is a new TestCase
self.emit('new')
def __repr__(self):
return u'TestCase %s' % self.path
@property
def path(self):
return u'%s.%s' % (self.parent.path, self.name)
@property
def active(self):
return self._active
def set_active(self, is_active, cascade=True):
if self._active:
if not is_active:
self._active = False
self.emit('inactive')
if cascade:
self.parent._update_active()
for testMethod in self.values():
testMethod.set_active(False, cascade=False)
else:
if is_active:
self._active = True
self.emit('active')
if cascade:
self.parent._update_active()
for testMethod in self.values():
testMethod.set_active(True, cascade=False)
def toggle_active(self):
self.set_active(not self.active)
def find_tests(self, active=True, status=None, labels=None):
tests = []
count = 0
for testMethod_name, testMethod in self.items():
include = True
# If only active tests have been requested, the method
# must be active.
if active and not testMethod.active:
include = False
# If a list of statuses has been provided, the
# method status must be in that list.
if status and testMethod.status not in status:
include = False
# If a list of test labels has been provided, the method
# must be named explicitly
if labels and testMethod.path not in labels:
include = False
if include:
count = count + 1
tests.append(testMethod.path)
# If all the tests are included, then just reference the test case.
if len(self) == count:
return len(self), self.path
return count, tests
def _purge(self, timestamp):
"Purge any test method that isn't current as of the timestamp"
for testMethod_name, testMethod in self.items():
if testMethod.timestamp != timestamp:
self.pop(testMethod_name)
def _update_active(self):
"Check the active status of all child nodes, and update the status of this node accordingly"
for testMethod_name, testMethod in self.items():
if testMethod.active:
# As soon as we find an active child, this node
# must be marked active, and no other checks are
# required.
self.set_active(True)
return
self.set_active(False)
class TestModule(dict, EventSource):
def __init__(self, name, parent):
super(TestModule, self).__init__()
self.name = name
self._active = True
# Set the parent of the TestModule.
self.parent = parent
self.parent[name] = self
# Announce that there is a new test case
self.emit('new')
def __repr__(self):
return u'TestModule %s' % self.path
@property
def path(self):
"The dotted-path name that identifies this app to the test runner"
if self.parent.path:
return u'%s.%s' % (self.parent.path, self.name)
return self.name
@property
def active(self):
"Is this test method currently active?"
return self._active
def set_active(self, is_active, cascade=True):
if self._active:
if not is_active:
self._active = False
self.emit('inactive')
if cascade:
self.parent._update_active()
for testModule in self.values():
testModule.set_active(False, cascade=False)
else:
if is_active:
self._active = True
self.emit('active')
if cascade:
self.parent._update_active()
for testModule in self.values():
testModule.set_active(True, cascade=False)
def toggle_active(self):
self.set_active(not self.active)
def find_tests(self, active=True, status=None, labels=None):
tests = []
count = 0
found_partial = False
for testModule_name, testModule in self.items():
include = True
# If only active tests have been requested, the module
# must be active.
if active and not testModule.active:
include = False
# If a list of test labels has been provided, either the
# module, or a test *in* the module, must be named explicitly.
if labels:
if testModule.path in labels:
# The module is named explicitly. Include all active
# subtests of this module
subcount, subtests = testModule.find_tests(True, status)
else:
# The module isn't named. Look for all subtests.
# Search for subtests that match.
subcount, subtests = testModule.find_tests(active, status, labels)
else:
subcount, subtests = testModule.find_tests(active, status)
if include:
count = count + subcount
if isinstance(subtests, list):
found_partial = True
tests.extend(subtests)
else:
tests.append(subtests)
# No partials found; just reference the app.
if not found_partial:
return count, self.path
return count, tests
def _purge(self, timestamp):
for testModule_name, testModule in self.items():
testModule._purge(timestamp)
if len(testModule) == 0:
self.pop(testModule_name)
def _update_active(self):
"Check the active status of all child nodes, and update the status of this node accordingly"
for subModule_name, subModule in self.items():
if subModule.active:
self.set_active(True)
return
self.set_active(False)
class Project(dict, EventSource):
"""A data representation of an project, containing 1+ test apps.
"""
def __init__(self):
super(Project, self).__init__()
self.errors = []
def __repr__(self):
return u'Project'
@property
def path(self):
return ''
def find_tests(self, active=True, status=None, labels=None):
tests = []
count = 0
found_partial = False
for testApp_name, testApp in self.items():
include = True
# If only active tests have been requested, the module
# must be active.
if active and not testApp.active:
include = False
# If a list of test labels has been provided, either the
# module, or a test *in* the module, must be named explicitly.
if labels:
if testApp.path in labels:
# The module is named explicitly. Include all active
# subtests of this module
subcount, subtests = testApp.find_tests(True, status)
else:
# The module isn't named. Look for all subtests.
# Search for subtests that match.
subcount, subtests = testApp.find_tests(active, status, labels)
else:
subcount, subtests = testApp.find_tests(active, status)
if include:
count = count + subcount
if isinstance(subtests, list):
found_partial = True
tests.extend(subtests)
else:
tests.append(subtests)
# No partials found; just reference the app.
if not found_partial:
return count, []
return count, tests
def confirm_exists(self, test_label, timestamp=None):
parts = test_label.split('.')
if len(parts) < 2:
return
parentModule = self
for testModule_name in parts[:-2]:
try:
testModule = parentModule[testModule_name]
except KeyError:
testModule = TestModule(testModule_name, parentModule)
parentModule = testModule
try:
testCase = parentModule[parts[-2]]
except KeyError:
testCase = TestCase(parts[-2], parentModule)
try:
testMethod = testCase[parts[-1]]
except KeyError:
testMethod = TestMethod(parts[-1], testCase)
testMethod.timestamp = timestamp
return testMethod
def refresh(self, test_list, errors=None):
timestamp = datetime.now()
# Make sure there is a data representation for every test in the list.
for test_label in test_list:
self.confirm_exists(test_label, timestamp)
for testModule_name, testModule in self.items():
testModule._purge(timestamp)
if len(testModule) == 0:
self.pop(testModule_name)
self.errors = errors if errors is not None else []
def _update_active(self):
"Exists for API consistency"
pass
class UnittestProject(Project):
def __init__(self):
super(UnittestProject, self).__init__()
def discover_commandline(self, testdir='.'):
"Command line: Discover all available tests in a project."
return [sys.executable, 'discover.py', '--testdir', testdir]
def execute_commandline(self, labels, testdir='.'):
"Return the command line to execute the specified test labels"
args = [sys.executable, 'runner.py', '--testdir', testdir]
return args + labels