1 # Copyright 2014 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.
10 from tvcm import dev_server
11 from tvcm import browser_controller
12 from tvcm import test_runner
13 from tvcm import resource_loader
16 _currently_active_module_test_suite = None
19 def _NavigateToTestCaseRunner(bc):
20 bc.NavigateToPath('/tvcm/unittest/module_test_case_runner.html')
21 bc.WaitForJavaScriptExpression('window.__readyToRun == true')
22 bc.WaitForJavaScriptExpression('window.tvcm !== undefined')
23 bc.WaitForJavaScriptExpression('window.tvcm.hasPanic !== undefined')
24 bc.WaitForJavaScriptExpression('window.discoverTestsInModules !== undefined')
25 bc.WaitForJavaScriptExpression('window.runTestNamed !== undefined')
28 class ModuleTestSuite(unittest.TestSuite):
29 def __init__(self, project):
30 super(ModuleTestSuite, self).__init__()
31 self._project = project
37 return self.recreateEmptyVersion()
39 def recreateEmptyVersion(self):
40 return ModuleTestSuite(self._project)
42 def __call__(self, *args):
43 return self.run(*args)
45 def run(self, result):
48 return super(ModuleTestSuite, self).run(result)
58 self._bc = browser_controller.BrowserController(self._project)
59 _NavigateToTestCaseRunner(self._bc)
65 global _currently_active_module_test_suite
66 assert _currently_active_module_test_suite == None
67 _currently_active_module_test_suite = self
68 self._bc.stdout_enabled = True
72 self._bc.stdout_enabled = False
76 global _currently_active_module_test_suite
77 _currently_active_module_test_suite = None
79 def DiscoverTestsInModule(project, start_path):
81 return _DiscoverTestsInModuleImpl(project, start_path)
84 sys.stderr.write("While discovering tests:\n")
85 sys.stderr.write(sys.exc_info()[1].message)
86 sys.stderr.write("\n\n")
89 def _DiscoverTestsInModuleImpl(project, start_path):
90 if test_runner.PY_ONLY_TESTS:
91 return unittest.TestSuite()
93 if not browser_controller.IsSupported():
94 raise Exception('Cannot run all tests: telemetry could not be found')
95 rl = resource_loader.ResourceLoader(project)
97 test_modules = [x.name for x in
98 project.FindAllTestModuleResources(start_path=start_path)]
100 bc = browser_controller.BrowserController(project)
102 _NavigateToTestCaseRunner(bc)
103 if bc.EvaluateJavaScript('tvcm.hasPanic()'):
104 raise Exception('Runner failure: %s' % bc.EvaluateJavaScript('tvcm.getPanicText()'))
106 tests = bc.EvaluateThennableAndWait(
107 'discoverTestsInModules(%s)' % json.dumps(test_modules))
109 if bc.EvaluateJavaScript('tvcm.hasPanic()'):
110 raise Exception('Test loading failure: %s' % bc.EvaluateJavaScript('tvcm.getPanicText()'))
112 suite = ModuleTestSuite(project)
113 for fully_qualified_test_name in tests:
114 suite.addTest(ModuleTestCase(fully_qualified_test_name))
121 class ModuleTestCase(unittest.TestCase):
122 def __init__(self, fully_qualified_test_name):
123 super(ModuleTestCase, self).__init__(methodName='runTest')
124 self.fully_qualified_test_name = fully_qualified_test_name
127 return self.fully_qualified_test_name
129 def shortDescription(self):
133 i = self.fully_qualified_test_name.rfind('.')
134 modname = self.fully_qualified_test_name[0:i]
135 testname = self.fully_qualified_test_name[i+1:]
136 return '%s (%s)' % (testname, modname)
139 global _currently_active_module_test_suite
140 mts = _currently_active_module_test_suite
141 assert mts, 'Something is wrong: ModuleTestCase can only be run inside a ModuleTestSuite.run()'
144 res = bc.EvaluateThennableAndWait(
145 'runTestNamed(%s)' % json.dumps(self.fully_qualified_test_name))