5 This will find all modules whose name is "test_*" in the test
6 directory, and run them. Various command line options provide
11 -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)
17 If non-option arguments are present, they are names for tests to run,
18 unless -x is given, in which case they are names for tests not to run.
19 If no test names are given, all tests are run.
21 -v is incompatible with -g and does not compare test output files.
23 -s means to run only a single test and exit. This is useful when Purifying
24 the Python interpreter. The file /tmp/pynexttest is read to find the next
25 test to run. If this file is missing, the first test_*.py file in testdir or
26 on the command line is used. (actually tempfile.gettempdir() is used instead
39 def main(tests
=None, testdir
=None):
40 """Execute a test suite.
42 This also parses command-line options and modifies its behaviour
45 tests -- a list of strings containing test names (optional)
46 testdir -- the directory in which to look for tests (optional)
48 Users other than the Python test suite will certainly want to
49 specify testdir; if it's omitted, the directory containing the
50 Python test suite is searched for.
52 If the tests argument is omitted, the tests listed on the
53 command-line will be used. If that's empty, too, then all *.py
54 files beginning with test_ will be used.
59 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'vgqxs')
60 except getopt
.error
, msg
:
70 if o
== '-v': verbose
= verbose
+1
71 if o
== '-q': quiet
= 1; verbose
= 0
72 if o
== '-g': generate
= 1
73 if o
== '-x': exclude
= 1
74 if o
== '-s': single
= 1
75 if generate
and verbose
:
76 print "-g and -v don't go together!"
83 from tempfile
import gettempdir
84 filename
= os
.path
.join(gettempdir(), 'pynexttest')
86 fp
= open(filename
, 'r')
87 next
= string
.strip(fp
.read())
92 for i
in range(len(args
)):
93 # Strip trailing ".py" from arguments
94 if args
[i
][-3:] == '.py':
95 args
[i
] = args
[i
][:-3]
96 stdtests
= STDTESTS
[:]
97 nottests
= NOTTESTS
[:]
104 tests
= tests
or args
or findtests(testdir
, stdtests
, nottests
)
107 test_support
.verbose
= verbose
# Tell tests to be moderately quiet
111 ok
= runtest(test
, generate
, verbose
, testdir
)
119 print "skipped -- an optional feature could not be imported"
121 if good
and not quiet
:
122 if not bad
and not skipped
and len(good
) > 1:
124 print count(len(good
), "test"), "OK."
126 print count(len(bad
), "test"), "failed:",
127 print string
.join(bad
)
128 if skipped
and not quiet
:
129 print count(len(skipped
), "test"), "skipped:",
130 print string
.join(skipped
)
133 alltests
= findtests(testdir
, stdtests
, nottests
)
134 for i
in range(len(alltests
)):
135 if tests
[0] == alltests
[i
]:
136 if i
== len(alltests
) - 1:
139 fp
= open(filename
, 'w')
140 fp
.write(alltests
[i
+1] + '\n')
163 def findtests(testdir
=None, stdtests
=STDTESTS
, nottests
=NOTTESTS
):
164 """Return a list of all applicable test modules."""
165 if not testdir
: testdir
= findtestdir()
166 names
= os
.listdir(testdir
)
169 if name
[:5] == "test_" and name
[-3:] == ".py":
171 if modname
not in stdtests
and modname
not in nottests
:
172 tests
.append(modname
)
174 return stdtests
+ tests
176 def runtest(test
, generate
, verbose
, testdir
= None):
177 """Run a single test.
178 test -- the name of the test
179 generate -- if true, generate output, instead of running the test
180 and comparing it to a previously created output file
181 verbose -- if true, print more messages
182 testdir -- test directory
184 test_support
.unload(test
)
185 if not testdir
: testdir
= findtestdir()
186 outputdir
= os
.path
.join(testdir
, "output")
187 outputfile
= os
.path
.join(outputdir
, test
)
190 cfp
= open(outputfile
, "w")
194 cfp
= Compare(outputfile
)
197 print "Warning: can't open", outputfile
199 save_stdout
= sys
.stdout
203 print test
# Output file starts with test name
204 __import__(test
, globals(), locals(), [])
206 sys
.stdout
= save_stdout
207 except ImportError, msg
:
209 except KeyboardInterrupt, v
:
210 raise KeyboardInterrupt, v
, sys
.exc_info()[2]
211 except test_support
.TestFailed
, msg
:
212 print "test", test
, "failed --", msg
215 type, value
= sys
.exc_info()[:2]
216 print "test", test
, "crashed --", type, ":", value
218 traceback
.print_exc(file=sys
.stdout
)
224 if __name__
== '__main__':
228 testdir
= os
.path
.dirname(file) or os
.curdir
233 return "%d %s" % (n
, word
)
235 return "%d %ss" % (n
, word
)
239 def __init__(self
, filename
):
240 self
.fp
= open(filename
, 'r')
242 def write(self
, data
):
243 expected
= self
.fp
.read(len(data
))
245 raise test_support
.TestFailed
, \
246 'Writing: '+`data`
+', expected: '+`expected`
248 def writelines(self
, listoflines
):
249 map(self
.write
, listoflines
)
255 leftover
= self
.fp
.read()
257 raise test_support
.TestFailed
, 'Unread: '+`leftover`
263 if __name__
== '__main__':