OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 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 host-driven test cases. | 5 """Base class for host-driven test cases. |
6 | 6 |
7 This test case is intended to serve as the base class for any host-driven | 7 This test case is intended to serve as the base class for any host-driven |
8 test cases. It is similar to the Python unitttest module in that test cases | 8 test cases. It is similar to the Python unitttest module in that test cases |
9 inherit from this class and add methods which will be run as tests. | 9 inherit from this class and add methods which will be run as tests. |
10 | 10 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 | 69 |
70 def GetOutDir(self): | 70 def GetOutDir(self): |
71 return os.path.join(os.environ['CHROME_SRC'], 'out', | 71 return os.path.join(os.environ['CHROME_SRC'], 'out', |
72 constants.GetBuildType()) | 72 constants.GetBuildType()) |
73 | 73 |
74 def Run(self): | 74 def Run(self): |
75 logging.info('Running host-driven test: %s', self.tagged_name) | 75 logging.info('Running host-driven test: %s', self.tagged_name) |
76 # Get the test method on the derived class and execute it | 76 # Get the test method on the derived class and execute it |
77 return getattr(self, self.test_name)() | 77 return getattr(self, self.test_name)() |
78 | 78 |
79 def __RunJavaTest(self, package_name, test_case, test_method): | 79 def __RunJavaTest(self, test, test_pkg): |
80 """Runs a single Java test method with a Java TestRunner. | 80 """Runs a single Java test in a Java TestRunner. |
81 | 81 |
82 Args: | 82 Args: |
83 package_name: Package name in which the java tests live | 83 test: Fully qualified test name (ex. foo.bar.TestClass#testMethod) |
84 (e.g. foo.bar.baz.tests) | 84 test_pkg: TestPackage object. |
85 test_case: Name of the Java test case (e.g. FooTest) | |
86 test_method: Name of the test method to run (e.g. testFooBar) | |
87 | 85 |
88 Returns: | 86 Returns: |
89 TestRunResults object with a single test result. | 87 TestRunResults object with a single test result. |
90 """ | 88 """ |
91 test = '%s.%s#%s' % (package_name, test_case, test_method) | |
92 test_pkg = test_package.TestPackage( | |
93 self.instrumentation_options.test_apk_path, | |
94 self.instrumentation_options.test_apk_jar_path) | |
95 java_test_runner = test_runner.TestRunner(self.instrumentation_options, | 89 java_test_runner = test_runner.TestRunner(self.instrumentation_options, |
96 self.device_id, | 90 self.device_id, |
97 self.shard_index, test_pkg, | 91 self.shard_index, test_pkg, |
98 self.ports_to_forward) | 92 self.ports_to_forward) |
99 try: | 93 try: |
100 java_test_runner.SetUp() | 94 java_test_runner.SetUp() |
101 return java_test_runner.RunTest(test)[0] | 95 return java_test_runner.RunTest(test)[0] |
102 finally: | 96 finally: |
103 java_test_runner.TearDown() | 97 java_test_runner.TearDown() |
104 | 98 |
| 99 # TODO(gkanwar): Remove old method once downstream tests are updated |
105 def _RunJavaTests(self, package_name, tests): | 100 def _RunJavaTests(self, package_name, tests): |
| 101 """Calls a list of tests and stops at the first test failure.""" |
| 102 return self._RunJavaTestFilters(tests) |
| 103 |
| 104 def _RunJavaTestFilters(self, test_filters): |
106 """Calls a list of tests and stops at the first test failure. | 105 """Calls a list of tests and stops at the first test failure. |
107 | 106 |
108 This method iterates until either it encounters a non-passing test or it | 107 This method iterates until either it encounters a non-passing test or it |
109 exhausts the list of tests. Then it returns the appropriate overall result. | 108 exhausts the list of tests. Then it returns the appropriate overall result. |
110 | 109 |
111 Test cases may make use of this method internally to assist in running | 110 Test cases may make use of this method internally to assist in running |
112 instrumentation tests. This function relies on instrumentation_options | 111 instrumentation tests. This function relies on instrumentation_options |
113 being defined. | 112 being defined. |
114 | 113 |
115 Args: | 114 Args: |
116 package_name: Package name in which the java tests live | 115 test_filters: A list of Java test filters. |
117 (e.g. foo.bar.baz.tests) | |
118 tests: A list of Java test names which will be run | |
119 | 116 |
120 Returns: | 117 Returns: |
121 A TestRunResults object containing an overall result for this set of Java | 118 A TestRunResults object containing an overall result for this set of Java |
122 tests. If any Java tests do not pass, this is a fail overall. | 119 tests. If any Java tests do not pass, this is a fail overall. |
123 """ | 120 """ |
124 test_type = base_test_result.ResultType.PASS | 121 test_type = base_test_result.ResultType.PASS |
125 log = '' | 122 log = '' |
126 | 123 |
| 124 test_pkg = test_package.TestPackage( |
| 125 self.instrumentation_options.test_apk_path, |
| 126 self.instrumentation_options.test_apk_jar_path) |
| 127 |
127 start_ms = int(time.time()) * 1000 | 128 start_ms = int(time.time()) * 1000 |
128 for test in tests: | 129 done = False |
129 # We're only running one test at a time, so this TestRunResults object | 130 for test_filter in test_filters: |
130 # will hold only one result. | 131 tests = test_pkg._GetAllMatchingTests(None, None, test_filter) |
131 suite, test_name = test.split('.') | 132 # Filters should always result in >= 1 test. |
132 java_result = self.__RunJavaTest(package_name, suite, test_name) | 133 if len(tests) == 0: |
133 assert len(java_result.GetAll()) == 1 | 134 raise Exception('Java test filter "%s" returned no tests.' |
134 if not java_result.DidRunPass(): | 135 % test_filter) |
135 result = java_result.GetNotPass().pop() | 136 for test in tests: |
136 log = result.GetLog() | 137 # We're only running one test at a time, so this TestRunResults object |
137 test_type = result.GetType() | 138 # will hold only one result. |
| 139 java_result = self.__RunJavaTest(test, test_pkg) |
| 140 assert len(java_result.GetAll()) == 1 |
| 141 if not java_result.DidRunPass(): |
| 142 result = java_result.GetNotPass().pop() |
| 143 log = result.GetLog() |
| 144 test_type = result.GetType() |
| 145 done = True |
| 146 break |
| 147 if done: |
138 break | 148 break |
139 duration_ms = int(time.time()) * 1000 - start_ms | 149 duration_ms = int(time.time()) * 1000 - start_ms |
140 | 150 |
141 overall_result = base_test_result.TestRunResults() | 151 overall_result = base_test_result.TestRunResults() |
142 overall_result.AddResult( | 152 overall_result.AddResult( |
143 test_result.InstrumentationTestResult( | 153 test_result.InstrumentationTestResult( |
144 self.tagged_name, test_type, start_ms, duration_ms, log=log)) | 154 self.tagged_name, test_type, start_ms, duration_ms, log=log)) |
145 return overall_result | 155 return overall_result |
146 | 156 |
147 def __str__(self): | 157 def __str__(self): |
148 return self.tagged_name | 158 return self.tagged_name |
149 | 159 |
150 def __repr__(self): | 160 def __repr__(self): |
151 return self.tagged_name | 161 return self.tagged_name |
OLD | NEW |