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 2bee0abd9d71115d57d41cfa18c7f6225a7723b2..c96aba36d2cd0f9c27b1929ddd1ae53f1f983755 100755 |
--- a/mojo/tools/run_mojo_python_tests.py |
+++ b/mojo/tools/run_mojo_python_tests.py |
@@ -5,21 +5,27 @@ |
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(): |
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', metavar='FILENAME', action='store', |
+ 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()) |