OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Base class for Android Python-driven tests. | 5 """Base class for Android Python-driven tests. |
6 | 6 |
7 This test case is intended to serve as the base class for any Python-driven | 7 This test case is intended to serve as the base class for any Python-driven |
8 tests. It is similar to the Python unitttest module in that the user's tests | 8 tests. It is similar to the Python unitttest module in that the user's tests |
9 inherit from this case and add their tests in that case. | 9 inherit from this case and add their tests in that case. |
10 | 10 |
11 When a PythonTestBase object is instantiated, its purpose is to run only one of | 11 When a PythonTestBase object is instantiated, its purpose is to run only one of |
12 its tests. The test runner gives it the name of the test the instance will | 12 its tests. The test runner gives it the name of the test the instance will |
13 run. The test runner calls SetUp with the Android device ID which the test will | 13 run. The test runner calls SetUp with the Android device ID which the test will |
14 run against. The runner runs the test method itself, collecting the result, | 14 run against. The runner runs the test method itself, collecting the result, |
15 and calls TearDown. | 15 and calls TearDown. |
16 | 16 |
17 Tests can basically do whatever they want in the test methods, such as call | 17 Tests can basically do whatever they want in the test methods, such as call |
18 Java tests using _RunJavaTests. Those methods have the advantage of massaging | 18 Java tests using _RunJavaTests. Those methods have the advantage of massaging |
19 the Java test results into Python test results. | 19 the Java test results into Python test results. |
20 """ | 20 """ |
21 | 21 |
22 import logging | 22 import logging |
23 import os | 23 import os |
24 import time | 24 import time |
25 | 25 |
26 import android_commands | 26 import android_commands |
27 import apk_info | 27 import apk_info |
28 from run_java_tests import TestRunner | 28 from run_java_tests import TestRunner |
29 import test_options_parser | |
30 from test_result import SingleTestResult, TestResults | 29 from test_result import SingleTestResult, TestResults |
31 | 30 |
32 | 31 |
33 # aka the parent of com.google.android | 32 # aka the parent of com.google.android |
34 BASE_ROOT = 'src' + os.sep | 33 BASE_ROOT = 'src' + os.sep |
35 | 34 |
36 | 35 |
37 class PythonTestBase(object): | 36 class PythonTestBase(object): |
38 """Base class for Python-driven tests.""" | 37 """Base class for Python-driven tests.""" |
39 | 38 |
40 def __init__(self, test_name): | 39 def __init__(self, test_name): |
41 # test_name must match one of the test methods defined on a subclass which | 40 # test_name must match one of the test methods defined on a subclass which |
42 # inherits from this class. | 41 # inherits from this class. |
43 # It's stored so we can do the attr lookup on demand, allowing this class | 42 # It's stored so we can do the attr lookup on demand, allowing this class |
44 # to be pickled, a requirement for the multiprocessing module. | 43 # to be pickled, a requirement for the multiprocessing module. |
45 self.test_name = test_name | 44 self.test_name = test_name |
46 class_name = self.__class__.__name__ | 45 class_name = self.__class__.__name__ |
47 self.qualified_name = class_name + '.' + self.test_name | 46 self.qualified_name = class_name + '.' + self.test_name |
| 47 |
| 48 def SetUp(self, options): |
| 49 self.options = options |
| 50 self.shard_index = self.options.shard_index |
| 51 self.device_id = self.options.device_id |
| 52 self.adb = android_commands.AndroidCommands(self.device_id) |
48 self.ports_to_forward = [] | 53 self.ports_to_forward = [] |
49 | 54 |
50 def SetUp(self, device_id, shard_index): | |
51 self.shard_index = shard_index | |
52 self.device_id = device_id | |
53 self.adb = android_commands.AndroidCommands(self.device_id) | |
54 | |
55 def TearDown(self): | 55 def TearDown(self): |
56 pass | 56 pass |
57 | 57 |
58 def Run(self): | 58 def Run(self): |
59 logging.warning('Running Python-driven test: %s', self.test_name) | 59 logging.warning('Running Python-driven test: %s', self.test_name) |
60 return getattr(self, self.test_name)() | 60 return getattr(self, self.test_name)() |
61 | 61 |
62 def _RunJavaTest(self, fname, suite, test): | 62 def _RunJavaTest(self, fname, suite, test): |
63 """Runs a single Java test with a Java TestRunner. | 63 """Runs a single Java test with a Java TestRunner. |
64 | 64 |
65 Args: | 65 Args: |
66 fname: filename for the test (e.g. foo/bar/baz/tests/FooTest.py) | 66 fname: filename for the test (e.g. foo/bar/baz/tests/FooTest.py) |
67 suite: name of the Java test suite (e.g. FooTest) | 67 suite: name of the Java test suite (e.g. FooTest) |
68 test: name of the test method to run (e.g. testFooBar) | 68 test: name of the test method to run (e.g. testFooBar) |
69 | 69 |
70 Returns: | 70 Returns: |
71 TestResults object with a single test result. | 71 TestResults object with a single test result. |
72 """ | 72 """ |
73 test = self._ComposeFullTestName(fname, suite, test) | 73 test = self._ComposeFullTestName(fname, suite, test) |
74 # Get a set of default options | 74 apks = [apk_info.ApkInfo(self.options.test_apk_path, |
75 options = test_options_parser.ParseInstrumentationArgs(['']) | 75 self.options.test_apk_jar_path)] |
76 apks = [apk_info.ApkInfo(options.test_apk_path, options.test_apk_jar_path)] | 76 java_test_runner = TestRunner(self.options, self.device_id, [test], False, |
77 java_test_runner = TestRunner(options, self.device_id, [test], False, | |
78 self.shard_index, | 77 self.shard_index, |
79 apks, | 78 apks, |
80 self.ports_to_forward) | 79 self.ports_to_forward) |
81 return java_test_runner.Run() | 80 return java_test_runner.Run() |
82 | 81 |
83 def _RunJavaTests(self, fname, tests): | 82 def _RunJavaTests(self, fname, tests): |
84 """Calls a list of tests and stops at the first test failure. | 83 """Calls a list of tests and stops at the first test failure. |
85 | 84 |
86 This method iterates until either it encounters a non-passing test or it | 85 This method iterates until either it encounters a non-passing test or it |
87 exhausts the list of tests. Then it returns the appropriate Python result. | 86 exhausts the list of tests. Then it returns the appropriate Python result. |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 | 159 |
161 def _ComposeFullTestName(self, fname, suite, test): | 160 def _ComposeFullTestName(self, fname, suite, test): |
162 package_name = self._GetPackageName(fname) | 161 package_name = self._GetPackageName(fname) |
163 return package_name + '.' + suite + '#' + test | 162 return package_name + '.' + suite + '#' + test |
164 | 163 |
165 def _GetPackageName(self, fname): | 164 def _GetPackageName(self, fname): |
166 """Extracts the package name from the test file path.""" | 165 """Extracts the package name from the test file path.""" |
167 dirname = os.path.dirname(fname) | 166 dirname = os.path.dirname(fname) |
168 package = dirname[dirname.rfind(BASE_ROOT) + len(BASE_ROOT):] | 167 package = dirname[dirname.rfind(BASE_ROOT) + len(BASE_ROOT):] |
169 return package.replace(os.sep, '.') | 168 return package.replace(os.sep, '.') |
OLD | NEW |