2 # Copyright 2011 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
15 def discover(dir, filters):
16 if hasattr(unittest.TestLoader, 'discover'):
17 return unittest.TestLoader().discover(dir, '*')
19 # poor mans unittest.discover
20 loader = unittest.TestLoader()
23 for (dirpath, dirnames, filenames) in os.walk(dir):
24 for filename in [x for x in filenames if re.match('.*_test\.py$', x)]:
25 if filename.startswith('.') or filename.startswith('_'):
27 fqn = dirpath.replace('/', '.') + '.' + re.match('(.+)\.py$', filename).group(1)
31 module = __import__(fqn,fromlist=[True])
33 print "While importing [%s]\n" % fqn
37 def test_is_selected(name):
43 if hasattr(module, 'suite'):
44 base_suite = module.suite()
46 base_suite = loader.loadTestsFromModule(module)
47 new_suite = unittest.TestSuite()
49 if isinstance(t, unittest.TestSuite):
51 if test_is_selected(i.id()):
53 elif isinstance(t, unittest.TestCase):
54 if test_is_selected(t.id()):
57 raise Exception("Wtf, expected TestSuite or TestCase, got %s" % t)
59 if new_suite.countTestCases():
60 subsuites.append(new_suite)
62 return unittest.TestSuite(subsuites)
65 parser = optparse.OptionParser()
67 '-v', '--verbose', action='count', default=0,
68 help='Increase verbosity level (repeat as needed)')
69 parser.add_option('--debug', dest='debug', action='store_true', default=False, help='Break into pdb when an assertion fails')
70 parser.add_option('--incremental', dest='incremental', action='store_true', default=False, help='Run tests one at a time.')
71 parser.add_option('--stop', dest='stop_on_error', action='store_true', default=False, help='Stop running tests on error.')
72 (options, args) = parser.parse_args()
74 if options.verbose >= 2:
75 logging.basicConfig(level=logging.DEBUG)
77 logging.basicConfig(level=logging.INFO)
79 logging.basicConfig(level=logging.WARNING)
81 # install hook on set_trace if --debug
84 class DebuggingAssertionError(exceptions.AssertionError):
85 def __init__(self, *args):
86 exceptions.AssertionError.__init__(self, *args)
87 print "Assertion failed, entering PDB..."
89 if hasattr(sys, '_getframe'):
90 pdb.Pdb().set_trace(sys._getframe().f_back.f_back)
93 unittest.TestCase.failureException = DebuggingAssertionError
97 traceback.print_exception(*args)
102 browser.debug_mode = True
105 def hook(exc, value, tb):
107 if not str(value).startswith("_noprint"):
108 traceback.print_exception(exc, value, tb)
109 import src.message_loop
110 if src.message_loop.is_main_loop_running():
111 if not str(value).startswith("_noprint"):
112 print "Untrapped exception! Exiting message loop with exception."
113 src.message_loop.quit_main_loop(quit_with_exception=True)
115 sys.excepthook = hook
117 # make sure cwd is the base directory!
118 os.chdir(os.path.dirname(__file__))
121 suites = discover('trace_event_impl', args)
123 suites = discover('trace_event_impl', ['.*'])
125 r = unittest.TextTestRunner()
126 if not options.incremental:
128 if res.wasSuccessful():
134 if isinstance(s, unittest.TestSuite):
136 print '----------------------------------------------------------------------'
137 print 'Running %s' % str(t)
139 if not res.wasSuccessful():
141 if options.stop_on_error:
143 if ok == False and options.stop_on_error:
147 if not res.wasSuccessful():
149 if options.stop_on_error:
155 if __name__ == "__main__":