| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 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 import inspect | 5 import inspect |
| 6 import re | 6 import re |
| 7 import unittest | 7 import unittest |
| 8 | 8 |
| 9 from py_utils import cloud_storage | 9 from py_utils import cloud_storage |
| 10 from telemetry.internal.browser import browser_finder | 10 from telemetry.internal.browser import browser_finder |
| 11 from telemetry.testing import browser_test_context | 11 from telemetry.testing import options_for_unittests |
| 12 from telemetry.util import wpr_modes | 12 from telemetry.util import wpr_modes |
| 13 | 13 |
| 14 | 14 |
| 15 class SeriallyExecutedBrowserTestCase(unittest.TestCase): | 15 class SeriallyExecutedBrowserTestCase(unittest.TestCase): |
| 16 def __init__(self, methodName): | 16 def __init__(self, methodName): |
| 17 super(SeriallyExecutedBrowserTestCase, self).__init__(methodName) | 17 super(SeriallyExecutedBrowserTestCase, self).__init__(methodName) |
| 18 self._private_methodname = methodName | 18 self._private_methodname = methodName |
| 19 | 19 |
| 20 def shortName(self): | 20 def shortName(self): |
| 21 """Returns the method name this test runs, without the package prefix.""" | 21 """Returns the method name this test runs, without the package prefix.""" |
| 22 return self._private_methodname | 22 return self._private_methodname |
| 23 | 23 |
| 24 @classmethod | 24 @classmethod |
| 25 def Name(cls): | 25 def Name(cls): |
| 26 return cls.__name__ | 26 return cls.__name__ |
| 27 | 27 |
| 28 @classmethod | 28 @classmethod |
| 29 def AddCommandlineArgs(cls, parser): | 29 def AddCommandlineArgs(cls, parser): |
| 30 pass | 30 pass |
| 31 | 31 |
| 32 @classmethod | 32 @classmethod |
| 33 def SetUpProcess(cls): | 33 def setUpClass(cls): |
| 34 """ Set up testing logic before running the test case. | 34 cls._finder_options = options_for_unittests.GetCopy() |
| 35 This is guaranteed to be called only once for all the tests before the test | |
| 36 suite runs. | |
| 37 """ | |
| 38 cls._finder_options = browser_test_context.GetCopy().finder_options | |
| 39 cls.platform = None | 35 cls.platform = None |
| 40 cls.browser = None | 36 cls.browser = None |
| 41 cls._browser_to_create = None | 37 cls._browser_to_create = None |
| 42 cls._browser_options = None | 38 cls._browser_options = None |
| 43 | 39 |
| 44 @classmethod | 40 @classmethod |
| 45 def SetBrowserOptions(cls, browser_options): | 41 def SetBrowserOptions(cls, browser_options): |
| 46 """Sets the browser option for the browser to create. | 42 """Sets the browser option for the browser to create. |
| 47 | 43 |
| 48 Args: | 44 Args: |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 | 85 |
| 90 cls.browser = cls._browser_to_create.Create(cls._browser_options) | 86 cls.browser = cls._browser_to_create.Create(cls._browser_options) |
| 91 | 87 |
| 92 @classmethod | 88 @classmethod |
| 93 def StopBrowser(cls): | 89 def StopBrowser(cls): |
| 94 assert cls.browser, 'Browser is not started' | 90 assert cls.browser, 'Browser is not started' |
| 95 cls.browser.Close() | 91 cls.browser.Close() |
| 96 cls.browser = None | 92 cls.browser = None |
| 97 | 93 |
| 98 @classmethod | 94 @classmethod |
| 99 def TearDownProcess(cls): | 95 def tearDownClass(cls): |
| 100 """ Tear down the testing logic after running the test cases. | |
| 101 This is guaranteed to be called only once for all the tests after the test | |
| 102 suite finishes running. | |
| 103 """ | |
| 104 | |
| 105 if cls.platform: | 96 if cls.platform: |
| 106 cls.platform.StopAllLocalServers() | 97 cls.platform.StopAllLocalServers() |
| 107 cls.platform.network_controller.Close() | 98 cls.platform.network_controller.Close() |
| 108 if cls.browser: | 99 if cls.browser: |
| 109 cls.StopBrowser() | 100 cls.StopBrowser() |
| 110 | 101 |
| 111 @classmethod | 102 @classmethod |
| 112 def SetStaticServerDirs(cls, dirs_path): | 103 def SetStaticServerDirs(cls, dirs_path): |
| 113 assert cls.platform | 104 assert cls.platform |
| 114 assert isinstance(dirs_path, list) | 105 assert isinstance(dirs_path, list) |
| (...skipping 29 matching lines...) Expand all Loading... |
| 144 ################################################################# | 135 ################################################################# |
| 145 | 136 |
| 146 Args: | 137 Args: |
| 147 module: the module which contains test cases classes. | 138 module: the module which contains test cases classes. |
| 148 | 139 |
| 149 Returns: | 140 Returns: |
| 150 an instance of unittest.TestSuite, which contains all the tests & generated | 141 an instance of unittest.TestSuite, which contains all the tests & generated |
| 151 test cases to be run. | 142 test cases to be run. |
| 152 """ | 143 """ |
| 153 suite = unittest.TestSuite() | 144 suite = unittest.TestSuite() |
| 154 test_context = browser_test_context.GetCopy() | 145 finder_options = options_for_unittests.GetCopy() |
| 155 if not test_context: | 146 if not hasattr(finder_options, 'browser_test_runner_running'): |
| 156 return suite | 147 return suite |
| 157 for _, obj in inspect.getmembers(module): | 148 for _, obj in inspect.getmembers(module): |
| 158 if (inspect.isclass(obj) and | 149 if (inspect.isclass(obj) and |
| 159 issubclass(obj, SeriallyExecutedBrowserTestCase)): | 150 issubclass(obj, SeriallyExecutedBrowserTestCase)): |
| 160 # We bail out early if this class doesn't match the targeted | |
| 161 # test_class in test_context to avoid calling GenerateTestCases | |
| 162 # for tests that we don't intend to run. This is to avoid possible errors | |
| 163 # in GenerateTestCases as the test class may define custom options in | |
| 164 # the finder_options object, and hence would raise error if they can't | |
| 165 # find their custom options in finder_options object. | |
| 166 if test_context.test_class != obj: | |
| 167 continue | |
| 168 for test in GenerateTestCases( | 151 for test in GenerateTestCases( |
| 169 test_class=obj, finder_options=test_context.finder_options): | 152 test_class=obj, finder_options=finder_options): |
| 170 if test.id() in test_context.test_case_ids_to_run: | 153 suite.addTest(test) |
| 171 suite.addTest(test) | |
| 172 return suite | 154 return suite |
| 173 | 155 |
| 174 | 156 |
| 175 def _GenerateTestMethod(based_method, args): | 157 def _GenerateTestMethod(based_method, args): |
| 176 return lambda self: based_method(self, *args) | 158 return lambda self: based_method(self, *args) |
| 177 | 159 |
| 178 | 160 |
| 179 _TEST_GENERATOR_PREFIX = 'GenerateTestCases_' | 161 _TEST_GENERATOR_PREFIX = 'GenerateTestCases_' |
| 180 _INVALID_TEST_NAME_RE = re.compile(r'[^a-zA-Z0-9_]') | 162 _INVALID_TEST_NAME_RE = re.compile(r'[^a-zA-Z0-9_]') |
| 181 | 163 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 201 assert hasattr(test_class, based_method_name), ( | 183 assert hasattr(test_class, based_method_name), ( |
| 202 '%s is specified but based method %s does not exist' % | 184 '%s is specified but based method %s does not exist' % |
| 203 (name, based_method_name)) | 185 (name, based_method_name)) |
| 204 based_method = getattr(test_class, based_method_name) | 186 based_method = getattr(test_class, based_method_name) |
| 205 for generated_test_name, args in method(finder_options): | 187 for generated_test_name, args in method(finder_options): |
| 206 _ValidateTestMethodname(generated_test_name) | 188 _ValidateTestMethodname(generated_test_name) |
| 207 setattr(test_class, generated_test_name, _GenerateTestMethod( | 189 setattr(test_class, generated_test_name, _GenerateTestMethod( |
| 208 based_method, args)) | 190 based_method, args)) |
| 209 test_cases.append(test_class(generated_test_name)) | 191 test_cases.append(test_class(generated_test_name)) |
| 210 return test_cases | 192 return test_cases |
| OLD | NEW |