OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import argparse | |
7 import json | |
8 import os | 6 import os |
9 import sys | 7 import sys |
10 import time | 8 |
11 import unittest | 9 _script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 10 sys.path.insert(0, os.path.join(_script_dir, "pylib")) |
| 11 |
| 12 from mojo_python_tests_runner import MojoPythonTestRunner |
12 | 13 |
13 | 14 |
14 def main(): | 15 def main(): |
15 parser = argparse.ArgumentParser() | 16 runner = MojoPythonTestRunner(os.path.join('mojo', 'public', 'tools', |
16 parser.usage = 'run_mojo_python_tests.py [options] [tests...]' | 17 'bindings', 'pylib')) |
17 parser.add_argument('-v', '--verbose', action='count', default=0) | 18 sys.exit(runner.run()) |
18 parser.add_argument('--metadata', action='append', default=[], | |
19 help=('optional key=value metadata that will be stored ' | |
20 'in the results files (can be used for revision ' | |
21 'numbers, etc.)')) | |
22 parser.add_argument('--write-full-results-to', metavar='FILENAME', | |
23 action='store', | |
24 help='path to write the list of full results to.') | |
25 parser.add_argument('tests', nargs='*') | |
26 | |
27 args = parser.parse_args() | |
28 | |
29 bad_metadata = False | |
30 for val in args.metadata: | |
31 if '=' not in val: | |
32 print >> sys.stderr, ('Error: malformed metadata "%s"' % val) | |
33 bad_metadata = True | |
34 if bad_metadata: | |
35 print >> sys.stderr | |
36 parser.print_help() | |
37 return 2 | |
38 | |
39 chromium_src_dir = os.path.join(os.path.dirname(__file__), | |
40 os.pardir, | |
41 os.pardir) | |
42 | |
43 loader = unittest.loader.TestLoader() | |
44 print "Running Python unit tests under mojo/public/tools/bindings/pylib ..." | |
45 | |
46 pylib_dir = os.path.join(chromium_src_dir, 'mojo', 'public', | |
47 'tools', 'bindings', 'pylib') | |
48 if args.tests: | |
49 if pylib_dir not in sys.path: | |
50 sys.path.append(pylib_dir) | |
51 suite = unittest.TestSuite() | |
52 for test_name in args.tests: | |
53 suite.addTests(loader.loadTestsFromName(test_name)) | |
54 else: | |
55 suite = loader.discover(pylib_dir, pattern='*_unittest.py') | |
56 | |
57 runner = unittest.runner.TextTestRunner(verbosity=(args.verbose + 1)) | |
58 result = runner.run(suite) | |
59 | |
60 full_results = _FullResults(suite, result, args.metadata) | |
61 if args.write_full_results_to: | |
62 with open(args.write_full_results_to, 'w') as fp: | |
63 json.dump(full_results, fp, indent=2) | |
64 fp.write("\n") | |
65 | |
66 return 0 if result.wasSuccessful() else 1 | |
67 | |
68 | |
69 TEST_SEPARATOR = '.' | |
70 | |
71 | |
72 def _FullResults(suite, result, metadata): | |
73 """Convert the unittest results to the Chromium JSON test result format. | |
74 | |
75 This matches run-webkit-tests (the layout tests) and the flakiness dashboard. | |
76 """ | |
77 | |
78 full_results = {} | |
79 full_results['interrupted'] = False | |
80 full_results['path_delimiter'] = TEST_SEPARATOR | |
81 full_results['version'] = 3 | |
82 full_results['seconds_since_epoch'] = time.time() | |
83 for md in metadata: | |
84 key, val = md.split('=', 1) | |
85 full_results[key] = val | |
86 | |
87 all_test_names = _AllTestNames(suite) | |
88 failed_test_names = _FailedTestNames(result) | |
89 | |
90 full_results['num_failures_by_type'] = { | |
91 'FAIL': len(failed_test_names), | |
92 'PASS': len(all_test_names) - len(failed_test_names), | |
93 } | |
94 | |
95 full_results['tests'] = {} | |
96 | |
97 for test_name in all_test_names: | |
98 value = {} | |
99 value['expected'] = 'PASS' | |
100 if test_name in failed_test_names: | |
101 value['actual'] = 'FAIL' | |
102 value['is_unexpected'] = True | |
103 else: | |
104 value['actual'] = 'PASS' | |
105 _AddPathToTrie(full_results['tests'], test_name, value) | |
106 | |
107 return full_results | |
108 | |
109 | |
110 def _AllTestNames(suite): | |
111 test_names = [] | |
112 # _tests is protected pylint: disable=W0212 | |
113 for test in suite._tests: | |
114 if isinstance(test, unittest.suite.TestSuite): | |
115 test_names.extend(_AllTestNames(test)) | |
116 else: | |
117 test_names.append(test.id()) | |
118 return test_names | |
119 | |
120 | |
121 def _FailedTestNames(result): | |
122 return set(test.id() for test, _ in result.failures + result.errors) | |
123 | |
124 | |
125 def _AddPathToTrie(trie, path, value): | |
126 if TEST_SEPARATOR not in path: | |
127 trie[path] = value | |
128 return | |
129 directory, rest = path.split(TEST_SEPARATOR, 1) | |
130 if directory not in trie: | |
131 trie[directory] = {} | |
132 _AddPathToTrie(trie[directory], rest, value) | |
133 | 19 |
134 | 20 |
135 if __name__ == '__main__': | 21 if __name__ == '__main__': |
136 sys.exit(main()) | 22 sys.exit(main()) |
OLD | NEW |