michael@0: # Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: # Use of this source code is governed by a BSD-style license that can be michael@0: # found in the LICENSE file. michael@0: michael@0: """Base class for Android Python-driven tests. michael@0: michael@0: This test case is intended to serve as the base class for any Python-driven michael@0: tests. It is similar to the Python unitttest module in that the user's tests michael@0: inherit from this case and add their tests in that case. michael@0: michael@0: When a PythonTestBase object is instantiated, its purpose is to run only one of michael@0: its tests. The test runner gives it the name of the test the instance will michael@0: run. The test runner calls SetUp with the Android device ID which the test will michael@0: run against. The runner runs the test method itself, collecting the result, michael@0: and calls TearDown. michael@0: michael@0: Tests can basically do whatever they want in the test methods, such as call michael@0: Java tests using _RunJavaTests. Those methods have the advantage of massaging michael@0: the Java test results into Python test results. michael@0: """ michael@0: michael@0: import logging michael@0: import os michael@0: import time michael@0: michael@0: import android_commands michael@0: import apk_info michael@0: from run_java_tests import TestRunner michael@0: from test_result import SingleTestResult, TestResults michael@0: michael@0: michael@0: # aka the parent of com.google.android michael@0: BASE_ROOT = 'src' + os.sep michael@0: michael@0: michael@0: class PythonTestBase(object): michael@0: """Base class for Python-driven tests.""" michael@0: michael@0: def __init__(self, test_name): michael@0: # test_name must match one of the test methods defined on a subclass which michael@0: # inherits from this class. michael@0: # It's stored so we can do the attr lookup on demand, allowing this class michael@0: # to be pickled, a requirement for the multiprocessing module. michael@0: self.test_name = test_name michael@0: class_name = self.__class__.__name__ michael@0: self.qualified_name = class_name + '.' + self.test_name michael@0: michael@0: def SetUp(self, options): michael@0: self.options = options michael@0: self.shard_index = self.options.shard_index michael@0: self.device_id = self.options.device_id michael@0: self.adb = android_commands.AndroidCommands(self.device_id) michael@0: self.ports_to_forward = [] michael@0: michael@0: def TearDown(self): michael@0: pass michael@0: michael@0: def Run(self): michael@0: logging.warning('Running Python-driven test: %s', self.test_name) michael@0: return getattr(self, self.test_name)() michael@0: michael@0: def _RunJavaTest(self, fname, suite, test): michael@0: """Runs a single Java test with a Java TestRunner. michael@0: michael@0: Args: michael@0: fname: filename for the test (e.g. foo/bar/baz/tests/FooTest.py) michael@0: suite: name of the Java test suite (e.g. FooTest) michael@0: test: name of the test method to run (e.g. testFooBar) michael@0: michael@0: Returns: michael@0: TestResults object with a single test result. michael@0: """ michael@0: test = self._ComposeFullTestName(fname, suite, test) michael@0: apks = [apk_info.ApkInfo(self.options.test_apk_path, michael@0: self.options.test_apk_jar_path)] michael@0: java_test_runner = TestRunner(self.options, self.device_id, [test], False, michael@0: self.shard_index, michael@0: apks, michael@0: self.ports_to_forward) michael@0: return java_test_runner.Run() michael@0: michael@0: def _RunJavaTests(self, fname, tests): michael@0: """Calls a list of tests and stops at the first test failure. michael@0: michael@0: This method iterates until either it encounters a non-passing test or it michael@0: exhausts the list of tests. Then it returns the appropriate Python result. michael@0: michael@0: Args: michael@0: fname: filename for the Python test michael@0: tests: a list of Java test names which will be run michael@0: michael@0: Returns: michael@0: A TestResults object containing a result for this Python test. michael@0: """ michael@0: start_ms = int(time.time()) * 1000 michael@0: michael@0: result = None michael@0: for test in tests: michael@0: # We're only running one test at a time, so this TestResults object will michael@0: # hold only one result. michael@0: suite, test_name = test.split('.') michael@0: result = self._RunJavaTest(fname, suite, test_name) michael@0: # A non-empty list means the test did not pass. michael@0: if result.GetAllBroken(): michael@0: break michael@0: michael@0: duration_ms = int(time.time()) * 1000 - start_ms michael@0: michael@0: # Do something with result. michael@0: return self._ProcessResults(result, start_ms, duration_ms) michael@0: michael@0: def _ProcessResults(self, result, start_ms, duration_ms): michael@0: """Translates a Java test result into a Python result for this test. michael@0: michael@0: The TestRunner class that we use under the covers will return a test result michael@0: for that specific Java test. However, to make reporting clearer, we have michael@0: this method to abstract that detail and instead report that as a failure of michael@0: this particular test case while still including the Java stack trace. michael@0: michael@0: Args: michael@0: result: TestResults with a single Java test result michael@0: start_ms: the time the test started michael@0: duration_ms: the length of the test michael@0: michael@0: Returns: michael@0: A TestResults object containing a result for this Python test. michael@0: """ michael@0: test_results = TestResults() michael@0: michael@0: # If our test is in broken, then it crashed/failed. michael@0: broken = result.GetAllBroken() michael@0: if broken: michael@0: # Since we have run only one test, take the first and only item. michael@0: single_result = broken[0] michael@0: michael@0: log = single_result.log michael@0: if not log: michael@0: log = 'No logging information.' michael@0: michael@0: python_result = SingleTestResult(self.qualified_name, start_ms, michael@0: duration_ms, michael@0: log) michael@0: michael@0: # Figure out where the test belonged. There's probably a cleaner way of michael@0: # doing this. michael@0: if single_result in result.crashed: michael@0: test_results.crashed = [python_result] michael@0: elif single_result in result.failed: michael@0: test_results.failed = [python_result] michael@0: elif single_result in result.unknown: michael@0: test_results.unknown = [python_result] michael@0: michael@0: else: michael@0: python_result = SingleTestResult(self.qualified_name, start_ms, michael@0: duration_ms) michael@0: test_results.ok = [python_result] michael@0: michael@0: return test_results michael@0: michael@0: def _ComposeFullTestName(self, fname, suite, test): michael@0: package_name = self._GetPackageName(fname) michael@0: return package_name + '.' + suite + '#' + test michael@0: michael@0: def _GetPackageName(self, fname): michael@0: """Extracts the package name from the test file path.""" michael@0: dirname = os.path.dirname(fname) michael@0: package = dirname[dirname.rfind(BASE_ROOT) + len(BASE_ROOT):] michael@0: return package.replace(os.sep, '.')