Skip to content

Commit 63062bd

Browse files
committed
[tests] color test results and sort alphabetically
1 parent c7e73ea commit 63062bd

File tree

1 file changed

+61
-25
lines changed

1 file changed

+61
-25
lines changed

test/functional/test_runner.py

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
import re
2626
import logging
2727

28+
BOLD = ("", "")
29+
if os.name == 'posix':
30+
# primitive formatting on supported
31+
# terminal via ANSI escape sequences:
32+
BOLD = ('\033[0m', '\033[1m')
33+
2834
TEST_EXIT_PASSED = 0
2935
TEST_EXIT_SKIPPED = 77
3036

@@ -231,11 +237,6 @@ def main():
231237
run_tests(test_list, config["environment"]["SRCDIR"], config["environment"]["BUILDDIR"], config["environment"]["EXEEXT"], args.jobs, args.coverage, passon_args)
232238

233239
def run_tests(test_list, src_dir, build_dir, exeext, jobs=1, enable_coverage=False, args=[]):
234-
BOLD = ("","")
235-
if os.name == 'posix':
236-
# primitive formatting on supported
237-
# terminal via ANSI escape sequences:
238-
BOLD = ('\033[0m', '\033[1m')
239240

240241
#Set env vars
241242
if "BITCOIND" not in os.environ:
@@ -258,42 +259,54 @@ def run_tests(test_list, src_dir, build_dir, exeext, jobs=1, enable_coverage=Fal
258259
subprocess.check_output([tests_dir + 'create_cache.py'] + flags)
259260

260261
#Run Tests
261-
all_passed = True
262-
time_sum = 0
263-
time0 = time.time()
264-
265262
job_queue = TestHandler(jobs, tests_dir, test_list, flags)
263+
time0 = time.time()
264+
test_results = []
266265

267266
max_len_name = len(max(test_list, key=len))
268-
results = "\n" + BOLD[1] + "%s | %s | %s\n\n" % ("TEST".ljust(max_len_name), "STATUS ", "DURATION") + BOLD[0]
267+
269268
for _ in range(len(test_list)):
270-
(name, stdout, stderr, status, duration) = job_queue.get_next()
271-
all_passed = all_passed and status != "Failed"
272-
time_sum += duration
273-
274-
if status == "Passed":
275-
logging.debug("\n%s%s%s passed, Duration: %s s" % (BOLD[1], name, BOLD[0], duration))
276-
elif status == "Skipped":
277-
logging.debug("\n%s%s%s skipped" % (BOLD[1], name, BOLD[0]))
269+
test_result, stdout, stderr = job_queue.get_next()
270+
test_results.append(test_result)
271+
272+
if test_result.status == "Passed":
273+
logging.debug("\n%s%s%s passed, Duration: %s s" % (BOLD[1], test_result.name, BOLD[0], test_result.time))
274+
elif test_result.status == "Skipped":
275+
logging.debug("\n%s%s%s skipped" % (BOLD[1], test_result.name, BOLD[0]))
278276
else:
279-
print("\n%s%s%s failed, Duration: %s s\n" % (BOLD[1], name, BOLD[0], duration))
277+
print("\n%s%s%s failed, Duration: %s s\n" % (BOLD[1], test_result.name, BOLD[0], test_result.time))
280278
print(BOLD[1] + 'stdout:\n' + BOLD[0] + stdout + '\n')
281279
print(BOLD[1] + 'stderr:\n' + BOLD[0] + stderr + '\n')
282280

283-
results += "%s | %s | %s s\n" % (name.ljust(max_len_name), status.ljust(7), duration)
284-
285-
results += BOLD[1] + "\n%s | %s | %s s (accumulated)" % ("ALL".ljust(max_len_name), str(all_passed).ljust(7), time_sum) + BOLD[0]
286-
print(results)
287-
print("\nRuntime: %s s" % (int(time.time() - time0)))
281+
print_results(test_results, max_len_name, (int(time.time() - time0)))
288282

289283
if coverage:
290284
coverage.report_rpc_coverage()
291285

292286
logging.debug("Cleaning up coverage data")
293287
coverage.cleanup()
294288

289+
all_passed = all(map(lambda test_result: test_result.status == "Passed", test_results))
290+
295291
sys.exit(not all_passed)
296292

293+
def print_results(test_results, max_len_name, runtime):
294+
results = "\n" + BOLD[1] + "%s | %s | %s\n\n" % ("TEST".ljust(max_len_name), "STATUS ", "DURATION") + BOLD[0]
295+
296+
test_results.sort(key=lambda result: result.name.lower())
297+
all_passed = True
298+
time_sum = 0
299+
300+
for test_result in test_results:
301+
all_passed = all_passed and test_result.status != "Failed"
302+
time_sum += test_result.time
303+
test_result.padding = max_len_name
304+
results += str(test_result)
305+
306+
results += BOLD[1] + "\n%s | %s | %s s (accumulated) \n" % ("ALL".ljust(max_len_name), str(all_passed).ljust(7), time_sum) + BOLD[0]
307+
results += "Runtime: %s s\n" % (runtime)
308+
print(results)
309+
297310
class TestHandler:
298311
"""
299312
Trigger the testscrips passed in via the list.
@@ -348,9 +361,32 @@ def get_next(self):
348361
status = "Failed"
349362
self.num_running -= 1
350363
self.jobs.remove(j)
351-
return name, stdout, stderr, status, int(time.time() - time0)
364+
365+
return TestResult(name, status, int(time.time() - time0)), stdout, stderr
352366
print('.', end='', flush=True)
353367

368+
class TestResult():
369+
def __init__(self, name, status, time):
370+
self.name = name
371+
self.status = status
372+
self.time = time
373+
self.padding = 0
374+
375+
def __repr__(self):
376+
COLOR = ("", "")
377+
if os.name == 'posix':
378+
# primitive formatting on supported
379+
# terminal via ANSI escape sequences:
380+
if self.status == "Passed":
381+
COLOR = ('\033[0m', '\033[0;34m')
382+
elif self.status == "Failed":
383+
COLOR = ('\033[0m', '\033[0;31m')
384+
elif self.status == "Skipped":
385+
COLOR = ('\033[0m', '\033[1;30m')
386+
387+
return COLOR[1] + "%s | %s | %s s\n" % (self.name.ljust(self.padding), self.status.ljust(7), self.time) + COLOR[0]
388+
389+
354390
def check_script_list(src_dir):
355391
"""Check scripts directory.
356392

0 commit comments

Comments
 (0)