-g: generate -- write the output file for a test instead of comparing it
-x: exclude -- arguments are tests to *exclude*
-s: single -- run only a single test (see below)
+-r: random -- randomize test execution order
If non-option arguments are present, they are names for tests to run,
unless -x is given, in which case they are names for tests not to run.
import os
import getopt
import traceback
+import random
import test_support
-def main(tests=None, testdir=None):
+def main(tests=None, testdir=None, verbose=0, quiet=0, generate=0,
+ exclude=0, single=0, randomize=0):
"""Execute a test suite.
This also parses command-line options and modifies its behaviour
If the tests argument is omitted, the tests listed on the
command-line will be used. If that's empty, too, then all *.py
files beginning with test_ will be used.
-
+
+ The other six default arguments (verbose, quiet, generate, exclude,
+ single, and randomize) allow programmers calling main() directly to
+ set the values that would normally be set by flags on the command
+ line.
"""
try:
- opts, args = getopt.getopt(sys.argv[1:], 'vgqxs')
+ opts, args = getopt.getopt(sys.argv[1:], 'vgqxsr')
except getopt.error, msg:
print msg
print __doc__
return 2
- verbose = 0
- quiet = 0
- generate = 0
- exclude = 0
- single = 0
for o, a in opts:
if o == '-v': verbose = verbose+1
if o == '-q': quiet = 1; verbose = 0
if o == '-g': generate = 1
if o == '-x': exclude = 1
if o == '-s': single = 1
+ if o == '-r': randomize = 1
if generate and verbose:
print "-g and -v don't go together!"
return 2
tests = tests or args or findtests(testdir, stdtests, nottests)
if single:
tests = tests[:1]
+ if randomize:
+ random.shuffle(tests)
test_support.verbose = verbose # Tell tests to be moderately quiet
save_modules = sys.modules.keys()
for test in tests: