summaryrefslogtreecommitdiffstats
path: root/run_test.py
blob: 06ad64191eabf0a773075d72b36fdf31a01509ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python

import sys
import unittest
import tests
import string
from optparse import OptionParser


def getFullSuiteName(suite, full_suite_names):
    suites = []
    for full_suite_name in full_suite_names:
        if full_suite_name.lower().find(suite) != -1:
            suites.append(full_suite_name)

    return suites


def main():
    usage = 'Usage: %prog [options] [arg1 arg2 ... argN]'
    version = '%prog 0.1'
    parser = OptionParser(usage=usage, version=version)
    parser.add_option('-l', '--list', action='store_true', default=False,
                      help='print all available test suites and exit')
    parser.add_option('-i', '--interactive', action='store_true', default=False,
                      help='run in the interactive mode')

    (options, args) = parser.parse_args(sys.argv[1:])
    if not args and not (options.list or options.interactive):
        parser.error('you must specify either an option or an argument\n'
                     'Try "%s --help" for more information' % sys.argv[0])

    print 'Searching for test suites'
    available_suites = tests.getAvailableSuites()
    if not available_suites:
        print 'No test suites available, exiting'
        sys.exit(1)

    suite_names = available_suites.keys()
    suite_names.sort()

    if options.list and not options.interactive:
        print '\nAvailable test suites:'
        for suite_name in suite_names:
            print suite_name
        sys.exit(0)

    suites_to_run = []
    if options.interactive:
        print '\nAvailable test suites:'
        suite_num = 0
        for suite_name in suite_names:
            print '[%3d] %s' % (suite_num, suite_name)
            suite_num += 1

        try:
            input_string = raw_input('\nType in the test you want to run, '
                                     'or "all" to run all listed tests: ')
        except KeyboardInterrupt:
            print '\nAborted by user'
            sys.exit(1)

        for arg in input_string.split():
            if arg.isdigit():
                arg = int(arg)
                try:
                    args.append(suite_names[arg])
                except KeyError:
                    pass
            else:
                args.append(arg)

    args = map(string.lower, args)
    if 'all' in args:
        suites_to_run = suite_names[:]
    else:
        for arg in args:
            matching_suites = getFullSuiteName(arg, suite_names)
            suites_to_run.extend(filter(lambda suite: suite not in suites_to_run,
                                        matching_suites))

    if suites_to_run:
        suites_to_run.sort()
        print 'Running test suites: %s' % suites_to_run
        suite = unittest.TestSuite([available_suites[suite_name]
                                    for suite_name in suites_to_run])

        try:
            result = unittest.TextTestRunner(verbosity=2).run(suite)
        except KeyboardInterrupt:
            print '\nAborted by user'
            sys.exit(1)

        if result.wasSuccessful():
            print '\nAll test suites OK'
            sys.exit(0)
        else:
            print '\nTest suites finished with %d errors and %d failures' % (len(result.errors),
                                                                             len(result.failures))
            sys.exit(2)
    else:
        print 'No test suites matching your criteria found, exiting'
        sys.exit(1)


if __name__ == '__main__':
    main()