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 class ModuleTestSuite(unittest.TestSuite):
20 def __init__(self, project):
21 super(ModuleTestSuite, self).__init__()
22 self._project = project
25 def recreateEmptyVersion(self):
26 return ModuleTestSuite(self._project)
28 def run(self, result):
31 super(ModuleTestSuite, self).run(result)
40 self._bc = browser_controller.BrowserController(self._project)
41 self._bc.NavigateToPath('/tvcm/unittest/module_test_case_runner.html')
43 global _currently_active_module_test_suite
44 assert _currently_active_module_test_suite == None
45 _currently_active_module_test_suite = self
52 global _currently_active_module_test_suite
53 _currently_active_module_test_suite = None
55 def DiscoverTestsInModule(project, start_path):
56 if test_runner.PY_ONLY_TESTS:
57 return unittest.TestSuite()
59 if not browser_controller.IsSupported():
60 raise Exception('Cannot run all tests: telemetry could not be found')
61 rl = resource_loader.ResourceLoader(project)
63 test_modules = project.FindAllTestModuleNames(start_path=start_path)
65 bc = browser_controller.BrowserController(project)
67 bc.NavigateToPath('/tvcm/unittest/module_test_case_runner.html')
69 if bc.EvaluateJavaScript('tvcm.hasPanic()'):
70 raise Exception('Runner failure: %s' % bc.EvaluateJavaScript('tvcm.getPanicText()'))
72 tests = bc.EvaluateThennableAndWait(
73 'discoverTestsInModules(%s)' % json.dumps(test_modules))
75 if bc.EvaluateJavaScript('tvcm.hasPanic()'):
76 raise Exception('Test loading failure: %s' % bc.EvaluateJavaScript('tvcm.getPanicText()'))
78 suite = ModuleTestSuite(project)
79 for fully_qualified_test_name in tests:
80 suite.addTest(ModuleTestCase(fully_qualified_test_name))
87 class ModuleTestCase(unittest.TestCase):
88 def __init__(self, fully_qualified_test_name):
89 super(ModuleTestCase, self).__init__(methodName='runTest')
90 self.fully_qualified_test_name = fully_qualified_test_name
93 return self.fully_qualified_test_name
95 def shortDescription(self):
99 i = self.fully_qualified_test_name.rfind('.')
100 modname = self.fully_qualified_test_name[0:i]
101 testname = self.fully_qualified_test_name[i+1:]
102 return '%s (%s)' % (testname, modname)
105 mts = _currently_active_module_test_suite
106 assert mts, 'Something is wrong: ModuleTestCase can only be run inside a ModuleTestSuite.run()'
109 res = bc.EvaluateThennableAndWait(
110 'runTestNamed(%s)' % json.dumps(self.fully_qualified_test_name))