Home | History | Annotate | Download | only in test
      1 # Copyright (C) 2012 Google, Inc.
      2 #
      3 # Redistribution and use in source and binary forms, with or without
      4 # modification, are permitted provided that the following conditions
      5 # are met:
      6 # 1.  Redistributions of source code must retain the above copyright
      7 #     notice, this list of conditions and the following disclaimer.
      8 # 2.  Redistributions in binary form must reproduce the above copyright
      9 #     notice, this list of conditions and the following disclaimer in the
     10 #     documentation and/or other materials provided with the distribution.
     11 #
     12 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
     13 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     14 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     15 # DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
     16 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     17 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     18 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     19 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     20 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     21 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     22 
     23 """code to actually run a list of python tests."""
     24 
     25 import re
     26 import sys
     27 import time
     28 import unittest
     29 
     30 from webkitpy.common import message_pool
     31 
     32 _test_description = re.compile("(\w+) \(([\w.]+)\)")
     33 
     34 
     35 def unit_test_name(test):
     36     m = _test_description.match(str(test))
     37     return "%s.%s" % (m.group(2), m.group(1))
     38 
     39 
     40 class Runner(object):
     41     def __init__(self, printer, loader, webkit_finder):
     42         self.printer = printer
     43         self.loader = loader
     44         self.webkit_finder = webkit_finder
     45         self.tests_run = 0
     46         self.errors = []
     47         self.failures = []
     48         self.worker_factory = lambda caller: _Worker(caller, self.loader, self.webkit_finder)
     49 
     50     def run(self, test_names, num_workers):
     51         if not test_names:
     52             return
     53         num_workers = min(num_workers, len(test_names))
     54         with message_pool.get(self, self.worker_factory, num_workers) as pool:
     55             pool.run(('test', test_name) for test_name in test_names)
     56 
     57     def handle(self, message_name, source, test_name, delay=None, failures=None, errors=None):
     58         if message_name == 'started_test':
     59             self.printer.print_started_test(source, test_name)
     60             return
     61 
     62         self.tests_run += 1
     63         if failures:
     64             self.failures.append((test_name, failures))
     65         if errors:
     66             self.errors.append((test_name, errors))
     67         self.printer.print_finished_test(source, test_name, delay, failures, errors)
     68 
     69 
     70 class _Worker(object):
     71     def __init__(self, caller, loader, webkit_finder):
     72         self._caller = caller
     73         self._loader = loader
     74 
     75         # FIXME: unittest2 and coverage need to be in sys.path for their internal imports to work.
     76         thirdparty_path = webkit_finder.path_from_webkit_base('Tools', 'Scripts', 'webkitpy', 'thirdparty')
     77         if not thirdparty_path in sys.path:
     78             sys.path.append(thirdparty_path)
     79 
     80 
     81     def handle(self, message_name, source, test_name):
     82         assert message_name == 'test'
     83         result = unittest.TestResult()
     84         start = time.time()
     85         self._caller.post('started_test', test_name)
     86 
     87         # We will need to rework this if a test_name results in multiple tests.
     88         self._loader.loadTestsFromName(test_name, None).run(result)
     89         self._caller.post('finished_test', test_name, time.time() - start,
     90             [failure[1] for failure in result.failures], [error[1] for error in result.errors])
     91