diff options
| author | Thomi Richards <thomi.richards@canonical.com> | 2012-02-29 13:01:48 +1300 |
|---|---|---|
| committer | Thomi Richards <thomi.richards@canonical.com> | 2012-02-29 13:01:48 +1300 |
| commit | ed810717be03f87b1aa9fe8d2077d1f390af9f6b (patch) | |
| tree | fb2dd4cbf24d5e1ccc7fb1ad7401511945a61880 /tools | |
| parent | 004f8e7e8822f1a5da741134bb9fd228073029ba (diff) | |
Can now run specific tests.
(bzr r2035.2.3)
Diffstat (limited to 'tools')
| -rwxr-xr-x | tools/run_autopilot | 88 |
1 files changed, 51 insertions, 37 deletions
diff --git a/tools/run_autopilot b/tools/run_autopilot index 5d7525d39..bf4013a50 100755 --- a/tools/run_autopilot +++ b/tools/run_autopilot @@ -6,6 +6,8 @@ import os.path import sys from textwrap import dedent from argparse import ArgumentParser +from unittest.loader import TestLoader +from unittest.runner import TextTestRunner # list autopilot depends here, with the form: @@ -60,7 +62,7 @@ def ensure_autopilot_is_importable(): "../tests/autopilot") ap_dir = os.path.realpath(ap_dir) sys.path.append(ap_dir) - print "Patching sys.path to include local autopilot folder '%s'" % ap_dir + print "Patching sys.path to include local autopilot folder '%s'\n" % ap_dir def parse_arguments(): @@ -73,6 +75,7 @@ def parse_arguments(): help='Write test result report to file. Defaults to stdout') parser_run.add_argument('-f', "--format", choices=['text', 'xml'], default='text', required=False, help='Specify desired output format. Default is "text".') + parser_run.add_argument("test", nargs="*", help="Specify tests to run, as listed by the 'list' command") parser_list = subparsers.add_parser('list', help="List autopilot tests") args = parser.parse_args() @@ -80,49 +83,60 @@ def parse_arguments(): return args -def main(): - # These are here so we can print a nice error message if they're not found, - # rather than getting an ImportError. THey're guaranteed to be installed by - # the time main() is called. - import junitxml +def list_tests(): + """Print a list of tests we find inside autopilot.tests.""" from testtools import iterate_tests - from unittest.loader import TestLoader - from unittest.runner import TextTestRunner + loader = TestLoader() + test_suite = loader.discover('autopilot.tests') + print "Listing all autopilot tests:" + print + for test in iterate_tests(test_suite): + has_scenarios = hasattr(test, "scenarios") + if has_scenarios: + print " * " + test.id() + else: + print test.id() - args = parse_arguments() +def run_tests(args): + """Run tests, using input from `args`.""" + import junitxml loader = TestLoader() - test_suite = loader.discover('autopilot.tests') + if args.test: + test_suite = loader.loadTestsFromNames(args.test) + else: + test_suite = loader.discover('autopilot.tests') + + if args.output == None: + results_stream = sys.stdout + else: + try: + path = os.path.dirname(args.output) + if path != '' and not os.path.exists(path): + os.makedirs(path) + results_stream = open(args.output, 'w') + except: + results_stream = sys.stdout + if args.format == "xml": + result = junitxml.JUnitXmlResult(results_stream) + result.startTestRun() + test_suite.run(result) + result.stopTestRun() + results_stream.close() + if not result.wasSuccessful: + exit(1) + elif args.format == "text": + runner = TextTestRunner(stream=results_stream) + success = runner.run(test_suite).wasSuccessful() + if not success: + exit(1) +def main(): + args = parse_arguments() if args.mode == 'list': - print "Listing all autopilot tests:" - print - for test in iterate_tests(test_suite): - print "\t" + test.id() + list_tests() elif args.mode == 'run': - if args.output == None: - results_stream = sys.stdout - else: - try: - path = os.path.dirname(args.output) - if path != '' and not os.path.exists(path): - os.makedirs(path) - results_stream = open(args.output, 'w') - except: - results_stream = sys.stdout - if args.format == "xml": - result = junitxml.JUnitXmlResult(results_stream) - result.startTestRun() - test_suite.run(result) - result.stopTestRun() - results_stream.close() - if not result.wasSuccessful: - exit(1) - elif args.format == "text": - runner = TextTestRunner(stream=results_stream) - success = runner.run(test_suite).wasSuccessful() - if not success: - exit(1) + run_tests(args) if __name__ == "__main__": if not check_depends(): |
