forked from tfio/pytest-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.py
362 lines (295 loc) · 13 KB
/
runner.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
import json
import subprocess
import sys
from threading import Thread
try:
from Queue import Queue, Empty
except ImportError:
from queue import Queue, Empty # python 3.x
from events import EventSource
from model import TestMethod
import pipes
def enqueue_output(out, queue):
"""A utility method for consuming piped output from a subprocess.
Reads content from `out` one line at a time, and puts it onto
queue for consumption in a separate thread.
"""
for line in iter(out.readline, b''):
queue.put(line.strip().decode('utf-8'))
out.close()
def parse_status_and_error(post):
if post['status'] == 'OK':
status = TestMethod.STATUS_PASS
error = None
elif post['status'] == 's':
status = TestMethod.STATUS_SKIP
error = 'Skipped: ' + post.get('error')
elif post['status'] == 'F':
status = TestMethod.STATUS_FAIL
error = post.get('error')
elif post['status'] == 'x':
status = TestMethod.STATUS_EXPECTED_FAIL
error = post.get('error')
elif post['status'] == 'u':
status = TestMethod.STATUS_UNEXPECTED_SUCCESS
error = None
elif post['status'] == 'E':
status = TestMethod.STATUS_ERROR
error = post.get('error')
return status, error
class Runner(EventSource):
"A wrapper around the subprocess that executes tests."
def __init__(self, project, count, labels, testdir):
self.project = project
self.proc = subprocess.Popen(
self.project.execute_commandline(labels, testdir),
stdin=None,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=False,
bufsize=1,
close_fds='posix' in sys.builtin_module_names
)
# Piped stdout/stderr reads are blocking; therefore, we need to
# do all our readline calls in a background thread, and use a
# queue object to store lines that have been read.
self.stdout = Queue()
t = Thread(target=enqueue_output, args=(self.proc.stdout, self.stdout))
t.daemon = True
t.start()
self.stderr = Queue()
t = Thread(target=enqueue_output, args=(self.proc.stderr, self.stderr))
t.daemon = True
t.start()
# The TestMethod object currently under execution.
self.current_test = None
# An accumulator of ouput from the tests. If buffer is None,
# then the test suite isn't currently running - it's in suite
# setup/teardown.
self.buffer = None
# An accumulator for error output from the tests.
self.error_buffer = []
# The timestamp when current_test started
self.start_time = None
# The total count of tests under execution
self.total_count = count
# The count of tests that have been executed.
self.completed_count = 0
# The count of specific test results.
self.result_count = {}
@property
def is_running(self):
"Return True if this runner currently running."
return self.proc.poll() is None
@property
def any_failed(self):
return sum(self.result_count.get(state, 0) for state in TestMethod.FAILING_STATES)
def terminate(self):
"Stop the executor."
self.proc.terminate()
def poll(self):
"Poll the runner looking for new test output"
stopped = False
finished = False
# Read from stdout, building a buffer.
lines = []
try:
while True:
lines.append(self.stdout.get(block=False))
except Empty:
# queue.get() raises an exception when the queue is empty.
# This means there is no more output to consume at this time.
pass
# Read from stderr, building a buffer.
try:
while True:
self.error_buffer.append(self.stderr.get(block=False))
except Empty:
# queue.get() raises an exception when the queue is empty.
# This means there is no more output to consume at this time.
pass
# Check to see if the subprocess is still running.
# If it isn't, raise an error.
if self.proc is None:
stopped = True
elif self.proc.poll() is not None:
stopped = True
# Process all the full lines that are available
for line in lines:
# Look for a separator.
if line in (pipes.PipedTestResult.RESULT_SEPARATOR, pipes.PipedTestRunner.START_TEST_RESULTS, pipes.PipedTestRunner.END_TEST_RESULTS):
if self.buffer is None:
# Preamble is finished. Set up the line buffer.
self.buffer = []
else:
# Start of new test result; record the last result
# Then, work out what content goes where.
pre = json.loads(self.buffer[0])
if len(self.buffer) == 2:
# No subtests are present, or only one subtest
post = json.loads(self.buffer[1])
status, error = parse_status_and_error(post)
else:
# We have subtests; capture the most important status (until we can capture all the statuses)
status = TestMethod.STATUS_PASS # Assume pass until told otherwise
error = ''
for line_num in range(1, len(self.buffer)):
post = json.loads(self.buffer[line_num])
subtest_status, subtest_error = parse_status_and_error(post)
if subtest_status > status:
status = subtest_status
if subtest_error:
error += subtest_error + '\n\n'
# Increase the count of executed tests
self.completed_count = self.completed_count + 1
# Get the start and end times for the test
start_time = float(pre['start_time'])
end_time = float(post['end_time'])
self.current_test.description = post['description']
self.current_test.set_result(
status=status,
output=post.get('output'),
error=error,
duration=end_time - start_time,
)
# Work out how long the suite has left to run (approximately)
if self.start_time is None:
self.start_time = start_time
total_duration = end_time - self.start_time
time_per_test = total_duration / self.completed_count
remaining_time = (self.total_count - self.completed_count) * time_per_test
if remaining_time > 4800:
remaining = '%s hours' % int(remaining_time / 2400)
elif remaining_time > 2400:
remaining = '%s hour' % int(remaining_time / 2400)
elif remaining_time > 120:
remaining = '%s mins' % int(remaining_time / 60)
elif remaining_time > 60:
remaining = '%s min' % int(remaining_time / 60)
else:
remaining = '%ss' % int(remaining_time)
# Update test result counts
self.result_count.setdefault(status, 0)
self.result_count[status] = self.result_count[status] + 1
# Notify the display to update.
self.emit('test_end', test_path=self.current_test.path, result=status, remaining_time=remaining)
# Clear the decks for the next test.
self.current_test = None
self.buffer = []
if line == pipes.PipedTestRunner.END_TEST_RESULTS:
# End of test execution.
# Mark the runner as finished, and move back
# to a pre-test state in the results.
finished = True
self.buffer = None
else:
# Not a separator line, so it's actual content.
if self.buffer is None:
# Suite isn't running yet - just display the output
# as a status update line.
self.emit('test_status_update', update=line)
else:
# Suite is running - have we got an active test?
# Doctest (and some other tools) output invisible escape sequences.
# Strip these if they exist.
if line.startswith('\x1b'):
line = line[line.find('{'):]
# Store the cleaned buffer
self.buffer.append(line)
# If we don't have an currently active test, this line will
# contain the path for the test.
if self.current_test is None:
try:
# No active test; first line tells us which test is running.
pre = json.loads(line)
except ValueError:
self.emit('suit_end')
return True
self.current_test = self.project.confirm_exists(pre['path'])
self.emit('test_start', test_path=pre['path'])
# If we're not finished, requeue the event.
if finished:
if self.error_buffer:
self.emit('suite_end', error='\n'.join(self.error_buffer))
else:
self.emit('suite_end')
return False
elif stopped:
# Suite has stopped producing output.
if self.error_buffer:
self.emit('suite_error', error='\n'.join(self.error_buffer))
else:
self.emit('suite_error', error='Test output ended unexpectedly')
# Suite has finished; don't requeue
return False
else:
# Still running - requeue event.
return True
import argparse
import unittest
class PyTestExecutor(object):
'''
This is a thing which, when run, produces a stream
of well-formed test result outputs. Its processing is
initiated by the top-level Runner class
'''
def __init__(self):
# Allows the executor to run a specified list of tests
self.specified_list = None
def flatten_results(self, iterable):
input = list(iterable)
while input:
item = input.pop(0)
try:
data = iter(item)
input = list(data) + input
except:
yield item
def run_only(self, specified_list):
self.specified_list = specified_list
def stream_suite(self, suite):
print ("Calling stream_suite: " + str(suite))
pipes.PipedTestRunner().run(suite)
def stream_results(self, testdir=None):
if testdir is None:
testdir = '.'
loader = unittest.TestLoader()
tests = loader.discover(testdir)
flat_tests = list(self.flatten_results(tests))
if not self.specified_list:
suite = loader.discover(testdir)
self.stream_suite(suite)
else:
suite = unittest.TestSuite()
# Add individual test cases.
for test in flat_tests:
if test.id() in self.specified_list:
suite.addTest(test)
# Add all tests in a file.
for specified in self.specified_list:
if specified.count('.') == 0:
for test in flat_tests:
module_name = test.id()[0:test.id().index('.')]
if specified == module_name:
suite.addTest(test)
# Add all tests in a class within a file.
for specified in self.specified_list:
if specified.count('.') == 1:
for test in flat_tests:
module_name = test.id()[0:test.id().rindex('.')]
if specified == module_name:
suite.addTest(test)
self.stream_suite(suite)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--testdir', dest='testdir', default='.', help='Directory to choose tests from')
parser.add_argument('labels', nargs=argparse.REMAINDER, help='Test labels to run.')
options = parser.parse_args()
executor = PyTestExecutor()
# options.labels = list()
# options.labels.append('test_acquire.TestAcquire.test_print_1')
if options.labels is not None:
print('Labels: ', options.labels)
if options.labels:
executor.run_only(options.labels)
executor.stream_results(options.testdir)