author | andy |
Wed, 10 Sep 2008 22:39:57 +0000 | |
changeset 2978 | 1e68f6a61dfe |
parent 2967 | ea62529bd1df |
child 2984 | c63f149d55aa |
permissions | -rw-r--r-- |
2963 | 1 |
#!/usr/bin/env python |
2966 | 2 |
#Copyright ReportLab Europe Ltd. 2000-2008 |
2963 | 3 |
#see license.txt for license details |
4 |
"""Runs all test files in all subfolders. |
|
5 |
""" |
|
2966 | 6 |
__version__=''' $Id$ ''' |
7 |
import os, glob, sys, string, traceback, unittest |
|
2978 | 8 |
|
9 |
#we need to ensure 'tests' is on the path. It will be if you |
|
10 |
#run 'setup.py tests', but won't be if you CD into the tests |
|
11 |
#directory and run this directly |
|
12 |
try: |
|
13 |
from tests.utils import GlobDirectoryWalker, outputfile, printLocation |
|
14 |
except ImportError: |
|
15 |
directoryAboveMe = os.path.dirname(os.getcwd()) |
|
16 |
sys.path.insert(0, directoryAboveMe) |
|
17 |
from tests.utils import GlobDirectoryWalker, outputfile, printLocation |
|
18 |
||
19 |
||
2963 | 20 |
|
21 |
def makeSuite(folder, exclude=[],nonImportable=[],pattern='test_*.py'): |
|
22 |
"Build a test suite of all available test files." |
|
23 |
allTests = unittest.TestSuite() |
|
24 |
||
25 |
if os.path.isdir(folder): sys.path.insert(0, folder) |
|
26 |
for filename in GlobDirectoryWalker(folder, pattern): |
|
27 |
modname = os.path.splitext(os.path.basename(filename))[0] |
|
28 |
if modname not in exclude: |
|
29 |
try: |
|
30 |
exec 'import %s as module' % modname |
|
31 |
allTests.addTest(module.makeSuite()) |
|
32 |
except: |
|
33 |
tt, tv, tb = sys.exc_info()[:] |
|
34 |
nonImportable.append((filename,traceback.format_exception(tt,tv,tb))) |
|
35 |
del tt,tv,tb |
|
36 |
del sys.path[0] |
|
37 |
||
38 |
return allTests |
|
39 |
||
40 |
||
41 |
def main(pattern='test_*.py'): |
|
42 |
try: |
|
43 |
folder = os.path.dirname(__file__) |
|
44 |
assert folder |
|
45 |
except: |
|
46 |
folder = os.path.dirname(sys.argv[0]) or os.getcwd() |
|
47 |
#allow for Benn's "screwball cygwin distro": |
|
2966 | 48 |
if not folder: |
2963 | 49 |
folder = '.' |
50 |
from reportlab.lib.utils import isSourceDistro |
|
51 |
haveSRC = isSourceDistro() |
|
52 |
||
53 |
def cleanup(folder,patterns=('*.pdf', '*.log','*.svg','runAll.txt', 'test_*.txt','_i_am_actually_a_*.*')): |
|
54 |
if not folder: return |
|
55 |
for pat in patterns: |
|
56 |
for filename in GlobDirectoryWalker(folder, pattern=pat): |
|
57 |
try: |
|
58 |
os.remove(filename) |
|
59 |
except: |
|
60 |
pass |
|
61 |
||
2967
ea62529bd1df
reportlab-2.2: first stage changes in on the trunk
rgbecker
parents:
2966
diff
changeset
|
62 |
# special case for tests directory - clean up |
2963 | 63 |
# all PDF & log files before starting run. You don't |
64 |
# want this if reusing runAll anywhere else. |
|
2967
ea62529bd1df
reportlab-2.2: first stage changes in on the trunk
rgbecker
parents:
2966
diff
changeset
|
65 |
if string.find(folder, os.sep+'tests') > -1: cleanup(folder) |
2963 | 66 |
cleanup(outputfile('')) |
67 |
NI = [] |
|
68 |
cleanOnly = '--clean' in sys.argv |
|
69 |
if not cleanOnly: |
|
70 |
testSuite = makeSuite(folder,nonImportable=NI,pattern=pattern+(not haveSRC and 'c' or '')) |
|
71 |
unittest.TextTestRunner().run(testSuite) |
|
72 |
if haveSRC: cleanup(folder,patterns=('*.pyc','*.pyo')) |
|
73 |
if not cleanOnly: |
|
74 |
if NI: |
|
75 |
sys.stderr.write('\n###################### the following tests could not be imported\n') |
|
76 |
for f,tb in NI: |
|
77 |
print 'file: "%s"\n%s\n' % (f,string.join(tb,'')) |
|
78 |
printLocation() |
|
79 |
||
80 |
def mainEx(): |
|
81 |
'''for use in subprocesses''' |
|
82 |
try: |
|
83 |
main() |
|
84 |
finally: |
|
85 |
sys.stdout.flush() |
|
86 |
sys.stderr.flush() |
|
87 |
sys.stdout.close() |
|
88 |
os.close(sys.stderr.fileno()) |
|
89 |
||
90 |
def runExternally(): |
|
2966 | 91 |
cmd = sys.executable+' -c"from tests import runAll;runAll.mainEx()"' |
2963 | 92 |
i,o,e=os.popen3(cmd) |
93 |
i.close() |
|
94 |
out = o.read() |
|
95 |
err=e.read() |
|
96 |
return '\n'.join((out,err)) |
|
97 |
||
98 |
def checkForFailure(outerr): |
|
99 |
return '\nFAILED' in outerr |
|
100 |
||
101 |
if __name__ == '__main__': #noruntests |
|
102 |
main() |