|
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 |
|
3 # found in the LICENSE file. |
|
4 |
|
5 """Runs the Python tests (relies on using the Java test runner).""" |
|
6 |
|
7 import logging |
|
8 import os |
|
9 import sys |
|
10 import types |
|
11 |
|
12 import android_commands |
|
13 import apk_info |
|
14 import constants |
|
15 import python_test_base |
|
16 from python_test_caller import CallPythonTest |
|
17 from python_test_sharder import PythonTestSharder |
|
18 import run_java_tests |
|
19 from run_java_tests import FatalTestException |
|
20 from test_info_collection import TestInfoCollection |
|
21 from test_result import TestResults |
|
22 |
|
23 |
|
24 def _GetPythonFiles(root, files): |
|
25 """Returns all files from |files| that end in 'Test.py'. |
|
26 |
|
27 Args: |
|
28 root: A directory name with python files. |
|
29 files: A list of file names. |
|
30 |
|
31 Returns: |
|
32 A list with all Python driven test file paths. |
|
33 """ |
|
34 return [os.path.join(root, f) for f in files if f.endswith('Test.py')] |
|
35 |
|
36 |
|
37 def _InferImportNameFromFile(python_file): |
|
38 """Given a file, infer the import name for that file. |
|
39 |
|
40 Example: /usr/foo/bar/baz.py -> baz. |
|
41 |
|
42 Args: |
|
43 python_file: path to the Python file, ostensibly to import later. |
|
44 |
|
45 Returns: |
|
46 The module name for the given file. |
|
47 """ |
|
48 return os.path.splitext(os.path.basename(python_file))[0] |
|
49 |
|
50 |
|
51 def DispatchPythonTests(options): |
|
52 """Dispatches the Python tests. If there are multiple devices, use sharding. |
|
53 |
|
54 Args: |
|
55 options: command line options. |
|
56 |
|
57 Returns: |
|
58 A list of test results. |
|
59 """ |
|
60 |
|
61 attached_devices = android_commands.GetAttachedDevices() |
|
62 if not attached_devices: |
|
63 raise FatalTestException('You have no devices attached or visible!') |
|
64 if options.device: |
|
65 attached_devices = [options.device] |
|
66 |
|
67 test_collection = TestInfoCollection() |
|
68 all_tests = _GetAllTests(options.python_test_root, options.official_build) |
|
69 test_collection.AddTests(all_tests) |
|
70 test_names = [t.qualified_name for t in all_tests] |
|
71 logging.debug('All available tests: ' + str(test_names)) |
|
72 |
|
73 available_tests = test_collection.GetAvailableTests( |
|
74 options.annotation, options.test_filter) |
|
75 |
|
76 if not available_tests: |
|
77 logging.warning('No Python tests to run with current args.') |
|
78 return TestResults() |
|
79 |
|
80 available_tests *= options.number_of_runs |
|
81 test_names = [t.qualified_name for t in available_tests] |
|
82 logging.debug('Final list of tests to run: ' + str(test_names)) |
|
83 |
|
84 # Copy files to each device before running any tests. |
|
85 for device_id in attached_devices: |
|
86 logging.debug('Pushing files to device %s', device_id) |
|
87 apks = [apk_info.ApkInfo(options.test_apk_path, options.test_apk_jar_path)] |
|
88 test_files_copier = run_java_tests.TestRunner(options, device_id, |
|
89 None, False, 0, apks, []) |
|
90 test_files_copier.CopyTestFilesOnce() |
|
91 |
|
92 # Actually run the tests. |
|
93 if len(attached_devices) > 1 and options.wait_for_debugger: |
|
94 logging.warning('Debugger can not be sharded, ' |
|
95 'using first available device') |
|
96 attached_devices = attached_devices[:1] |
|
97 logging.debug('Running Python tests') |
|
98 sharder = PythonTestSharder(attached_devices, available_tests, options) |
|
99 test_results = sharder.RunShardedTests() |
|
100 |
|
101 return test_results |
|
102 |
|
103 |
|
104 def _GetTestModules(python_test_root, is_official_build): |
|
105 """Retrieve a sorted list of pythonDrivenTests. |
|
106 |
|
107 Walks the location of pythonDrivenTests, imports them, and provides the list |
|
108 of imported modules to the caller. |
|
109 |
|
110 Args: |
|
111 python_test_root: the path to walk, looking for pythonDrivenTests |
|
112 is_official_build: whether to run only those tests marked 'official' |
|
113 |
|
114 Returns: |
|
115 A list of Python modules which may have zero or more tests. |
|
116 """ |
|
117 # By default run all python tests under pythonDrivenTests. |
|
118 python_test_file_list = [] |
|
119 for root, _, files in os.walk(python_test_root): |
|
120 if (root.endswith('pythonDrivenTests') |
|
121 or (is_official_build |
|
122 and root.endswith('pythonDrivenTests/official'))): |
|
123 python_test_file_list += _GetPythonFiles(root, files) |
|
124 python_test_file_list.sort() |
|
125 |
|
126 test_module_list = [_GetModuleFromFile(test_file) |
|
127 for test_file in python_test_file_list] |
|
128 return test_module_list |
|
129 |
|
130 |
|
131 def _GetModuleFromFile(python_file): |
|
132 """Gets the module associated with a file by importing it. |
|
133 |
|
134 Args: |
|
135 python_file: file to import |
|
136 |
|
137 Returns: |
|
138 The module object. |
|
139 """ |
|
140 sys.path.append(os.path.dirname(python_file)) |
|
141 import_name = _InferImportNameFromFile(python_file) |
|
142 return __import__(import_name) |
|
143 |
|
144 |
|
145 def _GetTestsFromClass(test_class): |
|
146 """Create a list of test objects for each test method on this class. |
|
147 |
|
148 Test methods are methods on the class which begin with 'test'. |
|
149 |
|
150 Args: |
|
151 test_class: class object which contains zero or more test methods. |
|
152 |
|
153 Returns: |
|
154 A list of test objects, each of which is bound to one test. |
|
155 """ |
|
156 test_names = [m for m in dir(test_class) |
|
157 if _IsTestMethod(m, test_class)] |
|
158 return map(test_class, test_names) |
|
159 |
|
160 |
|
161 def _GetTestClassesFromModule(test_module): |
|
162 tests = [] |
|
163 for name in dir(test_module): |
|
164 attr = getattr(test_module, name) |
|
165 if _IsTestClass(attr): |
|
166 tests.extend(_GetTestsFromClass(attr)) |
|
167 return tests |
|
168 |
|
169 |
|
170 def _IsTestClass(test_class): |
|
171 return (type(test_class) is types.TypeType and |
|
172 issubclass(test_class, python_test_base.PythonTestBase) and |
|
173 test_class is not python_test_base.PythonTestBase) |
|
174 |
|
175 |
|
176 def _IsTestMethod(attrname, test_case_class): |
|
177 """Checks whether this is a valid test method. |
|
178 |
|
179 Args: |
|
180 attrname: the method name. |
|
181 test_case_class: the test case class. |
|
182 |
|
183 Returns: |
|
184 True if test_case_class.'attrname' is callable and it starts with 'test'; |
|
185 False otherwise. |
|
186 """ |
|
187 attr = getattr(test_case_class, attrname) |
|
188 return callable(attr) and attrname.startswith('test') |
|
189 |
|
190 |
|
191 def _GetAllTests(test_root, is_official_build): |
|
192 """Retrieve a list of Python test modules and their respective methods. |
|
193 |
|
194 Args: |
|
195 test_root: path which contains Python-driven test files |
|
196 is_official_build: whether this is an official build |
|
197 |
|
198 Returns: |
|
199 List of test case objects for all available test methods. |
|
200 """ |
|
201 if not test_root: |
|
202 return [] |
|
203 all_tests = [] |
|
204 test_module_list = _GetTestModules(test_root, is_official_build) |
|
205 for module in test_module_list: |
|
206 all_tests.extend(_GetTestClassesFromModule(module)) |
|
207 return all_tests |