Bug 797671: Import Webrtc.org code from stable branch 3.12 (rev 2820) rs=jesup
[gecko.git] / media / webrtc / trunk / build / android / pylib / run_python_tests.py
blob6556892a1725b93ddfb55d98ffbffe38a3a784c4
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.
5 """Runs the Python tests (relies on using the Java test runner)."""
7 import logging
8 import os
9 import sys
10 import types
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
24 def _GetPythonFiles(root, files):
25 """Returns all files from |files| that end in 'Test.py'.
27 Args:
28 root: A directory name with python files.
29 files: A list of file names.
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')]
37 def _InferImportNameFromFile(python_file):
38 """Given a file, infer the import name for that file.
40 Example: /usr/foo/bar/baz.py -> baz.
42 Args:
43 python_file: path to the Python file, ostensibly to import later.
45 Returns:
46 The module name for the given file.
47 """
48 return os.path.splitext(os.path.basename(python_file))[0]
51 def DispatchPythonTests(options):
52 """Dispatches the Python tests. If there are multiple devices, use sharding.
54 Args:
55 options: command line options.
57 Returns:
58 A list of test results.
59 """
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]
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))
73 available_tests = test_collection.GetAvailableTests(
74 options.annotation, options.test_filter)
76 if not available_tests:
77 logging.warning('No Python tests to run with current args.')
78 return TestResults()
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))
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()
92 # Actually run the tests.
93 if (len(attached_devices) > 1 and
94 not options.wait_for_debugger):
95 logging.debug('Sharding Python tests.')
96 sharder = PythonTestSharder(attached_devices, options.shard_retries,
97 available_tests)
98 test_results = sharder.RunShardedTests()
99 else:
100 logging.debug('Running Python tests serially.')
101 test_results = _RunPythonTests(available_tests, attached_devices[0])
103 return test_results
106 def _RunPythonTests(tests_to_run, device_id):
107 """Runs a list of Python tests serially on one device and returns results.
109 Args:
110 tests_to_run: a list of objects inheriting from PythonTestBase.
111 device_id: ID of the device to run tests on.
113 Returns:
114 A list of test results, aggregated across all the tests run.
116 # This is a list of TestResults objects.
117 results = [CallPythonTest(t, device_id, 0) for t in tests_to_run]
118 # Merge the list of TestResults into one TestResults.
119 return TestResults.FromTestResults(results)
122 def _GetTestModules(python_test_root, is_official_build):
123 """Retrieve a sorted list of pythonDrivenTests.
125 Walks the location of pythonDrivenTests, imports them, and provides the list
126 of imported modules to the caller.
128 Args:
129 python_test_root: the path to walk, looking for pythonDrivenTests
130 is_official_build: whether to run only those tests marked 'official'
132 Returns:
133 A list of Python modules which may have zero or more tests.
135 # By default run all python tests under pythonDrivenTests.
136 python_test_file_list = []
137 for root, _, files in os.walk(python_test_root):
138 if (root.endswith('pythonDrivenTests')
139 or (is_official_build
140 and root.endswith('pythonDrivenTests/official'))):
141 python_test_file_list += _GetPythonFiles(root, files)
142 python_test_file_list.sort()
144 test_module_list = [_GetModuleFromFile(test_file)
145 for test_file in python_test_file_list]
146 return test_module_list
149 def _GetModuleFromFile(python_file):
150 """Gets the module associated with a file by importing it.
152 Args:
153 python_file: file to import
155 Returns:
156 The module object.
158 sys.path.append(os.path.dirname(python_file))
159 import_name = _InferImportNameFromFile(python_file)
160 return __import__(import_name)
163 def _GetTestsFromClass(test_class):
164 """Create a list of test objects for each test method on this class.
166 Test methods are methods on the class which begin with 'test'.
168 Args:
169 test_class: class object which contains zero or more test methods.
171 Returns:
172 A list of test objects, each of which is bound to one test.
174 test_names = [m for m in dir(test_class)
175 if _IsTestMethod(m, test_class)]
176 return map(test_class, test_names)
179 def _GetTestClassesFromModule(test_module):
180 tests = []
181 for name in dir(test_module):
182 attr = getattr(test_module, name)
183 if _IsTestClass(attr):
184 tests.extend(_GetTestsFromClass(attr))
185 return tests
188 def _IsTestClass(test_class):
189 return (type(test_class) is types.TypeType and
190 issubclass(test_class, python_test_base.PythonTestBase) and
191 test_class is not python_test_base.PythonTestBase)
194 def _IsTestMethod(attrname, test_case_class):
195 """Checks whether this is a valid test method.
197 Args:
198 attrname: the method name.
199 test_case_class: the test case class.
201 Returns:
202 True if test_case_class.'attrname' is callable and it starts with 'test';
203 False otherwise.
205 attr = getattr(test_case_class, attrname)
206 return callable(attr) and attrname.startswith('test')
209 def _GetAllTests(test_root, is_official_build):
210 """Retrieve a list of Python test modules and their respective methods.
212 Args:
213 test_root: path which contains Python-driven test files
214 is_official_build: whether this is an official build
216 Returns:
217 List of test case objects for all available test methods.
219 if not test_root:
220 return []
221 all_tests = []
222 test_module_list = _GetTestModules(test_root, is_official_build)
223 for module in test_module_list:
224 all_tests.extend(_GetTestClassesFromModule(module))
225 return all_tests