Index: build/android/pylib/host_driven/setup.py |
diff --git a/build/android/pylib/host_driven/run_python_tests.py b/build/android/pylib/host_driven/setup.py |
similarity index 41% |
rename from build/android/pylib/host_driven/run_python_tests.py |
rename to build/android/pylib/host_driven/setup.py |
index 518f6cf10852fc2560447a9a4818d77f481cac96..f093d41a8ec62aa4473366e6763f3bb81e13cf3e 100644 |
--- a/build/android/pylib/host_driven/run_python_tests.py |
+++ b/build/android/pylib/host_driven/setup.py |
@@ -2,8 +2,9 @@ |
# Use of this source code is governed by a BSD-style license that can be |
# found in the LICENSE file. |
-"""Runs the Python tests (relies on using the Java test runner).""" |
+"""Runs the host-driven tests (relies on using the Java test runner).""" |
+import functools |
import logging |
import os |
import sys |
@@ -11,13 +12,12 @@ import types |
from pylib import android_commands |
from pylib.base import base_test_result |
-from pylib.instrumentation import test_package |
-from pylib.instrumentation import test_runner |
+from pylib.base import shard |
frankf
2013/07/24 01:04:55
you mean test_dispatcher?
gkanwar1
2013/07/24 17:38:04
Actually, this should just get removed, since it's
|
from pylib.utils import report_results |
-import python_test_base |
-from python_test_sharder import PythonTestSharder |
-from test_info_collection import TestInfoCollection |
+import instrumentation_test |
+import test_runner |
+import test_info_collection |
def _GetPythonFiles(root, files): |
@@ -28,7 +28,7 @@ def _GetPythonFiles(root, files): |
files: A list of file names. |
Returns: |
- A list with all Python driven test file paths. |
+ A list with all host-driven test file paths. |
""" |
return [os.path.join(root, f) for f in files if f.endswith('Test.py')] |
@@ -39,7 +39,7 @@ def _InferImportNameFromFile(python_file): |
Example: /usr/foo/bar/baz.py -> baz. |
Args: |
- python_file: path to the Python file, ostensibly to import later. |
+ python_file: Path to the Python file, ostensibly to import later. |
Returns: |
The module name for the given file. |
@@ -47,98 +47,32 @@ def _InferImportNameFromFile(python_file): |
return os.path.splitext(os.path.basename(python_file))[0] |
-def DispatchPythonTests(options): |
- """Dispatches the Python tests. If there are multiple devices, use sharding. |
+def _GetTestModules(host_driven_test_root, is_official_build): |
+ """Retrieve a sorted list of host-driven tests. |
- Args: |
- options: command line options. |
- |
- Returns: |
- A tuple of (base_test_result.TestRunResults object, exit code) |
- |
- Raises: |
- Exception: If there are no attached devices. |
- """ |
- |
- attached_devices = android_commands.GetAttachedDevices() |
- if not attached_devices: |
- raise Exception('You have no devices attached or visible!') |
- if options.test_device: |
- attached_devices = [options.test_device] |
- |
- test_collection = TestInfoCollection() |
- all_tests = _GetAllTests(options.python_test_root, options.official_build) |
- test_collection.AddTests(all_tests) |
- test_names = [t.qualified_name for t in all_tests] |
- logging.debug('All available tests: ' + str(test_names)) |
- |
- available_tests = test_collection.GetAvailableTests( |
- options.annotations, options.exclude_annotations, options.test_filter) |
- |
- if not available_tests: |
- logging.warning('No Python tests to run with current args.') |
- return (base_test_result.TestRunResults(), 0) |
- |
- test_names = [t.qualified_name for t in available_tests] |
- logging.debug('Final list of tests to run: ' + str(test_names)) |
- |
- # Copy files to each device before running any tests. |
- for device_id in attached_devices: |
- logging.debug('Pushing files to device %s', device_id) |
- test_pkg = test_package.TestPackage(options.test_apk_path, |
- options.test_apk_jar_path) |
- test_files_copier = test_runner.TestRunner( |
- options.build_type, options.test_data, options.install_apk, |
- options.save_perf_json, options.screenshot_failures, options.tool, |
- options.wait_for_debugger, options.disable_assertions, |
- options.push_deps, options.cleanup_test_files, device_id, 0, test_pkg, |
- []) |
- test_files_copier.InstallTestPackage() |
- if options.push_deps: |
- logging.info('Pushing data deps to device.') |
- test_files_copier.PushDataDeps() |
- else: |
- logging.warning('Skipping pushing data deps to device.') |
- |
- # Actually run the tests. |
- if len(attached_devices) > 1 and options.wait_for_debugger: |
- logging.warning('Debugger can not be sharded, ' |
- 'using first available device') |
- attached_devices = attached_devices[:1] |
- logging.debug('Running Python tests') |
- sharder = PythonTestSharder(attached_devices, available_tests, options) |
- test_results = sharder.RunShardedTests() |
- |
- if not test_results.DidRunPass(): |
- return (test_results, 1) |
- |
- return (test_results, 0) |
- |
- |
-def _GetTestModules(python_test_root, is_official_build): |
- """Retrieve a sorted list of pythonDrivenTests. |
- |
- Walks the location of pythonDrivenTests, imports them, and provides the list |
+ Walks the location of host-driven tests, imports them, and provides the list |
of imported modules to the caller. |
Args: |
- python_test_root: the path to walk, looking for pythonDrivenTests |
- is_official_build: whether to run only those tests marked 'official' |
+ host_driven_test_root: The path to walk, looking for the |
+ pythonDrivenTests or host_driven_tests directory |
+ is_official_build: Whether to run only those tests marked 'official' |
Returns: |
A list of Python modules which may have zero or more tests. |
""" |
- # By default run all python tests under pythonDrivenTests. |
- python_test_file_list = [] |
- for root, _, files in os.walk(python_test_root): |
+ # By default run all host-driven tests under pythonDrivenTests or |
+ # host_driven_tests. |
+ host_driven_test_file_list = [] |
+ for root, _, files in os.walk(host_driven_test_root): |
if (root.endswith('host_driven_tests') or |
root.endswith('pythonDrivenTests') or |
(is_official_build and root.endswith('pythonDrivenTests/official'))): |
- python_test_file_list += _GetPythonFiles(root, files) |
- python_test_file_list.sort() |
+ host_driven_test_file_list += _GetPythonFiles(root, files) |
+ host_driven_test_file_list.sort() |
test_module_list = [_GetModuleFromFile(test_file) |
- for test_file in python_test_file_list] |
+ for test_file in host_driven_test_file_list] |
return test_module_list |
@@ -156,35 +90,40 @@ def _GetModuleFromFile(python_file): |
return __import__(import_name) |
-def _GetTestsFromClass(test_class): |
+def _GetTestsFromClass(test_class, extra_args): |
"""Create a list of test objects for each test method on this class. |
Test methods are methods on the class which begin with 'test'. |
Args: |
- test_class: class object which contains zero or more test methods. |
+ test_class: Class object which contains zero or more test methods. |
+ extra_args: List of extra args to pass into the constructor of the |
+ test cases. |
Returns: |
- A list of test objects, each of which is bound to one test. |
+ A list of test case objects, each initialized for particular test method. |
""" |
test_names = [m for m in dir(test_class) |
if _IsTestMethod(m, test_class)] |
- return map(test_class, test_names) |
+ return [test_class(name, *extra_args) for name in test_names] |
+ |
+def _GetTestClassesFromModule(test_module, extra_args): |
+ """Get all test classes from |test_module|.""" |
-def _GetTestClassesFromModule(test_module): |
tests = [] |
for name in dir(test_module): |
attr = getattr(test_module, name) |
if _IsTestClass(attr): |
- tests.extend(_GetTestsFromClass(attr)) |
+ tests.extend(_GetTestsFromClass(attr, extra_args)) |
return tests |
-def _IsTestClass(test_class): |
+def _IsTestClass(test_class, |
+ base_class=instrumentation_test.HostDrivenInstrumentationTest): |
return (type(test_class) is types.TypeType and |
- issubclass(test_class, python_test_base.PythonTestBase) and |
- test_class is not python_test_base.PythonTestBase) |
+ issubclass(test_class, base_class) and |
+ test_class is not base_class) |
def _IsTestMethod(attrname, test_case_class): |
@@ -202,12 +141,13 @@ def _IsTestMethod(attrname, test_case_class): |
return callable(attr) and attrname.startswith('test') |
-def _GetAllTests(test_root, is_official_build): |
- """Retrieve a list of Python test modules and their respective methods. |
+def _GetAllTests(test_root, is_official_build, extra_args): |
+ """Retrieve a list of host-driven test modules and their respective methods. |
Args: |
- test_root: path which contains Python-driven test files |
- is_official_build: whether this is an official build |
+ test_root: Path which contains host-driven test files |
+ is_official_build: Whether this is an official build |
+ extra_args: List of extra args to pass to the constructor of the test case. |
Returns: |
List of test case objects for all available test methods. |
@@ -217,5 +157,56 @@ def _GetAllTests(test_root, is_official_build): |
all_tests = [] |
test_module_list = _GetTestModules(test_root, is_official_build) |
for module in test_module_list: |
- all_tests.extend(_GetTestClassesFromModule(module)) |
+ all_tests.extend(_GetTestClassesFromModule(module, extra_args)) |
return all_tests |
+ |
+ |
+def InstrumentationSetup(host_driven_test_root, official_build, annotations, |
+ exclude_annotations, test_filter, tool, build_type, |
+ push_deps, cleanup_test_files, test_apk_path, |
+ test_apk_jar_path, test_data, install_apk, |
+ save_perf_json, screenshot_failures, |
+ wait_for_debugger, disable_assertions): |
+ """Creates test set of host-driven instrumentation tests and runner factory. |
+ |
+ Args: |
+ host_driven_test_root: Directory where the host-driven tests are. |
+ official_build: True if this is an official build. |
+ annotations: Annotations for the tests. |
+ exclude_annotations: Any annotations to exclude from running. |
+ test_filter: Filter string for tests. |
+ tool: Name of the Valgrind tool. |
+ build_type: 'Release' or 'Debug'. |
+ push_deps: If True, push all dependencies to the device. |
+ cleanup_test_files: If True, cleanup test files on device. |
+ test_apk_path: Path to the test apk file. |
+ test_apk_jar_path: Path to the accompanying jar file. |
+ test_data: Location of the test data. |
+ install_apk: Re-installs the apk if opted. |
+ save_perf_json: Whether or not to save the JSON file from UI perf tests. |
+ screenshot_failures: Take a screenshot for a test failure |
+ wait_for_debugger: Blocks until the debugger is connected. |
+ disable_assertions: Whether to disable java assertions on the device. |
+ |
+ Returns: |
+ A tuple of (TestRunnerFactory, tests). |
+ """ |
+ |
+ test_collection = test_info_collection.TestInfoCollection() |
+ all_tests = _GetAllTests( |
+ host_driven_test_root, official_build, [ |
+ test_apk_path, test_apk_jar_path, test_data, install_apk, |
+ save_perf_json, screenshot_failures, tool, wait_for_debugger, |
+ disable_assertions]) |
+ test_collection.AddTests(all_tests) |
+ test_names = [t.qualified_name for t in all_tests] |
+ logging.debug('All available tests: ' + str(test_names)) |
+ |
+ available_tests = test_collection.GetAvailableTests( |
+ annotations, exclude_annotations, test_filter) |
+ |
+ def TestRunnerFactory(device, shard_index): |
+ return test_runner.HostDrivenTestRunner( |
+ device, shard_index, tool, build_type, push_deps, cleanup_test_files) |
+ |
+ return (TestRunnerFactory, available_tests) |