Chromium Code Reviews| Index: mojo/tools/run_mojo_python_tests.py |
| diff --git a/mojo/tools/run_mojo_python_tests.py b/mojo/tools/run_mojo_python_tests.py |
| index 48b723cfd35f7738d45630c77514f2a75e1cf564..c4999ec717d8be8ee86f832db3d0428bb66de4a8 100755 |
| --- a/mojo/tools/run_mojo_python_tests.py |
| +++ b/mojo/tools/run_mojo_python_tests.py |
| @@ -1,25 +1,31 @@ |
| -#!/bin/python |
| +#!/usr/bin/python |
|
viettrungluu
2014/04/28 21:17:20
I think we mostly use "/usr/bin/env python" (and I
Dirk Pranke
2014/04/28 21:20:28
Hm. I didn't think that was either pervasive or st
|
| # Copyright 2014 The Chromium Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| import optparse |
| import os |
| +import re |
| import sys |
| import unittest |
| -# TODO(dpranke): crbug.com/364709 . We should add the ability to return |
| -# JSONified results data for the bots. |
| - |
| - |
| -def main(): |
| +def Main(): |
|
viettrungluu
2014/04/28 21:17:20
FWIW, both the Chromium OS and the internal Google
Dirk Pranke
2014/04/28 21:20:28
Ok. I thought it was Main, but I could easily be w
|
| parser = optparse.OptionParser() |
| + parser.usage = 'run_mojo_python_tests.py [options] [tests...]' |
| parser.add_option('-v', '--verbose', action='count', default=0) |
| + parser.add_option('--unexpected-failures', action='store', |
|
viettrungluu
2014/04/28 21:17:20
Add: metavar='FILENAME' or something like that?
(
Dirk Pranke
2014/04/28 21:20:28
Ok. I can switch to argparse if you'd prefer.
|
| + help=('path to write a list of any tests that fail ' |
| + 'unexpectedly.')) |
| + parser.epilog = ('If --unexpected-failures is passed, a list of the tests ' |
| + 'that failed (one per line) will be written to the file. ' |
| + 'If no tests failed, the file will be truncated (empty). ' |
| + 'If the test run did not completely properly, or something ' |
| + 'else weird happened, any existing file will be left ' |
| + 'unmodified. ' |
| + 'If --unexpected-failures is *not* passed, any existing ' |
| + 'file will be ignored and left unmodified.') |
| options, args = parser.parse_args() |
| - if args: |
| - parser.usage() |
| - return 1 |
| chromium_src_dir = os.path.join(os.path.dirname(__file__), |
| os.pardir, |
| @@ -27,14 +33,42 @@ def main(): |
| loader = unittest.loader.TestLoader() |
| print "Running Python unit tests under mojo/public/tools/bindings/pylib ..." |
| - suite = loader.discover(os.path.join(chromium_src_dir, 'mojo', 'public', |
| - 'tools', 'bindings', 'pylib'), |
| - pattern='*_unittest.py') |
| - runner = unittest.runner.TextTestRunner(verbosity=(options.verbose+1)) |
| + pylib_dir = os.path.join(chromium_src_dir, 'mojo', 'public', |
| + 'tools', 'bindings', 'pylib') |
| + if args: |
| + if not pylib_dir in sys.path: |
| + sys.path.append(pylib_dir) |
| + suite = unittest.TestSuite() |
| + for test_name in args: |
| + suite.addTests(loader.loadTestsFromName(test_name)) |
| + else: |
| + suite = loader.discover(pylib_dir, pattern='*_unittest.py') |
| + |
| + runner = unittest.runner.TextTestRunner(verbosity=(options.verbose + 1)) |
| result = runner.run(suite) |
| + |
| + if options.unexpected_failures: |
| + WriteUnexpectedFailures(result, options.unexpected_failures) |
| + |
| return 0 if result.wasSuccessful() else 1 |
| +def WriteUnexpectedFailures(result, path): |
| + |
| + # This regex and UnitTestName() extracts the test_name in a way |
| + # that can be handed back to the loader successfully. |
| + |
| + test_description = re.compile("(\w+) \(([\w.]+)\)") |
| + |
| + def UnitTestName(test): |
| + m = test_description.match(str(test)) |
| + return "%s.%s" % (m.group(2), m.group(1)) |
| + |
| + with open(path, 'w') as fp: |
| + for (test, _) in result.failures + result.errors: |
| + fp.write(UnitTestName(test) + '\n') |
| + |
| + |
| if __name__ == '__main__': |
| - sys.exit(main()) |
| + sys.exit(Main()) |