| OLD | NEW | 
|---|
|  | (Empty) | 
| 1 # Copyright (c) 2012 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. |  | 
| 4 import fnmatch |  | 
| 5 import logging |  | 
| 6 import os |  | 
| 7 import traceback |  | 
| 8 import unittest |  | 
| 9 |  | 
| 10 from telemetry.core import browser_options |  | 
| 11 from telemetry.test import gtest_testrunner |  | 
| 12 from telemetry.test import options_for_unittests |  | 
| 13 |  | 
| 14 |  | 
| 15 def RequiresBrowserOfType(*types): |  | 
| 16   def wrap(func): |  | 
| 17     func._requires_browser_types = types |  | 
| 18     return func |  | 
| 19   return wrap |  | 
| 20 |  | 
| 21 |  | 
| 22 def Discover(start_dir, pattern = 'test*.py', top_level_dir = None): |  | 
| 23   modules = [] |  | 
| 24   for dirpath, _, filenames in os.walk(start_dir): |  | 
| 25     for filename in filenames: |  | 
| 26       if not filename.endswith('.py'): |  | 
| 27         continue |  | 
| 28 |  | 
| 29       if not fnmatch.fnmatch(filename, pattern): |  | 
| 30         continue |  | 
| 31 |  | 
| 32       if filename.startswith('.') or filename.startswith('_'): |  | 
| 33         continue |  | 
| 34       name, _ = os.path.splitext(filename) |  | 
| 35 |  | 
| 36       relpath = os.path.relpath(dirpath, top_level_dir) |  | 
| 37       fqn = relpath.replace('/', '.') + '.' + name |  | 
| 38 |  | 
| 39       # load the module |  | 
| 40       try: |  | 
| 41         module = __import__(fqn, fromlist=[True]) |  | 
| 42       except Exception: |  | 
| 43         print 'While importing [%s]\n' % fqn |  | 
| 44         traceback.print_exc() |  | 
| 45         continue |  | 
| 46       modules.append(module) |  | 
| 47 |  | 
| 48   loader = unittest.defaultTestLoader |  | 
| 49   loader.suiteClass = gtest_testrunner.GTestTestSuite |  | 
| 50   subsuites = [] |  | 
| 51   for module in modules: |  | 
| 52     if hasattr(module, 'suite'): |  | 
| 53       new_suite = module.suite() |  | 
| 54     else: |  | 
| 55       new_suite = loader.loadTestsFromModule(module) |  | 
| 56     if new_suite.countTestCases(): |  | 
| 57       subsuites.append(new_suite) |  | 
| 58   return gtest_testrunner.GTestTestSuite(subsuites) |  | 
| 59 |  | 
| 60 |  | 
| 61 def FilterSuite(suite, predicate): |  | 
| 62   new_suite = suite.__class__() |  | 
| 63   for x in suite: |  | 
| 64     if isinstance(x, unittest.TestSuite): |  | 
| 65       subsuite = FilterSuite(x, predicate) |  | 
| 66       if subsuite.countTestCases() == 0: |  | 
| 67         continue |  | 
| 68 |  | 
| 69       new_suite.addTest(subsuite) |  | 
| 70       continue |  | 
| 71 |  | 
| 72     assert isinstance(x, unittest.TestCase) |  | 
| 73     if predicate(x): |  | 
| 74       new_suite.addTest(x) |  | 
| 75 |  | 
| 76   return new_suite |  | 
| 77 |  | 
| 78 |  | 
| 79 def DiscoverAndRunTests(dir_name, args, top_level_dir, runner=None): |  | 
| 80   if not runner: |  | 
| 81     runner = gtest_testrunner.GTestTestRunner(inner=True) |  | 
| 82 |  | 
| 83   suite = Discover(dir_name, '*_unittest.py', top_level_dir) |  | 
| 84 |  | 
| 85   def IsTestSelected(test): |  | 
| 86     if len(args) != 0: |  | 
| 87       found = False |  | 
| 88       for name in args: |  | 
| 89         if name in test.id(): |  | 
| 90           found = True |  | 
| 91       if not found: |  | 
| 92         return False |  | 
| 93 |  | 
| 94     if hasattr(test, '_testMethodName'): |  | 
| 95       method = getattr(test, test._testMethodName) # pylint: disable=W0212 |  | 
| 96       if hasattr(method, '_requires_browser_types'): |  | 
| 97         types = method._requires_browser_types # pylint: disable=W0212 |  | 
| 98         if options_for_unittests.GetBrowserType() not in types: |  | 
| 99           logging.debug('Skipping test %s because it requires %s' % |  | 
| 100                         (test.id(), types)) |  | 
| 101           return False |  | 
| 102 |  | 
| 103     return True |  | 
| 104 |  | 
| 105   filtered_suite = FilterSuite(suite, IsTestSelected) |  | 
| 106   test_result = runner.run(filtered_suite) |  | 
| 107   return test_result |  | 
| 108 |  | 
| 109 |  | 
| 110 def Main(args, start_dir, top_level_dir, runner=None): |  | 
| 111   """Unit test suite that collects all test cases for telemetry.""" |  | 
| 112   default_options = browser_options.BrowserOptions() |  | 
| 113   default_options.browser_type = 'any' |  | 
| 114 |  | 
| 115   parser = default_options.CreateParser('run_tests [options] [test names]') |  | 
| 116   parser.add_option('--repeat-count', dest='run_test_repeat_count', |  | 
| 117                     type='int', default=1, |  | 
| 118                     help='Repeats each a provided number of times.') |  | 
| 119 |  | 
| 120   _, args = parser.parse_args(args) |  | 
| 121 |  | 
| 122   if default_options.verbosity == 0: |  | 
| 123     logging.getLogger().setLevel(logging.ERROR) |  | 
| 124 |  | 
| 125   from telemetry.core import browser_finder |  | 
| 126   browser_to_create = browser_finder.FindBrowser(default_options) |  | 
| 127   if browser_to_create == None: |  | 
| 128     logging.error('No browser found of type %s. Cannot run tests.', |  | 
| 129                   default_options.browser_type) |  | 
| 130     logging.error('Re-run with --browser=list to see available browser types.') |  | 
| 131     return 1 |  | 
| 132 |  | 
| 133   options_for_unittests.Set(default_options, |  | 
| 134                             browser_to_create.browser_type) |  | 
| 135   olddir = os.getcwd() |  | 
| 136   try: |  | 
| 137     os.chdir(top_level_dir) |  | 
| 138     success = True |  | 
| 139     for _ in range( |  | 
| 140         default_options.run_test_repeat_count): # pylint: disable=E1101 |  | 
| 141       success = success and DiscoverAndRunTests(start_dir, args, top_level_dir, |  | 
| 142                                                 runner) |  | 
| 143     if success: |  | 
| 144       return 0 |  | 
| 145   finally: |  | 
| 146     os.chdir(olddir) |  | 
| 147     options_for_unittests.Set(None, None) |  | 
| 148 |  | 
| 149   return 1 |  | 
| OLD | NEW | 
|---|