Chih-Yu Huang | 08c6e9d | 2015-11-25 19:06:07 +0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # Copyright 2015 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
cyueh | dbaec98 | 2019-12-09 13:20:38 +0800 | [diff] [blame] | 7 | from __future__ import print_function |
| 8 | |
Chih-Yu Huang | 8c76e30 | 2016-02-18 19:03:55 +0800 | [diff] [blame] | 9 | import logging |
Chih-Yu Huang | 08c6e9d | 2015-11-25 19:06:07 +0800 | [diff] [blame] | 10 | import os |
| 11 | import sys |
| 12 | import tempfile |
| 13 | import time |
| 14 | import unittest |
| 15 | |
| 16 | WORKING_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 17 | |
| 18 | |
| 19 | def PassMessage(message): |
cyueh | dbaec98 | 2019-12-09 13:20:38 +0800 | [diff] [blame] | 20 | print('\033[22;32m%s\033[22;0m' % message) |
Chih-Yu Huang | 08c6e9d | 2015-11-25 19:06:07 +0800 | [diff] [blame] | 21 | |
| 22 | |
| 23 | def FailMessage(message): |
cyueh | dbaec98 | 2019-12-09 13:20:38 +0800 | [diff] [blame] | 24 | print('\033[22;31m%s\033[22;0m' % message) |
Chih-Yu Huang | 08c6e9d | 2015-11-25 19:06:07 +0800 | [diff] [blame] | 25 | |
| 26 | |
| 27 | def LogFilePath(dir_name, filename): |
| 28 | return os.path.join(dir_name, filename.replace('/', '_') + '.log') |
| 29 | |
| 30 | |
| 31 | def main(argv): |
Chih-Yu Huang | 8c76e30 | 2016-02-18 19:03:55 +0800 | [diff] [blame] | 32 | logging.disable(logging.CRITICAL) # Ignore all logging output from unittest. |
Chih-Yu Huang | 08c6e9d | 2015-11-25 19:06:07 +0800 | [diff] [blame] | 33 | temp_dir = tempfile.mkdtemp() |
| 34 | total_tests = 0 |
| 35 | passed_tests = 0 |
| 36 | failed_tests = [] |
| 37 | for filename in argv[1:]: |
| 38 | if not os.path.isfile(filename): |
| 39 | FailMessage('File is not found: %s' % filename) |
| 40 | continue |
| 41 | if not filename.endswith('.py'): |
| 42 | FailMessage('File is not python file: %s' % filename) |
| 43 | continue |
| 44 | filename = os.path.relpath(filename, WORKING_DIR) |
| 45 | module_name = filename[:-3].replace('/', '.') |
| 46 | |
| 47 | suite = unittest.defaultTestLoader.loadTestsFromName(module_name) |
| 48 | start_time = time.time() |
| 49 | log_file = LogFilePath(temp_dir, filename) |
cyueh | dbaec98 | 2019-12-09 13:20:38 +0800 | [diff] [blame] | 50 | with open(log_file, 'w') as stream: |
| 51 | result = unittest.TextTestRunner(stream=stream).run(suite) |
Chih-Yu Huang | 08c6e9d | 2015-11-25 19:06:07 +0800 | [diff] [blame] | 52 | duration = time.time() - start_time |
| 53 | if result.wasSuccessful(): |
| 54 | PassMessage('*** PASS [%.2f s] %s' % (duration, filename)) |
| 55 | passed_tests += 1 |
| 56 | else: |
| 57 | FailMessage('*** FAIL [%.2f s] %s' % (duration, filename)) |
| 58 | failed_tests.append(filename) |
| 59 | total_tests += 1 |
| 60 | |
cyueh | dbaec98 | 2019-12-09 13:20:38 +0800 | [diff] [blame] | 61 | print('%d/%d tests passed' % (passed_tests, total_tests)) |
Chih-Yu Huang | 08c6e9d | 2015-11-25 19:06:07 +0800 | [diff] [blame] | 62 | if failed_tests: |
cyueh | dbaec98 | 2019-12-09 13:20:38 +0800 | [diff] [blame] | 63 | print('') |
| 64 | print('Logs of %d failed tests:' % len(failed_tests)) |
Chih-Yu Huang | 08c6e9d | 2015-11-25 19:06:07 +0800 | [diff] [blame] | 65 | for failed_test in failed_tests: |
| 66 | FailMessage(LogFilePath(temp_dir, failed_test)) |
cyueh | dbaec98 | 2019-12-09 13:20:38 +0800 | [diff] [blame] | 67 | print('To re-test failed unittests, run:') |
| 68 | print('make test UNITTEST_WHITELIST="%s"' % ' '.join(failed_tests)) |
Chih-Yu Huang | 08c6e9d | 2015-11-25 19:06:07 +0800 | [diff] [blame] | 69 | |
| 70 | |
| 71 | if __name__ == '__main__': |
| 72 | main(sys.argv) |