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 """Runs the Python tests (relies on using the Java test runner).""" |
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 pylib import android_commands |
| 13 from pylib import dispatch |
13 from pylib.base import base_test_result | 14 from pylib.base import base_test_result |
| 15 from pylib.base import shard |
14 from pylib.instrumentation import test_package | 16 from pylib.instrumentation import test_package |
15 from pylib.instrumentation import test_runner | 17 from pylib.instrumentation import test_runner |
16 from pylib.utils import report_results | 18 from pylib.utils import report_results |
17 | 19 |
18 import python_test_base | 20 import python_test_base |
19 from python_test_sharder import PythonTestSharder | 21 import python_test_sharder |
20 from test_info_collection import TestInfoCollection | 22 from test_info_collection import TestInfoCollection |
21 | 23 |
22 | 24 |
23 def _GetPythonFiles(root, files): | 25 def RunHostDrivenTests(options): |
24 """Returns all files from |files| that end in 'Test.py'. | 26 """Runs Python host-driven tests. |
25 | |
26 Args: | |
27 root: A directory name with python files. | |
28 files: A list of file names. | |
29 | |
30 Returns: | |
31 A list with all Python driven test file paths. | |
32 """ | |
33 return [os.path.join(root, f) for f in files if f.endswith('Test.py')] | |
34 | |
35 | |
36 def _InferImportNameFromFile(python_file): | |
37 """Given a file, infer the import name for that file. | |
38 | |
39 Example: /usr/foo/bar/baz.py -> baz. | |
40 | |
41 Args: | |
42 python_file: path to the Python file, ostensibly to import later. | |
43 | |
44 Returns: | |
45 The module name for the given file. | |
46 """ | |
47 return os.path.splitext(os.path.basename(python_file))[0] | |
48 | |
49 | |
50 def DispatchPythonTests(options): | |
51 """Dispatches the Python tests. If there are multiple devices, use sharding. | |
52 | 27 |
53 Args: | 28 Args: |
54 options: command line options. | 29 options: command line options. |
55 | 30 |
56 Returns: | 31 Returns: |
57 A tuple of (base_test_result.TestRunResults object, exit code) | 32 A tuple of (base_test_result.TestRunResults object, exit code) |
58 | 33 |
59 Raises: | 34 Raises: |
60 Exception: If there are no attached devices. | 35 Exception: If there are no attached devices. |
61 """ | 36 """ |
62 | 37 |
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() | 38 test_collection = TestInfoCollection() |
70 all_tests = _GetAllTests(options.python_test_root, options.official_build) | 39 all_tests = _GetAllTests(options.python_test_root, options.official_build) |
71 test_collection.AddTests(all_tests) | 40 test_collection.AddTests(all_tests) |
72 test_names = [t.qualified_name for t in all_tests] | 41 test_names = [t.qualified_name for t in all_tests] |
73 logging.debug('All available tests: ' + str(test_names)) | 42 logging.debug('All available tests: ' + str(test_names)) |
74 | 43 |
75 available_tests = test_collection.GetAvailableTests( | 44 available_tests = test_collection.GetAvailableTests( |
76 options.annotations, options.exclude_annotations, options.test_filter) | 45 options.annotations, options.exclude_annotations, options.test_filter) |
77 | 46 |
78 if not available_tests: | 47 if not available_tests: |
79 logging.warning('No Python tests to run with current args.') | 48 logging.warning('No Python tests to run with current args.') |
80 return (base_test_result.TestRunResults(), 0) | 49 return (base_test_result.TestRunResults(), 0) |
81 | 50 |
82 test_names = [t.qualified_name for t in available_tests] | 51 test_names = [t.qualified_name for t in available_tests] |
83 logging.debug('Final list of tests to run: ' + str(test_names)) | 52 logging.debug('Final list of tests to run: ' + str(test_names)) |
84 | 53 |
85 # Copy files to each device before running any tests. | 54 def TestRunnerFactory(device, shard_index): |
86 for device_id in attached_devices: | 55 return python_test_sharder.PythonTestRunner(options, device, shard_index) |
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, device_id, 0, test_pkg, []) | |
92 test_files_copier.InstallTestPackage() | |
93 if options.push_deps: | |
94 logging.info('Pushing data deps to device.') | |
95 test_files_copier.PushDataDeps() | |
96 else: | |
97 logging.warning('Skipping pushing data deps to device.') | |
98 | 56 |
99 # Actually run the tests. | 57 return dispatch.Dispatch(options, available_tests, TestRunnerFactory) |
100 if len(attached_devices) > 1 and options.wait_for_debugger: | |
101 logging.warning('Debugger can not be sharded, ' | |
102 'using first available device') | |
103 attached_devices = attached_devices[:1] | |
104 logging.debug('Running Python tests') | |
105 sharder = PythonTestSharder(attached_devices, available_tests, options) | |
106 test_results = sharder.RunShardedTests() | |
107 | |
108 if not test_results.DidRunPass(): | |
109 return (test_results, 1) | |
110 | |
111 return (test_results, 0) | |
112 | 58 |
113 | 59 |
| 60 def _GetPythonFiles(root, files): |
| 61 """Returns all files from |files| that end in 'Test.py'. |
| 62 |
| 63 Args: |
| 64 root: A directory name with python files. |
| 65 files: A list of file names. |
| 66 |
| 67 Returns: |
| 68 A list with all Python driven test file paths. |
| 69 """ |
| 70 return [os.path.join(root, f) for f in files if f.endswith('Test.py')] |
| 71 |
| 72 |
| 73 def _InferImportNameFromFile(python_file): |
| 74 """Given a file, infer the import name for that file. |
| 75 |
| 76 Example: /usr/foo/bar/baz.py -> baz. |
| 77 |
| 78 Args: |
| 79 python_file: path to the Python file, ostensibly to import later. |
| 80 |
| 81 Returns: |
| 82 The module name for the given file. |
| 83 """ |
| 84 return os.path.splitext(os.path.basename(python_file))[0] |
| 85 |
| 86 |
| 87 # TODO(gkanwar): replace these functions with functionality from unittest |
114 def _GetTestModules(python_test_root, is_official_build): | 88 def _GetTestModules(python_test_root, is_official_build): |
115 """Retrieve a sorted list of pythonDrivenTests. | 89 """Retrieve a sorted list of pythonDrivenTests. |
116 | 90 |
117 Walks the location of pythonDrivenTests, imports them, and provides the list | 91 Walks the location of pythonDrivenTests, imports them, and provides the list |
118 of imported modules to the caller. | 92 of imported modules to the caller. |
119 | 93 |
120 Args: | 94 Args: |
121 python_test_root: the path to walk, looking for pythonDrivenTests | 95 python_test_root: the path to walk, looking for pythonDrivenTests |
122 is_official_build: whether to run only those tests marked 'official' | 96 is_official_build: whether to run only those tests marked 'official' |
123 | 97 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 Returns: | 182 Returns: |
209 List of test case objects for all available test methods. | 183 List of test case objects for all available test methods. |
210 """ | 184 """ |
211 if not test_root: | 185 if not test_root: |
212 return [] | 186 return [] |
213 all_tests = [] | 187 all_tests = [] |
214 test_module_list = _GetTestModules(test_root, is_official_build) | 188 test_module_list = _GetTestModules(test_root, is_official_build) |
215 for module in test_module_list: | 189 for module in test_module_list: |
216 all_tests.extend(_GetTestClassesFromModule(module)) | 190 all_tests.extend(_GetTestClassesFromModule(module)) |
217 return all_tests | 191 return all_tests |
OLD | NEW |