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