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 | 4 |
5 """Runs the Python tests (relies on using the Java test runner).""" | 5 """Setup for instrumentation host-driven tests.""" |
6 | 6 |
7 import logging | 7 import logging |
8 import os | 8 import os |
9 import sys | 9 import sys |
10 import types | 10 import types |
11 | 11 |
12 from pylib import android_commands | 12 from instrumentation_test_case import InstrumentationHostDrivenTestCase |
13 from pylib.base import base_test_result | 13 import test_info_collection |
14 from pylib.instrumentation import test_package | 14 import test_runner |
15 from pylib.instrumentation import test_runner | |
16 from pylib.utils import report_results | |
17 | |
18 import python_test_base | |
19 from python_test_sharder import PythonTestSharder | |
20 from test_info_collection import TestInfoCollection | |
21 | 15 |
22 | 16 |
23 def _GetPythonFiles(root, files): | 17 def _GetPythonFiles(root, files): |
24 """Returns all files from |files| that end in 'Test.py'. | 18 """Returns all files from |files| that end in 'Test.py'. |
25 | 19 |
26 Args: | 20 Args: |
27 root: A directory name with python files. | 21 root: A directory name with python files. |
28 files: A list of file names. | 22 files: A list of file names. |
29 | 23 |
30 Returns: | 24 Returns: |
31 A list with all Python driven test file paths. | 25 A list with all host-driven test case file paths. |
32 """ | 26 """ |
33 return [os.path.join(root, f) for f in files if f.endswith('Test.py')] | 27 return [os.path.join(root, f) for f in files if f.endswith('Test.py')] |
34 | 28 |
35 | 29 |
36 def _InferImportNameFromFile(python_file): | 30 def _InferImportNameFromFile(python_file): |
37 """Given a file, infer the import name for that file. | 31 """Given a file, infer the import name for that file. |
38 | 32 |
39 Example: /usr/foo/bar/baz.py -> baz. | 33 Example: /usr/foo/bar/baz.py -> baz. |
40 | 34 |
41 Args: | 35 Args: |
42 python_file: path to the Python file, ostensibly to import later. | 36 python_file: Path to the Python file, ostensibly to import later. |
43 | 37 |
44 Returns: | 38 Returns: |
45 The module name for the given file. | 39 The module name for the given file. |
46 """ | 40 """ |
47 return os.path.splitext(os.path.basename(python_file))[0] | 41 return os.path.splitext(os.path.basename(python_file))[0] |
48 | 42 |
49 | 43 |
50 def DispatchPythonTests(options): | 44 def _GetTestModules(host_driven_test_root, is_official_build): |
51 """Dispatches the Python tests. If there are multiple devices, use sharding. | 45 """Retrieve a sorted list of host-driven test case modules. |
52 | 46 |
53 Args: | 47 Walks the location of host-driven tests, imports them, and provides the list |
54 options: command line options. | |
55 | |
56 Returns: | |
57 A tuple of (base_test_result.TestRunResults object, exit code) | |
58 | |
59 Raises: | |
60 Exception: If there are no attached devices. | |
61 """ | |
62 | |
63 attached_devices = android_commands.GetAttachedDevices() | |
64 if not attached_devices: | |
65 raise Exception('You have no devices attached or visible!') | |
66 if options.test_device: | |
67 attached_devices = [options.test_device] | |
68 | |
69 test_collection = TestInfoCollection() | |
70 all_tests = _GetAllTests(options.python_test_root, options.official_build) | |
71 test_collection.AddTests(all_tests) | |
72 test_names = [t.qualified_name for t in all_tests] | |
73 logging.debug('All available tests: ' + str(test_names)) | |
74 | |
75 available_tests = test_collection.GetAvailableTests( | |
76 options.annotations, options.exclude_annotations, options.test_filter) | |
77 | |
78 if not available_tests: | |
79 logging.warning('No Python tests to run with current args.') | |
80 return (base_test_result.TestRunResults(), 0) | |
81 | |
82 test_names = [t.qualified_name for t in available_tests] | |
83 logging.debug('Final list of tests to run: ' + str(test_names)) | |
84 | |
85 # Copy files to each device before running any tests. | |
86 for device_id in attached_devices: | |
87 logging.debug('Pushing files to device %s', device_id) | |
88 test_pkg = test_package.TestPackage(options.test_apk_path, | |
89 options.test_apk_jar_path) | |
90 test_files_copier = test_runner.TestRunner( | |
91 options.build_type, options.test_data, options.install_apk, | |
92 options.save_perf_json, options.screenshot_failures, options.tool, | |
93 options.wait_for_debugger, options.disable_assertions, | |
94 options.push_deps, options.cleanup_test_files, device_id, 0, test_pkg, | |
95 []) | |
96 test_files_copier.InstallTestPackage() | |
97 if options.push_deps: | |
98 logging.info('Pushing data deps to device.') | |
99 test_files_copier.PushDataDeps() | |
100 else: | |
101 logging.warning('Skipping pushing data deps to device.') | |
102 | |
103 # Actually run the tests. | |
104 if len(attached_devices) > 1 and options.wait_for_debugger: | |
105 logging.warning('Debugger can not be sharded, ' | |
106 'using first available device') | |
107 attached_devices = attached_devices[:1] | |
108 logging.debug('Running Python tests') | |
109 sharder = PythonTestSharder(attached_devices, available_tests, options) | |
110 test_results = sharder.RunShardedTests() | |
111 | |
112 if not test_results.DidRunPass(): | |
113 return (test_results, 1) | |
114 | |
115 return (test_results, 0) | |
116 | |
117 | |
118 def _GetTestModules(python_test_root, is_official_build): | |
119 """Retrieve a sorted list of pythonDrivenTests. | |
120 | |
121 Walks the location of pythonDrivenTests, imports them, and provides the list | |
122 of imported modules to the caller. | 48 of imported modules to the caller. |
123 | 49 |
124 Args: | 50 Args: |
125 python_test_root: the path to walk, looking for pythonDrivenTests | 51 host_driven_test_root: The path to walk, looking for the |
126 is_official_build: whether to run only those tests marked 'official' | 52 pythonDrivenTests or host_driven_tests directory |
| 53 is_official_build: Whether to run only those tests marked 'official' |
127 | 54 |
128 Returns: | 55 Returns: |
129 A list of Python modules which may have zero or more tests. | 56 A list of host-driven test case modules which may have zero or more test |
| 57 methods defined. |
130 """ | 58 """ |
131 # By default run all python tests under pythonDrivenTests. | 59 # By default run all host-driven tests under pythonDrivenTests or |
132 python_test_file_list = [] | 60 # host_driven_tests. |
133 for root, _, files in os.walk(python_test_root): | 61 host_driven_test_file_list = [] |
| 62 for root, _, files in os.walk(host_driven_test_root): |
134 if (root.endswith('host_driven_tests') or | 63 if (root.endswith('host_driven_tests') or |
135 root.endswith('pythonDrivenTests') or | 64 root.endswith('pythonDrivenTests') or |
136 (is_official_build and root.endswith('pythonDrivenTests/official'))): | 65 (is_official_build and root.endswith('pythonDrivenTests/official'))): |
137 python_test_file_list += _GetPythonFiles(root, files) | 66 host_driven_test_file_list += _GetPythonFiles(root, files) |
138 python_test_file_list.sort() | 67 host_driven_test_file_list.sort() |
139 | 68 |
140 test_module_list = [_GetModuleFromFile(test_file) | 69 test_module_list = [_GetModuleFromFile(test_file) |
141 for test_file in python_test_file_list] | 70 for test_file in host_driven_test_file_list] |
142 return test_module_list | 71 return test_module_list |
143 | 72 |
144 | 73 |
145 def _GetModuleFromFile(python_file): | 74 def _GetModuleFromFile(python_file): |
146 """Gets the module associated with a file by importing it. | 75 """Gets the module associated with a file by importing it. |
147 | 76 |
148 Args: | 77 Args: |
149 python_file: file to import | 78 python_file: file to import |
150 | 79 |
151 Returns: | 80 Returns: |
152 The module object. | 81 The module object. |
153 """ | 82 """ |
154 sys.path.append(os.path.dirname(python_file)) | 83 sys.path.append(os.path.dirname(python_file)) |
155 import_name = _InferImportNameFromFile(python_file) | 84 import_name = _InferImportNameFromFile(python_file) |
156 return __import__(import_name) | 85 return __import__(import_name) |
157 | 86 |
158 | 87 |
159 def _GetTestsFromClass(test_class): | 88 def _GetTestsFromClass(test_case_class, extra_args): |
160 """Create a list of test objects for each test method on this class. | 89 """Create a list of test objects for each test method in this test case class. |
161 | 90 |
162 Test methods are methods on the class which begin with 'test'. | 91 Test methods are methods on the class which begin with 'test'. |
163 | 92 |
164 Args: | 93 Args: |
165 test_class: class object which contains zero or more test methods. | 94 test_case_class: Class object which contains zero or more test methods. |
| 95 extra_args: List of extra args to pass into the constructor of the |
| 96 test cases. |
166 | 97 |
167 Returns: | 98 Returns: |
168 A list of test objects, each of which is bound to one test. | 99 A list of test case objects, each initialized for a particular test method. |
169 """ | 100 """ |
170 test_names = [m for m in dir(test_class) | 101 test_names = [m for m in dir(test_case_class) |
171 if _IsTestMethod(m, test_class)] | 102 if _IsTestMethod(m, test_case_class)] |
172 return map(test_class, test_names) | 103 return [test_case_class(name, *extra_args) for name in test_names] |
173 | 104 |
174 | 105 |
175 def _GetTestClassesFromModule(test_module): | 106 def _GetTestsFromModule(test_module, extra_args): |
| 107 """Get all tests from |test_module|. |
| 108 |
| 109 Args: |
| 110 test_module: Module from which to get the set of test methods. |
| 111 extra_args: List of extra args to pass into the constructor of the |
| 112 test cases. |
| 113 |
| 114 Returns: |
| 115 A list of test case objects each initialized for a particular test method. |
| 116 """ |
| 117 |
176 tests = [] | 118 tests = [] |
177 for name in dir(test_module): | 119 for name in dir(test_module): |
178 attr = getattr(test_module, name) | 120 attr = getattr(test_module, name) |
179 if _IsTestClass(attr): | 121 if _IsTestCaseClass(attr): |
180 tests.extend(_GetTestsFromClass(attr)) | 122 tests.extend(_GetTestsFromClass(attr, extra_args)) |
181 return tests | 123 return tests |
182 | 124 |
183 | 125 |
184 def _IsTestClass(test_class): | 126 def _IsTestCaseClass(test_class, base_class=InstrumentationHostDrivenTestCase): |
185 return (type(test_class) is types.TypeType and | 127 return (type(test_class) is types.TypeType and |
186 issubclass(test_class, python_test_base.PythonTestBase) and | 128 issubclass(test_class, base_class) and |
187 test_class is not python_test_base.PythonTestBase) | 129 test_class is not base_class) |
188 | 130 |
189 | 131 |
190 def _IsTestMethod(attrname, test_case_class): | 132 def _IsTestMethod(attrname, test_case_class): |
191 """Checks whether this is a valid test method. | 133 """Checks whether this is a valid test method. |
192 | 134 |
193 Args: | 135 Args: |
194 attrname: the method name. | 136 attrname: the method name. |
195 test_case_class: the test case class. | 137 test_case_class: the test case class. |
196 | 138 |
197 Returns: | 139 Returns: |
198 True if test_case_class.'attrname' is callable and it starts with 'test'; | 140 True if test_case_class.'attrname' is callable and it starts with 'test'; |
199 False otherwise. | 141 False otherwise. |
200 """ | 142 """ |
201 attr = getattr(test_case_class, attrname) | 143 attr = getattr(test_case_class, attrname) |
202 return callable(attr) and attrname.startswith('test') | 144 return callable(attr) and attrname.startswith('test') |
203 | 145 |
204 | 146 |
205 def _GetAllTests(test_root, is_official_build): | 147 def _GetAllTests(test_root, is_official_build, extra_args): |
206 """Retrieve a list of Python test modules and their respective methods. | 148 """Retrieve a list of host-driven tests. |
207 | 149 |
208 Args: | 150 Args: |
209 test_root: path which contains Python-driven test files | 151 test_root: Path which contains host-driven test files |
210 is_official_build: whether this is an official build | 152 is_official_build: Whether this is an official build |
| 153 extra_args: List of extra args to pass to the constructor of the test case. |
211 | 154 |
212 Returns: | 155 Returns: |
213 List of test case objects for all available test methods. | 156 List of test case objects for all available test methods. |
214 """ | 157 """ |
215 if not test_root: | 158 if not test_root: |
216 return [] | 159 return [] |
217 all_tests = [] | 160 all_tests = [] |
218 test_module_list = _GetTestModules(test_root, is_official_build) | 161 test_module_list = _GetTestModules(test_root, is_official_build) |
219 for module in test_module_list: | 162 for module in test_module_list: |
220 all_tests.extend(_GetTestClassesFromModule(module)) | 163 all_tests.extend(_GetTestsFromModule(module, extra_args)) |
221 return all_tests | 164 return all_tests |
| 165 |
| 166 |
| 167 def InstrumentationSetup(host_driven_test_root, official_build, annotations, |
| 168 exclude_annotations, test_filter, tool, build_type, |
| 169 push_deps, cleanup_test_files, test_apk_path, |
| 170 test_apk_jar_path, test_data, install_apk, |
| 171 save_perf_json, screenshot_failures, |
| 172 wait_for_debugger, disable_assertions): |
| 173 """Creates test set of host-driven instrumentation tests and runner factory. |
| 174 |
| 175 Args: |
| 176 host_driven_test_root: Directory where the host-driven tests are. |
| 177 official_build: True if this is an official build. |
| 178 annotations: Annotations for the tests. |
| 179 exclude_annotations: Any annotations to exclude from running. |
| 180 test_filter: Filter string for tests. |
| 181 tool: Name of the Valgrind tool. |
| 182 build_type: 'Release' or 'Debug'. |
| 183 push_deps: If True, push all dependencies to the device. |
| 184 cleanup_test_files: If True, cleanup test files on device. |
| 185 test_apk_path: Path to the test apk file. |
| 186 test_apk_jar_path: Path to the accompanying jar file. |
| 187 test_data: Location of the test data. |
| 188 install_apk: Re-installs the apk if opted. |
| 189 save_perf_json: Whether or not to save the JSON file from UI perf tests. |
| 190 screenshot_failures: Take a screenshot for a test failure |
| 191 wait_for_debugger: Blocks until the debugger is connected. |
| 192 disable_assertions: Whether to disable java assertions on the device. |
| 193 |
| 194 Returns: |
| 195 A tuple of (TestRunnerFactory, tests). |
| 196 """ |
| 197 |
| 198 test_collection = test_info_collection.TestInfoCollection() |
| 199 all_tests = _GetAllTests( |
| 200 host_driven_test_root, official_build, [ |
| 201 test_apk_path, test_apk_jar_path, test_data, install_apk, |
| 202 save_perf_json, screenshot_failures, tool, wait_for_debugger, |
| 203 disable_assertions]) |
| 204 test_collection.AddTests(all_tests) |
| 205 |
| 206 available_tests = test_collection.GetAvailableTests( |
| 207 annotations, exclude_annotations, test_filter) |
| 208 logging.debug('All available tests: ' + str( |
| 209 [t.tagged_name for t in available_tests])) |
| 210 |
| 211 def TestRunnerFactory(device, shard_index): |
| 212 return test_runner.HostDrivenTestRunner( |
| 213 device, shard_index, tool, build_type, push_deps, cleanup_test_files) |
| 214 |
| 215 return (TestRunnerFactory, available_tests) |
OLD | NEW |