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*
16 If non-option arguments are present, they are names for tests to run,
17 unless -x is given, in which case they are names for tests not to run.
18 If no test names are given, all tests are run.
20 -v is incompatible with -g and does not compare test output files.
31 def main(tests
=None, testdir
=None):
32 """Execute a test suite.
34 This also parses command-line options and modifies its behaviour
37 tests -- a list of strings containing test names (optional)
38 testdir -- the directory in which to look for tests (optional)
40 Users other than the Python test suite will certainly want to
41 specify testdir; if it's omitted, the directory containing the
42 Python test suite is searched for.
44 If the tests argument is omitted, the tests listed on the
45 command-line will be used. If that's empty, too, then all *.py
46 files beginning with test_ will be used.
51 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'vgqx')
52 except getopt
.error
, msg
:
61 if o
== '-v': verbose
= verbose
+1
62 if o
== '-q': quiet
= 1; verbose
= 0
63 if o
== '-g': generate
= 1
64 if o
== '-x': exclude
= 1
65 if generate
and verbose
:
66 print "-g and -v don't go together!"
71 for i
in range(len(args
)):
72 # Strip trailing ".py" from arguments
73 if args
[i
][-3:] == '.py':
74 args
[i
] = args
[i
][:-3]
75 stdtests
= STDTESTS
[:]
76 nottests
= NOTTESTS
[:]
83 tests
= tests
or args
or findtests(testdir
, stdtests
, nottests
)
84 test_support
.verbose
= verbose
# Tell tests to be moderately quiet
88 ok
= runtest(test
, generate
, verbose
, testdir
)
96 print "skipped -- an optional feature could not be imported"
98 if good
and not quiet
:
99 if not bad
and not skipped
and len(good
) > 1:
101 print count(len(good
), "test"), "OK."
103 print count(len(bad
), "test"), "failed:",
104 print string
.join(bad
)
105 if skipped
and not quiet
:
106 print count(len(skipped
), "test"), "skipped:",
107 print string
.join(skipped
)
125 def findtests(testdir
=None, stdtests
=STDTESTS
, nottests
=NOTTESTS
):
126 """Return a list of all applicable test modules."""
127 if not testdir
: testdir
= findtestdir()
128 names
= os
.listdir(testdir
)
131 if name
[:5] == "test_" and name
[-3:] == ".py":
133 if modname
not in stdtests
and modname
not in nottests
:
134 tests
.append(modname
)
136 return stdtests
+ tests
138 def runtest(test
, generate
, verbose
, testdir
= None):
139 """Run a single test.
140 test -- the name of the test
141 generate -- if true, generate output, instead of running the test
142 and comparing it to a previously created output file
143 verbose -- if true, print more messages
144 testdir -- test directory
146 test_support
.unload(test
)
147 if not testdir
: testdir
= findtestdir()
148 outputdir
= os
.path
.join(testdir
, "output")
149 outputfile
= os
.path
.join(outputdir
, test
)
152 cfp
= open(outputfile
, "w")
156 cfp
= Compare(outputfile
)
159 print "Warning: can't open", outputfile
161 save_stdout
= sys
.stdout
165 print test
# Output file starts with test name
166 __import__(test
, globals(), locals(), [])
168 sys
.stdout
= save_stdout
169 except ImportError, msg
:
171 except KeyboardInterrupt, v
:
172 raise KeyboardInterrupt, v
, sys
.exc_info()[2]
173 except test_support
.TestFailed
, msg
:
174 print "test", test
, "failed --", msg
177 type, value
= sys
.exc_info()[:2]
178 print "test", test
, "crashed --", type, ":", value
180 traceback
.print_exc(file=sys
.stdout
)
186 if __name__
== '__main__':
190 testdir
= os
.path
.dirname(file) or os
.curdir
195 return "%d %s" % (n
, word
)
197 return "%d %ss" % (n
, word
)
201 def __init__(self
, filename
):
202 self
.fp
= open(filename
, 'r')
204 def write(self
, data
):
205 expected
= self
.fp
.read(len(data
))
207 raise test_support
.TestFailed
, \
208 'Writing: '+`data`
+', expected: '+`expected`
210 def writelines(self
, listoflines
):
211 map(self
.write
, listoflines
)
217 leftover
= self
.fp
.read()
219 raise test_support
.TestFailed
, 'Unread: '+`leftover`
225 if __name__
== '__main__':