3 # Copyright (c) 2004 Trent Mick
6 relic.py Regression Test Suite Harness
9 python test.py [<options>...] [<tests>...]
12 -x <testname>, --exclude=<testname>
13 Exclude the named test from the set of tests to be
14 run. This can be used multiple times to specify
16 -v, --verbose run tests in verbose mode with output to stdout
17 -q, --quiet don't print anything except if a test fails
18 -h, --help print this text and exit
20 This will find all modules whose name is "test_*" in the test
21 directory, and run them. Various command line options provide
22 additional facilities.
24 If non-option arguments are present, they are names for tests to run.
25 If no test names are given, all tests are run.
28 -c, --clean Don't setup, just clean up the test workspace.
29 -n, --no-clean Don't clean up after setting up and running the
46 class TestError(Exception):
55 #---- utility routines
57 def _rmtreeOnError(rmFunction
, filePath
, excInfo
):
58 if excInfo
[0] == OSError:
59 # presuming because file is read-only
60 os
.chmod(filePath
, 0777)
65 shutil
.rmtree(dirname
, 0, _rmtreeOnError
)
68 def _getAllTests(testDir
):
69 """Return a list of all tests to run."""
70 testPyFiles
= glob
.glob(os
.path
.join(testDir
, "test_*.py"))
71 modules
= [f
[:-3] for f
in testPyFiles
if f
and f
.endswith(".py")]
74 for f
in glob
.glob(os
.path
.join(testDir
, "test_*")):
75 if os
.path
.isdir(f
) and "." not in f
:
76 if os
.path
.isfile(os
.path
.join(testDir
, f
, "__init__.py")):
79 return modules
+ packages
83 # Ensure the *development* check is tested.
84 topDir
= os
.path
.abspath(os
.pardir
)
85 sys
.path
.insert(0, topDir
)
86 print "Setup to test:"
88 ver
= "%s.%s.%s" % relic
._version
_
89 print "relic %s at '%s'" % (ver
, relic
.__file
__)
97 def test(testModules
, testDir
=os
.curdir
, exclude
=[]):
98 """Run the given regression tests and report the results."""
99 # Determine the test modules to run.
101 testModules
= _getAllTests(testDir
)
102 testModules
= [t
for t
in testModules
if t
not in exclude
]
104 # Aggregate the TestSuite's from each module into one big one.
106 for moduleFile
in testModules
:
107 module
= __import__(moduleFile
, globals(), locals(), [])
108 suite
= getattr(module
, "suite", None)
109 if suite
is not None:
110 allSuites
.append(suite())
113 print "WARNING: module '%s' did not have a suite() method."\
115 suite
= unittest
.TestSuite(allSuites
)
118 runner
= unittest
.TextTestRunner(sys
.stdout
, verbosity
=gVerbosity
)
119 result
= runner
.run(suite
)
125 testDir
= os
.path
.dirname(sys
.argv
[0])
130 opts
, testModules
= getopt
.getopt(sys
.argv
[1:], 'hvqx:cn',
131 ['help', 'verbose', 'quiet', 'exclude=', 'clean',
133 except getopt
.error
, ex
:
134 print "%s: ERROR: %s" % (argv
[0], ex
)
141 for opt
, optarg
in opts
:
142 if opt
in ("-h", "--help"):
145 elif opt
in ("-v", "--verbose"):
147 elif opt
in ("-q", "--quiet"):
149 elif opt
in ("-x", "--exclude"):
150 exclude
.append(optarg
)
151 elif opt
in ("-c", "--clean"):
153 elif opt
in ("-n", "--no-clean"):
161 retval
= test(testModules
, testDir
=testDir
, exclude
=exclude
)
164 _tearDown(**setupOpts
)
167 if __name__
== '__main__':
168 sys
.exit( main(sys
.argv
) )