blob: f05f7645c4864ed04e4b8076cd6f51665772d56b [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
Barry Warsawa873b032000-08-03 15:50:37 +000011-v: verbose -- run tests in verbose mode with output to stdout
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-s: single -- run only a single test (see below)
16-r: random -- randomize test execution order
Neil Schemenauer8a00abc2000-10-13 01:32:42 +000017-l: findleaks -- if GC is available detect tests that leak memory
Trent Mickf29f47b2000-08-11 19:02:59 +000018--have-resources -- run tests that require large resources (time/space)
Guido van Rossum152494a1996-12-20 03:12:20 +000019
20If non-option arguments are present, they are names for tests to run,
21unless -x is given, in which case they are names for tests not to run.
22If no test names are given, all tests are run.
Guido van Rossumf58ed251997-03-07 21:04:33 +000023
Guido van Rossuma4122201997-08-18 20:08:24 +000024-v is incompatible with -g and does not compare test output files.
Barry Warsawe11e3de1999-01-28 19:51:51 +000025
Barry Warsaw22e41822001-02-23 18:31:40 +000026-s means to run only a single test and exit. This is useful when doing memory
27analysis on the Python interpreter (which tend to consume to many resources to
28run the full regression test non-stop). The file /tmp/pynexttest is read to
29find the next test to run. If this file is missing, the first test_*.py file
30in testdir or on the command line is used. (actually tempfile.gettempdir() is
31used instead of /tmp).
Barry Warsawe11e3de1999-01-28 19:51:51 +000032
Guido van Rossum152494a1996-12-20 03:12:20 +000033"""
34
35import sys
Guido van Rossum152494a1996-12-20 03:12:20 +000036import os
37import getopt
Guido van Rossum9e48b271997-07-16 01:56:13 +000038import traceback
Skip Montanaroab1c7912000-06-30 16:39:27 +000039import random
Guido van Rossum152494a1996-12-20 03:12:20 +000040
41import test_support
42
Skip Montanaroab1c7912000-06-30 16:39:27 +000043def main(tests=None, testdir=None, verbose=0, quiet=0, generate=0,
Neil Schemenauerd569f232000-09-22 15:29:28 +000044 exclude=0, single=0, randomize=0, findleaks=0,
Trent Mickf29f47b2000-08-11 19:02:59 +000045 use_large_resources=0):
Guido van Rossum6fd83b71998-08-01 17:04:08 +000046 """Execute a test suite.
47
Thomas Wouters7e474022000-07-16 12:04:32 +000048 This also parses command-line options and modifies its behavior
Fred Drake004d5e62000-10-23 17:22:08 +000049 accordingly.
Guido van Rossum6fd83b71998-08-01 17:04:08 +000050
51 tests -- a list of strings containing test names (optional)
52 testdir -- the directory in which to look for tests (optional)
53
54 Users other than the Python test suite will certainly want to
55 specify testdir; if it's omitted, the directory containing the
Fred Drake004d5e62000-10-23 17:22:08 +000056 Python test suite is searched for.
Guido van Rossum6fd83b71998-08-01 17:04:08 +000057
58 If the tests argument is omitted, the tests listed on the
59 command-line will be used. If that's empty, too, then all *.py
60 files beginning with test_ will be used.
Skip Montanaroab1c7912000-06-30 16:39:27 +000061
Barry Warsawa873b032000-08-03 15:50:37 +000062 The other seven default arguments (verbose, quiet, generate, exclude,
Neil Schemenauerd569f232000-09-22 15:29:28 +000063 single, randomize, and findleaks) allow programmers calling main()
Barry Warsawa873b032000-08-03 15:50:37 +000064 directly to set the values that would normally be set by flags on the
65 command line.
66
Guido van Rossum6fd83b71998-08-01 17:04:08 +000067 """
Fred Drake004d5e62000-10-23 17:22:08 +000068
Guido van Rossum152494a1996-12-20 03:12:20 +000069 try:
Trent Mickf29f47b2000-08-11 19:02:59 +000070 opts, args = getopt.getopt(sys.argv[1:], 'vgqxsrl', ['have-resources'])
Guido van Rossum152494a1996-12-20 03:12:20 +000071 except getopt.error, msg:
Guido van Rossum41360a41998-03-26 19:42:58 +000072 print msg
73 print __doc__
74 return 2
Guido van Rossum152494a1996-12-20 03:12:20 +000075 for o, a in opts:
Guido van Rossum41360a41998-03-26 19:42:58 +000076 if o == '-v': verbose = verbose+1
77 if o == '-q': quiet = 1; verbose = 0
78 if o == '-g': generate = 1
79 if o == '-x': exclude = 1
Barry Warsawe11e3de1999-01-28 19:51:51 +000080 if o == '-s': single = 1
Skip Montanaroab1c7912000-06-30 16:39:27 +000081 if o == '-r': randomize = 1
Neil Schemenauerd569f232000-09-22 15:29:28 +000082 if o == '-l': findleaks = 1
Trent Mickf29f47b2000-08-11 19:02:59 +000083 if o == '--have-resources': use_large_resources = 1
Guido van Rossuma4122201997-08-18 20:08:24 +000084 if generate and verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000085 print "-g and -v don't go together!"
86 return 2
Guido van Rossum152494a1996-12-20 03:12:20 +000087 good = []
88 bad = []
89 skipped = []
Barry Warsawe11e3de1999-01-28 19:51:51 +000090
Neil Schemenauerd569f232000-09-22 15:29:28 +000091 if findleaks:
Barry Warsawa873b032000-08-03 15:50:37 +000092 try:
93 import gc
94 except ImportError:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +000095 print 'No GC available, disabling findleaks.'
Neil Schemenauerd569f232000-09-22 15:29:28 +000096 findleaks = 0
Barry Warsawa873b032000-08-03 15:50:37 +000097 else:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +000098 # Uncomment the line below to report garbage that is not
99 # freeable by reference counting alone. By default only
100 # garbage that is not collectable by the GC is reported.
101 #gc.set_debug(gc.DEBUG_SAVEALL)
Neil Schemenauerd569f232000-09-22 15:29:28 +0000102 found_garbage = []
Barry Warsawa873b032000-08-03 15:50:37 +0000103
Barry Warsawe11e3de1999-01-28 19:51:51 +0000104 if single:
105 from tempfile import gettempdir
106 filename = os.path.join(gettempdir(), 'pynexttest')
107 try:
108 fp = open(filename, 'r')
Eric S. Raymondfc170b12001-02-09 11:51:27 +0000109 next = fp.read().strip()
Barry Warsawe11e3de1999-01-28 19:51:51 +0000110 tests = [next]
111 fp.close()
112 except IOError:
113 pass
Guido van Rossuma4122201997-08-18 20:08:24 +0000114 for i in range(len(args)):
Guido van Rossum41360a41998-03-26 19:42:58 +0000115 # Strip trailing ".py" from arguments
116 if args[i][-3:] == '.py':
117 args[i] = args[i][:-3]
Guido van Rossum6c74fea1998-08-25 12:29:08 +0000118 stdtests = STDTESTS[:]
119 nottests = NOTTESTS[:]
Guido van Rossum152494a1996-12-20 03:12:20 +0000120 if exclude:
Guido van Rossum6c74fea1998-08-25 12:29:08 +0000121 for arg in args:
122 if arg in stdtests:
123 stdtests.remove(arg)
124 nottests[:0] = args
Guido van Rossum41360a41998-03-26 19:42:58 +0000125 args = []
Guido van Rossum747e1ca1998-08-24 13:48:36 +0000126 tests = tests or args or findtests(testdir, stdtests, nottests)
Barry Warsawe11e3de1999-01-28 19:51:51 +0000127 if single:
128 tests = tests[:1]
Skip Montanaroab1c7912000-06-30 16:39:27 +0000129 if randomize:
130 random.shuffle(tests)
Guido van Rossum41360a41998-03-26 19:42:58 +0000131 test_support.verbose = verbose # Tell tests to be moderately quiet
Trent Mickf29f47b2000-08-11 19:02:59 +0000132 test_support.use_large_resources = use_large_resources
Guido van Rossum5796d262000-04-21 21:35:06 +0000133 save_modules = sys.modules.keys()
Guido van Rossum152494a1996-12-20 03:12:20 +0000134 for test in tests:
Guido van Rossum41360a41998-03-26 19:42:58 +0000135 if not quiet:
136 print test
Trent Mickf29f47b2000-08-11 19:02:59 +0000137 ok = runtest(test, generate, verbose, quiet, testdir)
Guido van Rossum41360a41998-03-26 19:42:58 +0000138 if ok > 0:
139 good.append(test)
140 elif ok == 0:
141 bad.append(test)
142 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000143 skipped.append(test)
Neil Schemenauerd569f232000-09-22 15:29:28 +0000144 if findleaks:
145 gc.collect()
146 if gc.garbage:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000147 print "Warning: test created", len(gc.garbage),
148 print "uncollectable object(s)."
149 # move the uncollectable objects somewhere so we don't see
150 # them again
Neil Schemenauerd569f232000-09-22 15:29:28 +0000151 found_garbage.extend(gc.garbage)
152 del gc.garbage[:]
Guido van Rossum5796d262000-04-21 21:35:06 +0000153 # Unload the newly imported modules (best effort finalization)
154 for module in sys.modules.keys():
Guido van Rossum51931142000-05-05 14:27:39 +0000155 if module not in save_modules and module.startswith("test."):
Guido van Rossum5796d262000-04-21 21:35:06 +0000156 test_support.unload(module)
Guido van Rossum152494a1996-12-20 03:12:20 +0000157 if good and not quiet:
Guido van Rossum41360a41998-03-26 19:42:58 +0000158 if not bad and not skipped and len(good) > 1:
159 print "All",
160 print count(len(good), "test"), "OK."
Tim Peters1a4d77b2000-12-30 22:21:22 +0000161 if verbose:
162 print "CAUTION: stdout isn't compared in verbose mode: a test"
163 print "that passes in verbose mode may fail without it."
Guido van Rossum152494a1996-12-20 03:12:20 +0000164 if bad:
Guido van Rossum41360a41998-03-26 19:42:58 +0000165 print count(len(bad), "test"), "failed:",
Eric S. Raymondfc170b12001-02-09 11:51:27 +0000166 print " ".join(bad)
Guido van Rossum152494a1996-12-20 03:12:20 +0000167 if skipped and not quiet:
Guido van Rossum41360a41998-03-26 19:42:58 +0000168 print count(len(skipped), "test"), "skipped:",
Eric S. Raymondfc170b12001-02-09 11:51:27 +0000169 print " ".join(skipped)
Barry Warsawe11e3de1999-01-28 19:51:51 +0000170
171 if single:
172 alltests = findtests(testdir, stdtests, nottests)
173 for i in range(len(alltests)):
174 if tests[0] == alltests[i]:
175 if i == len(alltests) - 1:
176 os.unlink(filename)
177 else:
178 fp = open(filename, 'w')
179 fp.write(alltests[i+1] + '\n')
180 fp.close()
181 break
182 else:
183 os.unlink(filename)
184
Guido van Rossume8387011997-08-14 19:40:34 +0000185 return len(bad) > 0
Guido van Rossum152494a1996-12-20 03:12:20 +0000186
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000187STDTESTS = [
Guido van Rossum152494a1996-12-20 03:12:20 +0000188 'test_grammar',
189 'test_opcodes',
190 'test_operations',
191 'test_builtin',
192 'test_exceptions',
193 'test_types',
194 ]
195
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000196NOTTESTS = [
Guido van Rossum152494a1996-12-20 03:12:20 +0000197 'test_support',
198 'test_b1',
199 'test_b2',
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +0000200 'test_future1',
201 'test_future2',
202 'test_future3',
203 'test_future4',
204 'test_future5',
205 'test_future6',
206 'test_future7',
Guido van Rossum152494a1996-12-20 03:12:20 +0000207 ]
208
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000209def findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS):
Guido van Rossum152494a1996-12-20 03:12:20 +0000210 """Return a list of all applicable test modules."""
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000211 if not testdir: testdir = findtestdir()
Guido van Rossum152494a1996-12-20 03:12:20 +0000212 names = os.listdir(testdir)
213 tests = []
214 for name in names:
Guido van Rossum41360a41998-03-26 19:42:58 +0000215 if name[:5] == "test_" and name[-3:] == ".py":
216 modname = name[:-3]
217 if modname not in stdtests and modname not in nottests:
218 tests.append(modname)
Guido van Rossum152494a1996-12-20 03:12:20 +0000219 tests.sort()
220 return stdtests + tests
221
Trent Mickf29f47b2000-08-11 19:02:59 +0000222def runtest(test, generate, verbose, quiet, testdir = None):
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000223 """Run a single test.
224 test -- the name of the test
225 generate -- if true, generate output, instead of running the test
226 and comparing it to a previously created output file
227 verbose -- if true, print more messages
Trent Mickf29f47b2000-08-11 19:02:59 +0000228 quiet -- if true, don't print 'skipped' messages (probably redundant)
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000229 testdir -- test directory
230 """
Guido van Rossum152494a1996-12-20 03:12:20 +0000231 test_support.unload(test)
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000232 if not testdir: testdir = findtestdir()
Guido van Rossum152494a1996-12-20 03:12:20 +0000233 outputdir = os.path.join(testdir, "output")
234 outputfile = os.path.join(outputdir, test)
235 try:
Guido van Rossum41360a41998-03-26 19:42:58 +0000236 if generate:
237 cfp = open(outputfile, "w")
238 elif verbose:
239 cfp = sys.stdout
240 else:
241 cfp = Compare(outputfile)
Guido van Rossum152494a1996-12-20 03:12:20 +0000242 except IOError:
Guido van Rossum41360a41998-03-26 19:42:58 +0000243 cfp = None
244 print "Warning: can't open", outputfile
Guido van Rossum152494a1996-12-20 03:12:20 +0000245 try:
Guido van Rossum41360a41998-03-26 19:42:58 +0000246 save_stdout = sys.stdout
247 try:
248 if cfp:
249 sys.stdout = cfp
250 print test # Output file starts with test name
251 __import__(test, globals(), locals(), [])
Jeremy Hyltonfff9e202000-07-11 15:15:31 +0000252 if cfp and not (generate or verbose):
253 cfp.close()
Guido van Rossum41360a41998-03-26 19:42:58 +0000254 finally:
255 sys.stdout = save_stdout
Thomas Wouters3af826e2000-08-04 13:17:51 +0000256 except (ImportError, test_support.TestSkipped), msg:
Trent Mickf29f47b2000-08-11 19:02:59 +0000257 if not quiet:
258 print "test", test,
259 print "skipped -- ", msg
Guido van Rossum41360a41998-03-26 19:42:58 +0000260 return -1
Fred Drakefe5c22a2000-08-18 16:04:05 +0000261 except KeyboardInterrupt:
262 raise
Guido van Rossum152494a1996-12-20 03:12:20 +0000263 except test_support.TestFailed, msg:
Guido van Rossum41360a41998-03-26 19:42:58 +0000264 print "test", test, "failed --", msg
265 return 0
Guido van Rossum9e48b271997-07-16 01:56:13 +0000266 except:
Guido van Rossum41360a41998-03-26 19:42:58 +0000267 type, value = sys.exc_info()[:2]
Fred Drake27c4b392000-08-23 20:34:40 +0000268 print "test", test, "crashed --", str(type) + ":", value
Guido van Rossum41360a41998-03-26 19:42:58 +0000269 if verbose:
270 traceback.print_exc(file=sys.stdout)
271 return 0
Guido van Rossum152494a1996-12-20 03:12:20 +0000272 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000273 return 1
Guido van Rossum152494a1996-12-20 03:12:20 +0000274
275def findtestdir():
276 if __name__ == '__main__':
Guido van Rossum41360a41998-03-26 19:42:58 +0000277 file = sys.argv[0]
Guido van Rossum152494a1996-12-20 03:12:20 +0000278 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000279 file = __file__
Guido van Rossum152494a1996-12-20 03:12:20 +0000280 testdir = os.path.dirname(file) or os.curdir
281 return testdir
282
283def count(n, word):
284 if n == 1:
Guido van Rossum41360a41998-03-26 19:42:58 +0000285 return "%d %s" % (n, word)
Guido van Rossum152494a1996-12-20 03:12:20 +0000286 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000287 return "%d %ss" % (n, word)
Guido van Rossum152494a1996-12-20 03:12:20 +0000288
289class Compare:
290
291 def __init__(self, filename):
Guido van Rossum41360a41998-03-26 19:42:58 +0000292 self.fp = open(filename, 'r')
Tim Peters1a4d77b2000-12-30 22:21:22 +0000293 self.stuffthatmatched = []
Guido van Rossum152494a1996-12-20 03:12:20 +0000294
295 def write(self, data):
Guido van Rossum41360a41998-03-26 19:42:58 +0000296 expected = self.fp.read(len(data))
Tim Peters1a4d77b2000-12-30 22:21:22 +0000297 if data == expected:
298 self.stuffthatmatched.append(expected)
299 else:
300 # This Compare instance is spoofing stdout, so we need to write
301 # to stderr instead.
302 from sys import stderr as e
303 print >> e, "The actual stdout doesn't match the expected stdout."
304 if self.stuffthatmatched:
305 print >> e, "This much did match (between asterisk lines):"
306 print >> e, "*" * 70
307 good = "".join(self.stuffthatmatched)
308 e.write(good)
309 if not good.endswith("\n"):
310 e.write("\n")
311 print >> e, "*" * 70
312 print >> e, "Then ..."
313 else:
314 print >> e, "The first write to stdout clashed:"
315 # Note that the prompts are the same length in next two lines.
316 # This is so what we expected and what we got line up.
317 print >> e, "We expected (repr):", `expected`
318 print >> e, "But instead we got:", `data`
319 raise test_support.TestFailed('Writing: ' + `data`+
320 ', expected: ' + `expected`)
Guido van Rossum152494a1996-12-20 03:12:20 +0000321
Guido van Rossume87ed5f1998-04-23 13:33:21 +0000322 def writelines(self, listoflines):
323 map(self.write, listoflines)
324
Guido van Rossum75fce301997-07-17 14:51:37 +0000325 def flush(self):
Guido van Rossum41360a41998-03-26 19:42:58 +0000326 pass
Guido van Rossum75fce301997-07-17 14:51:37 +0000327
Guido van Rossum152494a1996-12-20 03:12:20 +0000328 def close(self):
Guido van Rossum41360a41998-03-26 19:42:58 +0000329 leftover = self.fp.read()
330 if leftover:
Tim Peters1a4d77b2000-12-30 22:21:22 +0000331 raise test_support.TestFailed('Tail of expected stdout unseen: ' +
332 `leftover`)
Guido van Rossum41360a41998-03-26 19:42:58 +0000333 self.fp.close()
Guido van Rossum152494a1996-12-20 03:12:20 +0000334
335 def isatty(self):
Guido van Rossum41360a41998-03-26 19:42:58 +0000336 return 0
Guido van Rossum152494a1996-12-20 03:12:20 +0000337
338if __name__ == '__main__':
Guido van Rossume8387011997-08-14 19:40:34 +0000339 sys.exit(main())