runner.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. """code to actually run a list of python tests."""
  23. import re
  24. import time
  25. import unittest
  26. from webkitpy.common import message_pool
  27. _test_description = re.compile("(\w+) \(([\w.]+)\)")
  28. def unit_test_name(test):
  29. m = _test_description.match(str(test))
  30. return "%s.%s" % (m.group(2), m.group(1))
  31. class Runner(object):
  32. def __init__(self, printer, loader):
  33. self.printer = printer
  34. self.loader = loader
  35. self.tests_run = 0
  36. self.errors = []
  37. self.failures = []
  38. self.worker_factory = lambda caller: _Worker(caller, self.loader)
  39. def run(self, test_names, num_workers):
  40. if not test_names:
  41. return
  42. num_workers = min(num_workers, len(test_names))
  43. with message_pool.get(self, self.worker_factory, num_workers) as pool:
  44. pool.run(('test', test_name) for test_name in test_names)
  45. def handle(self, message_name, source, test_name, delay=None, failures=None, errors=None):
  46. if message_name == 'started_test':
  47. self.printer.print_started_test(source, test_name)
  48. return
  49. self.tests_run += 1
  50. if failures:
  51. self.failures.append((test_name, failures))
  52. if errors:
  53. self.errors.append((test_name, errors))
  54. self.printer.print_finished_test(source, test_name, delay, failures, errors)
  55. class _Worker(object):
  56. def __init__(self, caller, loader):
  57. self._caller = caller
  58. self._loader = loader
  59. def handle(self, message_name, source, test_name):
  60. assert message_name == 'test'
  61. result = unittest.TestResult()
  62. start = time.time()
  63. self._caller.post('started_test', test_name)
  64. # We will need to rework this if a test_name results in multiple tests.
  65. self._loader.loadTestsFromName(test_name, None).run(result)
  66. self._caller.post('finished_test', test_name, time.time() - start,
  67. [failure[1] for failure in result.failures], [error[1] for error in result.errors])