| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 import argparse | |
| 6 import os | |
| 7 import sys | |
| 8 import unittest | |
| 9 | |
| 10 import mopy.paths | |
| 11 | |
| 12 | |
| 13 class MojoPythonTestRunner(object): | |
| 14 """Helper class to run python tests on the bots.""" | |
| 15 | |
| 16 def __init__(self, test_dir): | |
| 17 self._test_dir = test_dir | |
| 18 | |
| 19 def run(self): | |
| 20 parser = argparse.ArgumentParser() | |
| 21 parser.add_argument('-v', '--verbose', action='count', default=0) | |
| 22 parser.add_argument('tests', nargs='*') | |
| 23 | |
| 24 self.add_custom_commandline_options(parser) | |
| 25 args = parser.parse_args() | |
| 26 self.apply_customization(args) | |
| 27 | |
| 28 loader = unittest.loader.TestLoader() | |
| 29 print "Running Python unit tests under %s..." % self._test_dir | |
| 30 | |
| 31 src_root = mopy.paths.Paths().src_root | |
| 32 pylib_dir = os.path.abspath(os.path.join(src_root, self._test_dir)) | |
| 33 if args.tests: | |
| 34 if pylib_dir not in sys.path: | |
| 35 sys.path.append(pylib_dir) | |
| 36 suite = unittest.TestSuite() | |
| 37 for test_name in args.tests: | |
| 38 suite.addTests(loader.loadTestsFromName(test_name)) | |
| 39 else: | |
| 40 suite = loader.discover(pylib_dir, pattern='*_unittest.py') | |
| 41 | |
| 42 runner = unittest.runner.TextTestRunner(verbosity=(args.verbose + 1)) | |
| 43 result = runner.run(suite) | |
| 44 return 0 if result.wasSuccessful() else 1 | |
| 45 | |
| 46 def add_custom_commandline_options(self, parser): | |
| 47 """Allow to add custom option to the runner script.""" | |
| 48 pass | |
| 49 | |
| 50 def apply_customization(self, args): | |
| 51 """Allow to apply any customization to the runner.""" | |
| 52 pass | |
| OLD | NEW |