Chromium Code Reviews| Index: build/android/pylib/host_driven/test_case.py |
| diff --git a/build/android/pylib/host_driven/python_test_base.py b/build/android/pylib/host_driven/test_case.py |
| similarity index 35% |
| rename from build/android/pylib/host_driven/python_test_base.py |
| rename to build/android/pylib/host_driven/test_case.py |
| index 42a63267c9212811c2ef289e07116e577aa17b48..84da729a73cb669cdf7e9645e63eeb84ae6afaf6 100644 |
| --- a/build/android/pylib/host_driven/python_test_base.py |
| +++ b/build/android/pylib/host_driven/test_case.py |
| @@ -2,21 +2,17 @@ |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| -"""Base class for Android Python-driven tests. |
| +"""Base class for host-driven test cases. |
| -This test case is intended to serve as the base class for any Python-driven |
| -tests. It is similar to the Python unitttest module in that the user's tests |
| -inherit from this case and add their tests in that case. |
| +This test case is intended to serve as the base class for any host-driven |
| +test cases. It is similar to the Python unitttest module in that test cases |
| +inherit from this class and add methods which will be run as tests. |
| -When a PythonTestBase object is instantiated, its purpose is to run only one of |
| -its tests. The test runner gives it the name of the test the instance will |
| -run. The test runner calls SetUp with the Android device ID which the test will |
| -run against. The runner runs the test method itself, collecting the result, |
| -and calls TearDown. |
| - |
| -Tests can basically do whatever they want in the test methods, such as call |
|
bulach
2013/08/05 10:18:20
I think this bit should still remain, it's part of
gkanwar1
2013/08/05 17:35:49
Good point. I think I removed this when moving Jav
|
| -Java tests using _RunJavaTests. Those methods have the advantage of massaging |
| -the Java test results into Python test results. |
| +When a HostDrivenTestCase object is instantiated, its purpose is to run only one |
| +test method in the derived class. The test runner gives it the name of the test |
| +method the instance will run. The test runner calls SetUp with the device ID |
| +which the test method will run against. The test runner runs the test method |
| +itself, collecting the result, and calls TearDown. |
| """ |
| import logging |
| @@ -25,77 +21,79 @@ import time |
| from pylib import android_commands |
| from pylib.base import base_test_result |
| -from pylib.instrumentation import test_options |
| from pylib.instrumentation import test_package |
| from pylib.instrumentation import test_result |
| from pylib.instrumentation import test_runner |
| - |
| # aka the parent of com.google.android |
| BASE_ROOT = 'src' + os.sep |
| -class PythonTestBase(object): |
| - """Base class for Python-driven tests.""" |
| +class HostDrivenTestCase(object): |
| + """Base class for host-driven test cases.""" |
| + |
| + _HOST_DRIVEN_TAG = 'HostDriven' |
| - def __init__(self, test_name): |
| - # test_name must match one of the test methods defined on a subclass which |
| - # inherits from this class. |
| - # It's stored so we can do the attr lookup on demand, allowing this class |
| - # to be pickled, a requirement for the multiprocessing module. |
| + def __init__(self, test_name, instrumentation_options=None): |
| + """Create a test case initialized to run |test_name|. |
| + |
| + Args: |
| + test_name: The name of the method to run as the test. |
| + instrumentation_options: An InstrumentationOptions object. |
| + """ |
| self.test_name = test_name |
| class_name = self.__class__.__name__ |
| - self.qualified_name = class_name + '.' + self.test_name |
| + self.qualified_name = '%s.%s' % (class_name, self.test_name) |
| + # Use tagged_name when creating results, so that we can identify host-driven |
| + # tests in the overall results. |
| + self.tagged_name = '%s_%s' % (self._HOST_DRIVEN_TAG, self.qualified_name) |
| - def SetUp(self, options): |
| - self.options = options |
| - self.shard_index = self.options.shard_index |
| - self.device_id = self.options.device_id |
| - self.adb = android_commands.AndroidCommands(self.device_id) |
| + self.instrumentation_options = instrumentation_options |
| self.ports_to_forward = [] |
| + def SetUp(self, device, shard_index, build_type, push_deps, |
| + cleanup_test_files): |
| + self.device_id = device |
| + self.shard_index = shard_index |
| + self.build_type = build_type |
| + self.adb = android_commands.AndroidCommands(self.device_id) |
| + self.push_deps = push_deps |
| + self.cleanup_test_files = cleanup_test_files |
| + |
| def TearDown(self): |
| pass |
| def GetOutDir(self): |
| return os.path.join(os.environ['CHROME_SRC'], 'out', |
| - self.options.build_type) |
| + self.build_type) |
| def Run(self): |
| - logging.warning('Running Python-driven test: %s', self.test_name) |
| + logging.info('Running host-driven test: %s', self.test_name) |
| + # Get the test method on the derived class and execute it |
| return getattr(self, self.test_name)() |
| - def _RunJavaTest(self, fname, suite, test): |
| - """Runs a single Java test with a Java TestRunner. |
| + # Instrumentation test case API. |
| + # |
| + # Test cases may make use of functions defined here internally to assist |
| + # in running instrumentation tests. These functions rely on |
| + # instrumentation_options being defined. |
|
bulach
2013/08/05 10:18:20
nit: I think 75 is not very descriptive, and 77-79
gkanwar1
2013/08/05 17:35:49
Fair enough. I think it makes sense to move this t
|
| + def _RunJavaTest(self, package_name, test_case, test_method): |
| + """Runs a single Java test method with a Java TestRunner. |
| Args: |
| - fname: filename for the test (e.g. foo/bar/baz/tests/FooTest.py) |
| - suite: name of the Java test suite (e.g. FooTest) |
| - test: name of the test method to run (e.g. testFooBar) |
| + package_name: Package name in which the java tests live |
| + (e.g. foo.bar.baz.tests) |
| + test_case: Name of the Java test case (e.g. FooTest) |
| + test_method: Name of the test method to run (e.g. testFooBar) |
| Returns: |
| TestRunResults object with a single test result. |
| """ |
| - test = self._ComposeFullTestName(fname, suite, test) |
| + test = '%s.%s#%s' % (package_name, test_case, test_method) |
| test_pkg = test_package.TestPackage( |
| - self.options.test_apk_path, self.options.test_apk_jar_path) |
| - instrumentation_options = test_options.InstrumentationOptions( |
| - self.options.build_type, |
| - self.options.tool, |
| - self.options.cleanup_test_files, |
| - self.options.push_deps, |
| - self.options.annotations, |
| - self.options.exclude_annotations, |
| - self.options.test_filter, |
| - self.options.test_data, |
| - self.options.save_perf_json, |
| - self.options.screenshot_failures, |
| - self.options.disable_assertions, |
| - self.options.wait_for_debugger, |
| - self.options.test_apk, |
| - self.options.test_apk_path, |
| - self.options.test_apk_jar_path) |
| - java_test_runner = test_runner.TestRunner(instrumentation_options, |
| + self.instrumentation_options.test_apk_path, |
| + self.instrumentation_options.test_apk_jar_path) |
| + java_test_runner = test_runner.TestRunner(self.instrumentation_options, |
| self.device_id, |
| self.shard_index, test_pkg, |
| self.ports_to_forward) |
| @@ -105,18 +103,20 @@ class PythonTestBase(object): |
| finally: |
| java_test_runner.TearDown() |
| - def _RunJavaTests(self, fname, tests): |
| + def _RunJavaTests(self, package_name, tests): |
| """Calls a list of tests and stops at the first test failure. |
| This method iterates until either it encounters a non-passing test or it |
| - exhausts the list of tests. Then it returns the appropriate Python result. |
| + exhausts the list of tests. Then it returns the appropriate overall result. |
| Args: |
| - fname: filename for the Python test |
| - tests: a list of Java test names which will be run |
| + package_name: Package name in which the java tests live |
| + (e.g. foo.bar.baz.tests) |
| + tests: A list of Java test names which will be run |
| Returns: |
| - A TestRunResults object containing a result for this Python test. |
| + A TestRunResults object containing an overall result for this set of Java |
| + tests. If any Java tests do not pass, this is a fail overall. |
| """ |
| test_type = base_test_result.ResultType.PASS |
| log = '' |
| @@ -126,27 +126,17 @@ class PythonTestBase(object): |
| # We're only running one test at a time, so this TestRunResults object |
| # will hold only one result. |
| suite, test_name = test.split('.') |
| - java_results = self._RunJavaTest(fname, suite, test_name) |
| - assert len(java_results.GetAll()) == 1 |
| - if not java_results.DidRunPass(): |
| - result = java_results.GetNotPass().pop() |
| + java_result = self._RunJavaTest(package_name, suite, test_name) |
| + assert len(java_result.GetAll()) == 1 |
| + if not java_result.DidRunPass(): |
| + result = java_result.GetNotPass().pop() |
| log = result.GetLog() |
| test_type = result.GetType() |
| break |
| duration_ms = int(time.time()) * 1000 - start_ms |
| - python_results = base_test_result.TestRunResults() |
| - python_results.AddResult( |
| + overall_result = base_test_result.TestRunResults() |
| + overall_result.AddResult( |
| test_result.InstrumentationTestResult( |
| - self.qualified_name, test_type, start_ms, duration_ms, log=log)) |
| - return python_results |
| - |
| - def _ComposeFullTestName(self, fname, suite, test): |
| - package_name = self._GetPackageName(fname) |
| - return package_name + '.' + suite + '#' + test |
| - |
| - def _GetPackageName(self, fname): |
| - """Extracts the package name from the test file path.""" |
| - dirname = os.path.dirname(fname) |
| - package = dirname[dirname.rfind(BASE_ROOT) + len(BASE_ROOT):] |
| - return package.replace(os.sep, '.') |
| + self.tagged_name, test_type, start_ms, duration_ms, log=log)) |
| + return overall_result |