OLD | NEW |
---|---|
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2013 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 instrumentation tests. |
6 | 6 |
7 This test case is intended to serve as the base class for any Python-driven | 7 This test case serves as the base class for any test cases which intend |
8 tests. It is similar to the Python unitttest module in that the user's tests | 8 to run instrumentation tests. It provides a constructor which accepts |
9 inherit from this case and add their tests in that case. | 9 the relevant options and stores them in object variables. |
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 """ | 10 """ |
21 | 11 |
22 import logging | |
23 import os | 12 import os |
24 import time | 13 import time |
25 | 14 |
26 from pylib import android_commands | |
27 from pylib.base import base_test_result | 15 from pylib.base import base_test_result |
28 from pylib.instrumentation import test_package | 16 from pylib.instrumentation import test_package |
29 from pylib.instrumentation import test_result | 17 from pylib.instrumentation import test_result |
30 from pylib.instrumentation import test_runner | 18 from pylib.instrumentation import test_runner |
31 | 19 |
20 import python_test_base | |
32 | 21 |
33 # aka the parent of com.google.android | 22 # aka the parent of com.google.android |
34 BASE_ROOT = 'src' + os.sep | 23 BASE_ROOT = 'src' + os.sep |
35 | 24 |
36 | 25 |
37 class PythonTestBase(object): | 26 class InstrumentationPythonTestBase(python_test_base.PythonTestBase): |
bulach
2013/07/23 17:27:52
hehe, everytime I look into "host-driven" or "pyth
gkanwar1
2013/07/23 18:38:55
So the reason we need a new class to derive from i
frankf
2013/07/24 01:04:55
I think we should follow the accepted python unitt
gkanwar1
2013/07/24 17:38:04
Done.
| |
38 """Base class for Python-driven tests.""" | 27 """Base class for Python-driven instrumentation tests. |
39 | 28 |
40 def __init__(self, test_name): | 29 Args: |
41 # test_name must match one of the test methods defined on a subclass which | 30 test_name: The name of the method to run as the test. |
42 # inherits from this class. | 31 test_apk_path: Path to the test apk file. |
43 # It's stored so we can do the attr lookup on demand, allowing this class | 32 test_apk_jar_path: Path to the accompanying jar file. |
44 # to be pickled, a requirement for the multiprocessing module. | 33 other args: All passed through to the instrumentation test runner. See the |
45 self.test_name = test_name | 34 test runner docs for details. |
46 class_name = self.__class__.__name__ | 35 """ |
47 self.qualified_name = class_name + '.' + self.test_name | |
48 | 36 |
49 def SetUp(self, options): | 37 def __init__(self, test_name, test_apk_path, test_apk_jar_path, |
50 self.options = options | 38 test_data, install_apk, save_perf_json, screenshot_failures, |
51 self.shard_index = self.options.shard_index | 39 tool, wait_for_debugger, disable_assertions): |
52 self.device_id = self.options.device_id | 40 super(InstrumentationPythonTestBase, self).__init__(test_name) |
53 self.adb = android_commands.AndroidCommands(self.device_id) | 41 self.test_apk_path = test_apk_path |
54 self.ports_to_forward = [] | 42 self.test_apk_jar_path = test_apk_jar_path |
55 | 43 self.test_data = test_data |
56 def TearDown(self): | 44 self.install_apk = install_apk |
57 pass | 45 self.save_perf_json = save_perf_json |
58 | 46 self.screenshot_failures = screenshot_failures |
59 def GetOutDir(self): | 47 self.wait_for_debugger = wait_for_debugger |
60 return os.path.join(os.environ['CHROME_SRC'], 'out', | 48 self.tool = tool |
61 self.options.build_type) | 49 self.disable_assertions = disable_assertions |
62 | |
63 def Run(self): | |
64 logging.warning('Running Python-driven test: %s', self.test_name) | |
65 return getattr(self, self.test_name)() | |
66 | 50 |
67 def _RunJavaTest(self, fname, suite, test): | 51 def _RunJavaTest(self, fname, suite, test): |
68 """Runs a single Java test with a Java TestRunner. | 52 """Runs a single Java test with a Java TestRunner. |
69 | 53 |
70 Args: | 54 Args: |
71 fname: filename for the test (e.g. foo/bar/baz/tests/FooTest.py) | 55 fname: filename for the test (e.g. foo/bar/baz/tests/FooTest.py) |
72 suite: name of the Java test suite (e.g. FooTest) | 56 suite: name of the Java test suite (e.g. FooTest) |
73 test: name of the test method to run (e.g. testFooBar) | 57 test: name of the test method to run (e.g. testFooBar) |
74 | 58 |
75 Returns: | 59 Returns: |
76 TestRunResults object with a single test result. | 60 TestRunResults object with a single test result. |
77 """ | 61 """ |
78 test = self._ComposeFullTestName(fname, suite, test) | 62 test = self._ComposeFullTestName(fname, suite, test) |
79 test_pkg = test_package.TestPackage( | 63 test_pkg = test_package.TestPackage( |
80 self.options.test_apk_path, self.options.test_apk_jar_path) | 64 self.test_apk_path, self.test_apk_jar_path) |
81 java_test_runner = test_runner.TestRunner(self.options.build_type, | 65 java_test_runner = test_runner.TestRunner(self.build_type, |
82 self.options.test_data, | 66 self.test_data, |
83 self.options.install_apk, | 67 self.install_apk, |
84 self.options.save_perf_json, | 68 self.save_perf_json, |
85 self.options.screenshot_failures, | 69 self.screenshot_failures, |
86 self.options.tool, | 70 self.tool, |
87 self.options.wait_for_debugger, | 71 self.wait_for_debugger, |
88 self.options.disable_assertions, | 72 self.disable_assertions, |
89 self.options.push_deps, | 73 self.push_deps, |
90 self.options.cleanup_test_files, | 74 self.cleanup_test_files, |
91 self.device_id, | 75 self.device_id, |
92 self.shard_index, test_pkg, | 76 self.shard_index, test_pkg, []) |
93 self.ports_to_forward) | |
94 try: | 77 try: |
95 java_test_runner.SetUp() | 78 java_test_runner.SetUp() |
96 return java_test_runner.RunTest(test)[0] | 79 return java_test_runner.RunTest(test)[0] |
97 finally: | 80 finally: |
98 java_test_runner.TearDown() | 81 java_test_runner.TearDown() |
99 | 82 |
100 def _RunJavaTests(self, fname, tests): | 83 def _RunJavaTests(self, fname, tests): |
101 """Calls a list of tests and stops at the first test failure. | 84 """Calls a list of tests and stops at the first test failure. |
102 | 85 |
103 This method iterates until either it encounters a non-passing test or it | 86 This method iterates until either it encounters a non-passing test or it |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
135 | 118 |
136 def _ComposeFullTestName(self, fname, suite, test): | 119 def _ComposeFullTestName(self, fname, suite, test): |
137 package_name = self._GetPackageName(fname) | 120 package_name = self._GetPackageName(fname) |
138 return package_name + '.' + suite + '#' + test | 121 return package_name + '.' + suite + '#' + test |
139 | 122 |
140 def _GetPackageName(self, fname): | 123 def _GetPackageName(self, fname): |
141 """Extracts the package name from the test file path.""" | 124 """Extracts the package name from the test file path.""" |
142 dirname = os.path.dirname(fname) | 125 dirname = os.path.dirname(fname) |
143 package = dirname[dirname.rfind(BASE_ROOT) + len(BASE_ROOT):] | 126 package = dirname[dirname.rfind(BASE_ROOT) + len(BASE_ROOT):] |
144 return package.replace(os.sep, '.') | 127 return package.replace(os.sep, '.') |
OLD | NEW |