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