OLD | NEW |
| (Empty) |
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 | |
3 # found in the LICENSE file. | |
4 | |
5 """Base class for Android Python-driven tests. | |
6 | |
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 | |
9 inherit from this case and add their tests in that case. | |
10 | |
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 | |
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, | |
15 and calls TearDown. | |
16 | |
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 | |
19 the Java test results into Python test results. | |
20 """ | |
21 | |
22 import logging | |
23 import os | |
24 import time | |
25 | |
26 import android_commands | |
27 import apk_info | |
28 from run_java_tests import TestRunner | |
29 from test_result import SingleTestResult, TestResults | |
30 | |
31 | |
32 # aka the parent of com.google.android | |
33 BASE_ROOT = 'src' + os.sep | |
34 | |
35 | |
36 class PythonTestBase(object): | |
37 """Base class for Python-driven tests.""" | |
38 | |
39 def __init__(self, test_name): | |
40 # test_name must match one of the test methods defined on a subclass which | |
41 # inherits from this class. | |
42 # It's stored so we can do the attr lookup on demand, allowing this class | |
43 # to be pickled, a requirement for the multiprocessing module. | |
44 self.test_name = test_name | |
45 class_name = self.__class__.__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) | |
53 self.ports_to_forward = [] | |
54 | |
55 def TearDown(self): | |
56 pass | |
57 | |
58 def GetOutDir(self): | |
59 return os.path.join(os.environ['CHROME_SRC'], 'out', | |
60 self.options.build_type) | |
61 | |
62 def Run(self): | |
63 logging.warning('Running Python-driven test: %s', self.test_name) | |
64 return getattr(self, self.test_name)() | |
65 | |
66 def _RunJavaTest(self, fname, suite, test): | |
67 """Runs a single Java test with a Java TestRunner. | |
68 | |
69 Args: | |
70 fname: filename for the test (e.g. foo/bar/baz/tests/FooTest.py) | |
71 suite: name of the Java test suite (e.g. FooTest) | |
72 test: name of the test method to run (e.g. testFooBar) | |
73 | |
74 Returns: | |
75 TestResults object with a single test result. | |
76 """ | |
77 test = self._ComposeFullTestName(fname, suite, test) | |
78 apks = [apk_info.ApkInfo(self.options.test_apk_path, | |
79 self.options.test_apk_jar_path)] | |
80 java_test_runner = TestRunner(self.options, self.device_id, [test], False, | |
81 self.shard_index, | |
82 apks, | |
83 self.ports_to_forward) | |
84 return java_test_runner.Run() | |
85 | |
86 def _RunJavaTests(self, fname, tests): | |
87 """Calls a list of tests and stops at the first test failure. | |
88 | |
89 This method iterates until either it encounters a non-passing test or it | |
90 exhausts the list of tests. Then it returns the appropriate Python result. | |
91 | |
92 Args: | |
93 fname: filename for the Python test | |
94 tests: a list of Java test names which will be run | |
95 | |
96 Returns: | |
97 A TestResults object containing a result for this Python test. | |
98 """ | |
99 start_ms = int(time.time()) * 1000 | |
100 | |
101 result = None | |
102 for test in tests: | |
103 # We're only running one test at a time, so this TestResults object will | |
104 # hold only one result. | |
105 suite, test_name = test.split('.') | |
106 result = self._RunJavaTest(fname, suite, test_name) | |
107 # A non-empty list means the test did not pass. | |
108 if result.GetAllBroken(): | |
109 break | |
110 | |
111 duration_ms = int(time.time()) * 1000 - start_ms | |
112 | |
113 # Do something with result. | |
114 return self._ProcessResults(result, start_ms, duration_ms) | |
115 | |
116 def _ProcessResults(self, result, start_ms, duration_ms): | |
117 """Translates a Java test result into a Python result for this test. | |
118 | |
119 The TestRunner class that we use under the covers will return a test result | |
120 for that specific Java test. However, to make reporting clearer, we have | |
121 this method to abstract that detail and instead report that as a failure of | |
122 this particular test case while still including the Java stack trace. | |
123 | |
124 Args: | |
125 result: TestResults with a single Java test result | |
126 start_ms: the time the test started | |
127 duration_ms: the length of the test | |
128 | |
129 Returns: | |
130 A TestResults object containing a result for this Python test. | |
131 """ | |
132 test_results = TestResults() | |
133 | |
134 # If our test is in broken, then it crashed/failed. | |
135 broken = result.GetAllBroken() | |
136 if broken: | |
137 # Since we have run only one test, take the first and only item. | |
138 single_result = broken[0] | |
139 | |
140 log = single_result.log | |
141 if not log: | |
142 log = 'No logging information.' | |
143 | |
144 python_result = SingleTestResult(self.qualified_name, start_ms, | |
145 duration_ms, | |
146 log) | |
147 | |
148 # Figure out where the test belonged. There's probably a cleaner way of | |
149 # doing this. | |
150 if single_result in result.crashed: | |
151 test_results.crashed = [python_result] | |
152 elif single_result in result.failed: | |
153 test_results.failed = [python_result] | |
154 elif single_result in result.unknown: | |
155 test_results.unknown = [python_result] | |
156 | |
157 else: | |
158 python_result = SingleTestResult(self.qualified_name, start_ms, | |
159 duration_ms) | |
160 test_results.ok = [python_result] | |
161 | |
162 return test_results | |
163 | |
164 def _ComposeFullTestName(self, fname, suite, test): | |
165 package_name = self._GetPackageName(fname) | |
166 return package_name + '.' + suite + '#' + test | |
167 | |
168 def _GetPackageName(self, fname): | |
169 """Extracts the package name from the test file path.""" | |
170 dirname = os.path.dirname(fname) | |
171 package = dirname[dirname.rfind(BASE_ROOT) + len(BASE_ROOT):] | |
172 return package.replace(os.sep, '.') | |
OLD | NEW |