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 from pylib import android_commands | |
27 from pylib.base import base_test_result | |
28 from pylib.instrumentation import test_options | |
29 from pylib.instrumentation import test_package | |
30 from pylib.instrumentation import test_result | |
31 from pylib.instrumentation import test_runner | |
32 | |
33 | |
34 # aka the parent of com.google.android | |
35 BASE_ROOT = 'src' + os.sep | |
36 | |
37 | |
38 class PythonTestBase(object): | |
39 """Base class for Python-driven tests.""" | |
40 | |
41 def __init__(self, test_name): | |
42 # test_name must match one of the test methods defined on a subclass which | |
43 # inherits from this class. | |
44 # It's stored so we can do the attr lookup on demand, allowing this class | |
45 # to be pickled, a requirement for the multiprocessing module. | |
46 self.test_name = test_name | |
47 class_name = self.__class__.__name__ | |
48 self.qualified_name = class_name + '.' + self.test_name | |
49 | |
50 def SetUp(self, options): | |
51 self.options = options | |
52 self.shard_index = self.options.shard_index | |
53 self.device_id = self.options.device_id | |
54 self.adb = android_commands.AndroidCommands(self.device_id) | |
55 self.ports_to_forward = [] | |
56 | |
57 def TearDown(self): | |
58 pass | |
59 | |
60 def GetOutDir(self): | |
61 return os.path.join(os.environ['CHROME_SRC'], 'out', | |
62 self.options.build_type) | |
63 | |
64 def Run(self): | |
65 logging.warning('Running Python-driven test: %s', self.test_name) | |
66 return getattr(self, self.test_name)() | |
67 | |
68 def _RunJavaTest(self, fname, suite, test): | |
69 """Runs a single Java test with a Java TestRunner. | |
70 | |
71 Args: | |
72 fname: filename for the test (e.g. foo/bar/baz/tests/FooTest.py) | |
73 suite: name of the Java test suite (e.g. FooTest) | |
74 test: name of the test method to run (e.g. testFooBar) | |
75 | |
76 Returns: | |
77 TestRunResults object with a single test result. | |
78 """ | |
79 test = self._ComposeFullTestName(fname, suite, test) | |
80 test_pkg = test_package.TestPackage( | |
81 self.options.test_apk_path, self.options.test_apk_jar_path) | |
82 instrumentation_options = test_options.InstrumentationOptions( | |
83 self.options.build_type, | |
84 self.options.tool, | |
85 self.options.cleanup_test_files, | |
86 self.options.push_deps, | |
87 self.options.annotations, | |
88 self.options.exclude_annotations, | |
89 self.options.test_filter, | |
90 self.options.test_data, | |
91 self.options.save_perf_json, | |
92 self.options.screenshot_failures, | |
93 self.options.disable_assertions, | |
94 self.options.wait_for_debugger, | |
95 self.options.test_apk, | |
96 self.options.test_apk_path, | |
97 self.options.test_apk_jar_path) | |
98 java_test_runner = test_runner.TestRunner(instrumentation_options, | |
99 self.device_id, | |
100 self.shard_index, test_pkg, | |
101 self.ports_to_forward) | |
102 try: | |
103 java_test_runner.SetUp() | |
104 return java_test_runner.RunTest(test)[0] | |
105 finally: | |
106 java_test_runner.TearDown() | |
107 | |
108 def _RunJavaTests(self, fname, tests): | |
109 """Calls a list of tests and stops at the first test failure. | |
110 | |
111 This method iterates until either it encounters a non-passing test or it | |
112 exhausts the list of tests. Then it returns the appropriate Python result. | |
113 | |
114 Args: | |
115 fname: filename for the Python test | |
116 tests: a list of Java test names which will be run | |
117 | |
118 Returns: | |
119 A TestRunResults object containing a result for this Python test. | |
120 """ | |
121 test_type = base_test_result.ResultType.PASS | |
122 log = '' | |
123 | |
124 start_ms = int(time.time()) * 1000 | |
125 for test in tests: | |
126 # We're only running one test at a time, so this TestRunResults object | |
127 # will hold only one result. | |
128 suite, test_name = test.split('.') | |
129 java_results = self._RunJavaTest(fname, suite, test_name) | |
130 assert len(java_results.GetAll()) == 1 | |
131 if not java_results.DidRunPass(): | |
132 result = java_results.GetNotPass().pop() | |
133 log = result.GetLog() | |
134 test_type = result.GetType() | |
135 break | |
136 duration_ms = int(time.time()) * 1000 - start_ms | |
137 | |
138 python_results = base_test_result.TestRunResults() | |
139 python_results.AddResult( | |
140 test_result.InstrumentationTestResult( | |
141 self.qualified_name, test_type, start_ms, duration_ms, log=log)) | |
142 return python_results | |
143 | |
144 def _ComposeFullTestName(self, fname, suite, test): | |
145 package_name = self._GetPackageName(fname) | |
146 return package_name + '.' + suite + '#' + test | |
147 | |
148 def _GetPackageName(self, fname): | |
149 """Extracts the package name from the test file path.""" | |
150 dirname = os.path.dirname(fname) | |
151 package = dirname[dirname.rfind(BASE_ROOT) + len(BASE_ROOT):] | |
152 return package.replace(os.sep, '.') | |
OLD | NEW |