Chromium Code Reviews| 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 54% |
| rename from build/android/pylib/host_driven/run_python_tests.py |
| rename to build/android/pylib/host_driven/setup.py |
| index 518f6cf10852fc2560447a9a4818d77f481cac96..1fba0d7fc0c5689f67fc9a79df702d7b2088c52d 100644 |
| --- a/build/android/pylib/host_driven/run_python_tests.py |
| +++ b/build/android/pylib/host_driven/setup.py |
| @@ -4,6 +4,7 @@ |
| """Runs the Python tests (relies on using the Java test runner).""" |
| +import functools |
| import logging |
| import os |
| import sys |
| @@ -11,13 +12,13 @@ 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 |
| from pylib.utils import report_results |
| +import instrumentation_test_base |
| import python_test_base |
| -from python_test_sharder import PythonTestSharder |
| -from test_info_collection import TestInfoCollection |
| +import test_runner |
| +import test_info_collection |
| def _GetPythonFiles(root, files): |
| @@ -39,7 +40,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,74 +48,6 @@ 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. |
| - |
| - 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. |
| @@ -165,26 +98,30 @@ def _GetTestsFromClass(test_class): |
| test_class: class object which contains zero or more test methods. |
| Returns: |
| - A list of test objects, each of which is bound to one test. |
| + A list of partially applied constructor, each one for a particular test |
| + name. To get the test objects from this, call the partial constructors |
| + with any remaining arguments. |
| """ |
| test_names = [m for m in dir(test_class) |
| if _IsTestMethod(m, test_class)] |
| - return map(test_class, test_names) |
| + return [functools.partial(test_class, name) for name in test_names] |
| + |
| +def _GetTestClassesFromModule(test_module, base_class): |
| + """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): |
| + if _IsTestClass(attr, base_class): |
| tests.extend(_GetTestsFromClass(attr)) |
| return tests |
| -def _IsTestClass(test_class): |
| +def _IsTestClass(test_class, base_class): |
| 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 +139,13 @@ def _IsTestMethod(attrname, test_case_class): |
| return callable(attr) and attrname.startswith('test') |
| -def _GetAllTests(test_root, is_official_build): |
| +def _GetAllTests(test_root, is_official_build, base_class): |
| """Retrieve a list of Python 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 Python-driven test files |
| + is_official_build: Whether this is an official build |
| + base_class: Base class that all tests should derive from |
| Returns: |
| List of test case objects for all available test methods. |
| @@ -217,5 +155,58 @@ 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, base_class)) |
| return all_tests |
| + |
| + |
| +def InstrumentationSetup(python_test_root, official_build, annotations, |
|
bulach
2013/07/23 17:27:52
as above, Setup is an incredibly overloaded term i
gkanwar1
2013/07/23 18:38:55
This is named setup.py to match the recent shardin
|
| + 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 python tests and test runner factory. |
| + |
| + Args: |
| + python_test_root: Directory where the Python 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_test_constructors = _GetAllTests( |
| + python_test_root, official_build, |
| + instrumentation_test_base.InstrumentationPythonTestBase) |
| + all_tests = [constructor(test_apk_path, test_apk_jar_path, test_data, |
| + install_apk, save_perf_json, screenshot_failures, |
| + tool, wait_for_debugger, disable_assertions) |
| + for constructor in all_test_constructors] |
| + 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.PythonTestRunner( |
| + device, shard_index, tool, build_type, push_deps, cleanup_test_files) |
| + |
| + return (TestRunnerFactory, available_tests) |