OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 """ | |
6 This script runs tests to verify that the perf tools are working. | |
7 """ | |
8 import fnmatch | |
9 import logging | |
10 import optparse | |
11 import os | |
12 import sys | |
13 import traceback | |
14 import unittest | |
15 | |
16 def Discover(start_dir, pattern = 'test*.py', top_level_dir = None): | |
17 if hasattr(unittest.defaultTestLoader, 'discover'): | |
18 return unittest.defaultTestLoader.discover( # pylint: disable=E1101 | |
19 start_dir, | |
20 pattern, | |
21 top_level_dir) | |
22 | |
23 modules = [] | |
24 for dirpath, _, filenames in os.walk(start_dir): | |
25 for filename in filenames: | |
26 if not filename.endswith('.py'): | |
27 continue | |
28 | |
29 if not fnmatch.fnmatch(filename, pattern): | |
30 continue | |
31 | |
32 if filename.startswith('.') or filename.startswith('_'): | |
33 continue | |
34 name, _ = os.path.splitext(filename) | |
35 | |
36 relpath = os.path.relpath(dirpath, top_level_dir) | |
37 if relpath == '.': | |
38 fqn = name | |
39 else: | |
40 fqn = relpath.replace('/', '.') + '.' + name | |
41 | |
42 # load the module | |
43 try: | |
44 module = __import__(fqn, fromlist=[True]) | |
45 except Exception: | |
46 print 'While importing [%s]\n' % fqn | |
47 traceback.print_exc() | |
48 continue | |
49 modules.append(module) | |
50 | |
51 loader = unittest.defaultTestLoader | |
52 subsuites = [] | |
53 for module in modules: | |
54 if hasattr(module, 'suite'): | |
55 new_suite = module.suite() | |
56 else: | |
57 new_suite = loader.loadTestsFromModule(module) | |
58 if new_suite.countTestCases(): | |
59 subsuites.append(new_suite) | |
60 return unittest.TestSuite(subsuites) | |
61 | |
62 def FilterSuite(suite, predicate): | |
63 new_suite = unittest.TestSuite() | |
64 for x in suite: | |
65 if isinstance(x, unittest.TestSuite): | |
66 subsuite = FilterSuite(x, predicate) | |
67 if subsuite.countTestCases() == 0: | |
68 continue | |
69 | |
70 new_suite.addTest(subsuite) | |
71 continue | |
72 | |
73 assert isinstance(x, unittest.TestCase) | |
74 if predicate(x): | |
75 new_suite.addTest(x) | |
76 | |
77 return new_suite | |
78 | |
79 def DiscoverAndRunTests(dir_name, args, top_level_dir): | |
80 suite = Discover(dir_name, '*_unittest.py', top_level_dir) | |
81 | |
82 def IsTestSelected(test): | |
83 if len(args) != 0: | |
84 found = False | |
85 for name in args: | |
86 if name in test.id(): | |
87 found = True | |
88 if not found: | |
89 return False | |
90 | |
91 if hasattr(test, '_testMethodName'): | |
92 method = getattr(test, test._testMethodName) # pylint: disable=W0212 | |
93 # Test method filters go here. | |
94 | |
95 return True | |
96 | |
97 filtered_suite = FilterSuite(suite, IsTestSelected) | |
98 runner = unittest.TextTestRunner(verbosity = 2) | |
99 import chromeapp | |
100 chromeapp._unittests_running = True | |
101 try: | |
102 test_result = runner.run(filtered_suite) | |
103 finally: | |
104 chromeapp._unittests_running = False | |
105 return len(test_result.errors) + len(test_result.failures) | |
106 | |
107 def Main(args, start_dir, top_level_dir): | |
108 """Unit test suite that collects all test cases for telemetry.""" | |
109 parser = optparse.OptionParser('run_tests [options] [test names]') | |
110 parser.add_option( | |
111 '-v', '--verbose', action='count', dest='verbosity', | |
112 help='Increase verbosity level (repeat as needed)') | |
113 parser.add_option('--repeat-count', dest='run_test_repeat_count', | |
114 type='int', default=1, | |
115 help='Repeats each a provided number of times.') | |
116 | |
117 options, args = parser.parse_args(args) | |
118 | |
119 if options.verbosity == 0: | |
120 logging.getLogger().setLevel(logging.ERROR) | |
121 | |
122 olddir = os.getcwd() | |
123 num_errors = 0 | |
124 try: | |
125 os.chdir(top_level_dir) | |
126 for _ in range( | |
127 options.run_test_repeat_count): # pylint: disable=E1101 | |
128 num_errors += DiscoverAndRunTests(start_dir, args, top_level_dir) | |
129 finally: | |
130 os.chdir(olddir) | |
131 | |
132 return min(num_errors, 255) | |
133 | |
134 | |
135 if __name__ == '__main__': | |
136 top_level_dir = os.path.abspath( | |
137 os.path.dirname(__file__)) | |
138 start_dir = os.path.abspath('.') | |
139 ret = Main( | |
140 sys.argv[1:], start_dir, top_level_dir) | |
141 | |
142 sys.exit(ret) | |
OLD | NEW |