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]
78 tests
= tests
or args
or findtests()
79 test_support
.verbose
= verbose
# Tell tests to be moderately quiet
83 ok
= runtest(test
, generate
, verbose
, testdir
)
91 print "skipped -- an optional feature could not be imported"
93 if good
and not quiet
:
94 if not bad
and not skipped
and len(good
) > 1:
96 print count(len(good
), "test"), "OK."
98 print count(len(bad
), "test"), "failed:",
99 print string
.join(bad
)
100 if skipped
and not quiet
:
101 print count(len(skipped
), "test"), "skipped:",
102 print string
.join(skipped
)
120 def findtests(testdir
=None, stdtests
=STDTESTS
, nottests
=NOTTESTS
):
121 """Return a list of all applicable test modules."""
122 if not testdir
: testdir
= findtestdir()
123 names
= os
.listdir(testdir
)
126 if name
[:5] == "test_" and name
[-3:] == ".py":
128 if modname
not in stdtests
and modname
not in nottests
:
129 tests
.append(modname
)
131 return stdtests
+ tests
133 def runtest(test
, generate
, verbose
, testdir
= None):
134 """Run a single test.
135 test -- the name of the test
136 generate -- if true, generate output, instead of running the test
137 and comparing it to a previously created output file
138 verbose -- if true, print more messages
139 testdir -- test directory
141 test_support
.unload(test
)
142 if not testdir
: testdir
= findtestdir()
143 outputdir
= os
.path
.join(testdir
, "output")
144 outputfile
= os
.path
.join(outputdir
, test
)
147 cfp
= open(outputfile
, "w")
151 cfp
= Compare(outputfile
)
154 print "Warning: can't open", outputfile
156 save_stdout
= sys
.stdout
160 print test
# Output file starts with test name
161 __import__(test
, globals(), locals(), [])
163 sys
.stdout
= save_stdout
164 except ImportError, msg
:
166 except KeyboardInterrupt, v
:
167 raise KeyboardInterrupt, v
, sys
.exc_info()[2]
168 except test_support
.TestFailed
, msg
:
169 print "test", test
, "failed --", msg
172 type, value
= sys
.exc_info()[:2]
173 print "test", test
, "crashed --", type, ":", value
175 traceback
.print_exc(file=sys
.stdout
)
181 if __name__
== '__main__':
185 testdir
= os
.path
.dirname(file) or os
.curdir
190 return "%d %s" % (n
, word
)
192 return "%d %ss" % (n
, word
)
196 def __init__(self
, filename
):
197 self
.fp
= open(filename
, 'r')
199 def write(self
, data
):
200 expected
= self
.fp
.read(len(data
))
202 raise test_support
.TestFailed
, \
203 'Writing: '+`data`
+', expected: '+`expected`
205 def writelines(self
, listoflines
):
206 map(self
.write
, listoflines
)
212 leftover
= self
.fp
.read()
214 raise test_support
.TestFailed
, 'Unread: '+`leftover`
220 if __name__
== '__main__':