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
74 global _currently_active_module_test_suite
75 _currently_active_module_test_suite = None
77 def DiscoverTestsInModule(project, start_path):
79 return _DiscoverTestsInModuleImpl(project, start_path)
82 sys.stderr.write("While discovering tests in %s:\n" % repr(project))
84 sys.stderr.write("\n\n")
87 def _DiscoverTestsInModuleImpl(project, start_path):
88 if test_runner.PY_ONLY_TESTS:
89 return unittest.TestSuite()
91 if not browser_controller.IsSupported():
92 raise Exception('Cannot run all tests: telemetry could not be found')
93 rl = resource_loader.ResourceLoader(project)
95 test_modules = [x.name for x in
96 project.FindAllTestModuleResources(start_path=start_path)]
98 bc = browser_controller.BrowserController(project)
100 _NavigateToTestCaseRunner(bc)
101 if bc.EvaluateJavaScript('tvcm.hasPanic()'):
102 raise Exception('Runner failure: %s' % bc.EvaluateJavaScript('tvcm.getPanicText()'))
104 tests = bc.EvaluateThennableAndWait(
105 'discoverTestsInModules(%s)' % json.dumps(test_modules))
107 if bc.EvaluateJavaScript('tvcm.hasPanic()'):
108 raise Exception('Test loading failure: %s' % bc.EvaluateJavaScript('tvcm.getPanicText()'))
110 suite = ModuleTestSuite(project)
111 for fully_qualified_test_name in tests:
112 suite.addTest(ModuleTestCase(fully_qualified_test_name))
119 class ModuleTestCase(unittest.TestCase):
120 def __init__(self, fully_qualified_test_name):
121 super(ModuleTestCase, self).__init__(methodName='runTest')
122 self.fully_qualified_test_name = fully_qualified_test_name
125 return self.fully_qualified_test_name
127 def shortDescription(self):
131 i = self.fully_qualified_test_name.rfind('.')
132 modname = self.fully_qualified_test_name[0:i]
133 testname = self.fully_qualified_test_name[i+1:]
134 return '%s (%s)' % (testname, modname)
137 global _currently_active_module_test_suite
138 mts = _currently_active_module_test_suite
139 assert mts, 'Something is wrong: ModuleTestCase can only be run inside a ModuleTestSuite.run()'
142 res = bc.EvaluateThennableAndWait(
143 'runTestNamed(%s)' % json.dumps(self.fully_qualified_test_name))