OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 import fnmatch | 4 import fnmatch |
5 import functools | |
6 import logging | 5 import logging |
7 import os | 6 import os |
8 import traceback | 7 import traceback |
9 import unittest | 8 import unittest |
10 | 9 |
11 from chrome_remote_control import browser_options | 10 from chrome_remote_control import browser_options |
12 | 11 |
13 def RequiresBrowserOfType(*types): | 12 def RequiresBrowserOfType(*types): |
14 def wrap(func): | 13 def wrap(func): |
15 func._requires_browser_types = types | 14 func._requires_browser_types = types |
16 return func | 15 return func |
17 return wrap | 16 return wrap |
18 | 17 |
19 def Discover(start_dir, pattern = 'test*.py', top_level_dir = None): | 18 def Discover(start_dir, pattern = 'test*.py', top_level_dir = None): |
20 if hasattr(unittest.defaultTestLoader, 'discover'): | 19 if hasattr(unittest.defaultTestLoader, 'discover'): |
21 return unittest.defaultTestLoader.discover(start_dir, | 20 return unittest.defaultTestLoader.discover( # pylint: disable=E1101 |
22 pattern, | 21 start_dir, |
23 top_level_dir) | 22 pattern, |
| 23 top_level_dir) |
24 | 24 |
25 modules = [] | 25 modules = [] |
26 for dirpath, _, filenames in os.walk(start_dir): | 26 for dirpath, _, filenames in os.walk(start_dir): |
27 for filename in filenames: | 27 for filename in filenames: |
28 if not filename.endswith('.py'): | 28 if not filename.endswith('.py'): |
29 continue | 29 continue |
30 | 30 |
31 if not fnmatch.fnmatch(filename, pattern): | 31 if not fnmatch.fnmatch(filename, pattern): |
32 continue | 32 continue |
33 | 33 |
34 if filename.startswith('.') or filename.startswith('_'): | 34 if filename.startswith('.') or filename.startswith('_'): |
35 continue | 35 continue |
36 name, _ = os.path.splitext(filename) | 36 name, _ = os.path.splitext(filename) |
37 | 37 |
38 relpath = os.path.relpath(dirpath, top_level_dir) | 38 relpath = os.path.relpath(dirpath, top_level_dir) |
39 fqn = relpath.replace('/', '.') + '.' + name | 39 fqn = relpath.replace('/', '.') + '.' + name |
40 | 40 |
41 # load the module | 41 # load the module |
42 try: | 42 try: |
43 module = __import__(fqn, fromlist=[True]) | 43 module = __import__(fqn, fromlist=[True]) |
44 except: | 44 except Exception: |
45 print 'While importing [%s]\n' % fqn | 45 print 'While importing [%s]\n' % fqn |
46 traceback.print_exc() | 46 traceback.print_exc() |
47 continue | 47 continue |
48 modules.append(module) | 48 modules.append(module) |
49 | 49 |
50 loader = unittest.defaultTestLoader | 50 loader = unittest.defaultTestLoader |
51 subsuites = [] | 51 subsuites = [] |
52 for module in modules: | 52 for module in modules: |
53 if hasattr(module, 'suite'): | 53 if hasattr(module, 'suite'): |
54 new_suite = module.suite() | 54 new_suite = module.suite() |
(...skipping 26 matching lines...) Expand all Loading... |
81 def IsTestSelected(test): | 81 def IsTestSelected(test): |
82 if len(args) != 0: | 82 if len(args) != 0: |
83 found = False | 83 found = False |
84 for name in args: | 84 for name in args: |
85 if name in test.id(): | 85 if name in test.id(): |
86 found = True | 86 found = True |
87 if not found: | 87 if not found: |
88 return False | 88 return False |
89 | 89 |
90 if hasattr(test, '_testMethodName'): | 90 if hasattr(test, '_testMethodName'): |
91 method = getattr(test, test._testMethodName) | 91 method = getattr(test, test._testMethodName) # pylint: disable=W0212 |
92 if hasattr(method, '_requires_browser_types'): | 92 if hasattr(method, '_requires_browser_types'): |
93 types = method._requires_browser_types | 93 types = method._requires_browser_types # pylint: disable=W0212 |
94 if browser_options.browser_type_for_unittests not in types: | 94 if browser_options.browser_type_for_unittests not in types: |
95 logging.debug('Skipping test %s because it requires %s' % | 95 logging.debug('Skipping test %s because it requires %s' % |
96 (test.id(), types)) | 96 (test.id(), types)) |
97 return False | 97 return False |
98 | 98 |
99 return True | 99 return True |
100 | 100 |
101 filtered_suite = FilterSuite(suite, IsTestSelected) | 101 filtered_suite = FilterSuite(suite, IsTestSelected) |
102 runner = unittest.TextTestRunner(verbosity = 2) | 102 runner = unittest.TextTestRunner(verbosity = 2) |
103 test_result = runner.run(filtered_suite) | 103 test_result = runner.run(filtered_suite) |
(...skipping 18 matching lines...) Expand all Loading... |
122 default_options.browser_type) | 122 default_options.browser_type) |
123 logging.error('Re-run with --browser=list to see available browser types.') | 123 logging.error('Re-run with --browser=list to see available browser types.') |
124 return 1 | 124 return 1 |
125 | 125 |
126 browser_options.options_for_unittests = default_options | 126 browser_options.options_for_unittests = default_options |
127 browser_options.browser_type_for_unittests = browser_to_create.browser_type | 127 browser_options.browser_type_for_unittests = browser_to_create.browser_type |
128 olddir = os.getcwd() | 128 olddir = os.getcwd() |
129 num_errors = 0 | 129 num_errors = 0 |
130 try: | 130 try: |
131 os.chdir(top_level_dir) | 131 os.chdir(top_level_dir) |
132 for _ in range(default_options.run_test_repeat_count): | 132 for _ in range( |
| 133 default_options.run_test_repeat_count): # pylint: disable=E1101 |
133 num_errors += DiscoverAndRunTests(start_dir, args, top_level_dir) | 134 num_errors += DiscoverAndRunTests(start_dir, args, top_level_dir) |
134 finally: | 135 finally: |
135 os.chdir(olddir) | 136 os.chdir(olddir) |
136 browser_options.options_for_unittests = None | 137 browser_options.options_for_unittests = None |
137 | 138 |
138 return num_errors | 139 return num_errors |
OLD | NEW |