blob: 431863ef87962bee45fb8b71f260dfa7d82c6347 [file] [log] [blame]
Guido van Rossum152494a1996-12-20 03:12:20 +00001#! /usr/bin/env python
2
3"""Regression test.
4
5This will find all modules whose name is "test_*" in the test
6directory, and run them. Various command line options provide
7additional facilities.
8
9Command line options:
10
11-v: verbose -- print the name name of each test as it is being run
12-q: quiet -- don't print anything except if a test fails
13-g: generate -- write the output file for a test instead of comparing it
14-x: exclude -- arguments are tests to *exclude*
15
16If non-option arguments are present, they are names for tests to run,
17unless -x is given, in which case they are names for tests not to run.
18If no test names are given, all tests are run.
Guido van Rossumf58ed251997-03-07 21:04:33 +000019
20If -v is given *twice*, the tests themselves are run in verbose mode.
21This is incompatible with -g and does not compare test output files.
Guido van Rossum152494a1996-12-20 03:12:20 +000022"""
23
24import sys
25import string
26import os
27import getopt
Guido van Rossum9e48b271997-07-16 01:56:13 +000028import traceback
Guido van Rossum152494a1996-12-20 03:12:20 +000029
30import test_support
31
32def main():
33 try:
34 opts, args = getopt.getopt(sys.argv[1:], 'vgqx')
35 except getopt.error, msg:
36 print msg
37 print __doc__
38 sys.exit(2)
39 verbose = 0
40 quiet = 0
41 generate = 0
42 exclude = 0
43 for o, a in opts:
Guido van Rossumf58ed251997-03-07 21:04:33 +000044 if o == '-v': verbose = verbose+1
Guido van Rossum152494a1996-12-20 03:12:20 +000045 if o == '-q': quiet = 1
46 if o == '-g': generate = 1
47 if o == '-x': exclude = 1
Guido van Rossumf58ed251997-03-07 21:04:33 +000048 if generate and verbose>1:
49 print "-g and more than one -v don't go together!"
50 sys.exit(2)
Guido van Rossum152494a1996-12-20 03:12:20 +000051 good = []
52 bad = []
53 skipped = []
54 if exclude:
55 nottests[:0] = args
56 args = []
57 tests = args or findtests()
Guido van Rossumf58ed251997-03-07 21:04:33 +000058 test_support.verbose = verbose>1 # Tell tests to be moderately quiet
Guido van Rossum152494a1996-12-20 03:12:20 +000059 for test in tests:
60 if verbose:
61 print test
Guido van Rossumf58ed251997-03-07 21:04:33 +000062 ok = runtest(test, generate, verbose>1)
Guido van Rossum152494a1996-12-20 03:12:20 +000063 if ok > 0:
64 good.append(test)
65 elif ok == 0:
66 bad.append(test)
67 else:
68 if not quiet:
69 print "test", test,
70 print "skipped -- an optional feature could not be imported"
71 skipped.append(test)
72 if good and not quiet:
73 if not bad and not skipped and len(good) > 1:
74 print "All",
75 print count(len(good), "test"), "OK."
76 if bad:
77 print count(len(bad), "test"), "failed:",
78 print string.join(bad)
79 if skipped and not quiet:
80 print count(len(skipped), "test"), "skipped:",
81 print string.join(skipped)
82 sys.exit(len(bad) > 0)
83
84stdtests = [
85 'test_grammar',
86 'test_opcodes',
87 'test_operations',
88 'test_builtin',
89 'test_exceptions',
90 'test_types',
91 ]
92
93nottests = [
94 'test_support',
95 'test_b1',
96 'test_b2',
97 ]
98
99def findtests():
100 """Return a list of all applicable test modules."""
101 testdir = findtestdir()
102 names = os.listdir(testdir)
103 tests = []
104 for name in names:
105 if name[:5] == "test_" and name[-3:] == ".py":
106 modname = name[:-3]
107 if modname not in stdtests and modname not in nottests:
108 tests.append(modname)
109 tests.sort()
110 return stdtests + tests
111
Guido van Rossumf58ed251997-03-07 21:04:33 +0000112def runtest(test, generate, verbose2):
Guido van Rossum152494a1996-12-20 03:12:20 +0000113 test_support.unload(test)
114 testdir = findtestdir()
115 outputdir = os.path.join(testdir, "output")
116 outputfile = os.path.join(outputdir, test)
117 try:
118 if generate:
119 cfp = open(outputfile, "w")
Guido van Rossumf58ed251997-03-07 21:04:33 +0000120 elif verbose2:
121 cfp = sys.stdout
Guido van Rossum152494a1996-12-20 03:12:20 +0000122 else:
123 cfp = Compare(outputfile)
124 except IOError:
125 cfp = None
126 print "Warning: can't open", outputfile
127 try:
128 save_stdout = sys.stdout
129 try:
130 if cfp:
131 sys.stdout = cfp
132 print test # Output file starts with test name
133 __import__(test)
134 finally:
135 sys.stdout = save_stdout
136 except ImportError, msg:
137 return -1
138 except test_support.TestFailed, msg:
139 print "test", test, "failed --", msg
140 return 0
Guido van Rossum9e48b271997-07-16 01:56:13 +0000141 except:
142 print "test", test, "crashed --", sys.exc_type, ":", sys.exc_value
143 if verbose2:
144 traceback.print_exc(file=sys.stdout)
145 return 0
Guido van Rossum152494a1996-12-20 03:12:20 +0000146 else:
147 return 1
148
149def findtestdir():
150 if __name__ == '__main__':
151 file = sys.argv[0]
152 else:
153 file = __file__
154 testdir = os.path.dirname(file) or os.curdir
155 return testdir
156
157def count(n, word):
158 if n == 1:
159 return "%d %s" % (n, word)
160 else:
161 return "%d %ss" % (n, word)
162
163class Compare:
164
165 def __init__(self, filename):
166 self.fp = open(filename, 'r')
167
168 def write(self, data):
169 expected = self.fp.read(len(data))
170 if data <> expected:
171 raise test_support.TestFailed, \
172 'Writing: '+`data`+', expected: '+`expected`
173
174 def close(self):
175 leftover = self.fp.read()
176 if leftover:
177 raise test_support.TestFailed, 'Unread: '+`leftover`
178 self.fp.close()
179
180 def isatty(self):
181 return 0
182
183if __name__ == '__main__':
184 main()