testall.py
8.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import sys, os, string, re
import pythoncom
import win32com.client
from win32com.test.util import CheckClean, TestCase, \
CapturingFunctionTestCase, ShellTestCase, \
TestLoader, TestRunner, RegisterPythonServer
import traceback
import getopt
import unittest
verbosity = 1 # default unittest verbosity.
try:
this_file = __file__
except NameError:
this_file = sys.argv[0]
def GenerateAndRunOldStyle():
import GenTestScripts
GenTestScripts.GenerateAll()
try:
pass #
finally:
GenTestScripts.CleanAll()
def CleanGenerated():
import win32com, shutil
if os.path.isdir(win32com.__gen_path__):
if verbosity > 1:
print "Deleting files from %s" % (win32com.__gen_path__)
shutil.rmtree(win32com.__gen_path__)
import win32com.client.gencache
win32com.client.gencache.__init__() # Reset
def RemoveRefCountOutput(data):
while 1:
last_line_pos = data.rfind("\n")
if not re.match("\[\d+ refs\]", data[last_line_pos+1:]):
break
if last_line_pos < 0:
# All the output
return ''
data = data[:last_line_pos]
return data
def ExecuteSilentlyIfOK(cmd, testcase):
f = os.popen(cmd)
data = f.read().strip()
rc = f.close()
if rc:
print data
testcase.fail("Executing '%s' failed (%d)" % (cmd, rc))
# for "_d" builds, strip the '[xxx refs]' line
return RemoveRefCountOutput(data)
class PyCOMTest(TestCase):
no_leak_tests = True # done by the test itself
def testit(self):
# Check that the item is registered, so we get the correct
# 'skipped' behaviour (and recorded as such) rather than either
# error or silence due to non-registration.
RegisterPythonServer(os.path.join(os.path.dirname(__file__), '..', "servers", "test_pycomtest.py"),
"Python.Test.PyCOMTest")
# Execute testPyComTest in its own process so it can play
# with the Python thread state
fname = os.path.join(os.path.dirname(this_file), "testPyComTest.py")
cmd = '%s "%s" -q 2>&1' % (sys.executable, fname)
data = ExecuteSilentlyIfOK(cmd, self)
class PippoTest(TestCase):
def testit(self):
# Check we are registered before spawning the process.
from win32com.test import pippo_server
RegisterPythonServer(pippo_server.__file__, "Python.Test.Pippo")
python = sys.executable
fname = os.path.join(os.path.dirname(this_file), "testPippo.py")
cmd = '%s "%s" 2>&1' % (python, fname)
ExecuteSilentlyIfOK(cmd, self)
# This is a list of "win32com.test.???" module names, optionally with a
# function in that module if the module isn't unitest based...
unittest_modules = [
# Level 1 tests.
"""testIterators testvbscript_regexp testStorage
testStreams testWMI policySemantics testShell testROT
testAXScript testxslt testDictionary testCollections
testServers errorSemantics.test testvb testArrays
testClipboard testMarshal
""".split(),
# Level 2 tests.
"""testMSOffice.TestAll testMSOfficeEvents.test testAccess.test
testExplorer.TestAll testExchange.test
""".split(),
# Level 3 tests.
"""testmakepy.TestAll
""".split()
]
# A list of other unittest modules we use - these are fully qualified module
# names and the module is assumed to be unittest based.
unittest_other_modules = [
# Level 1 tests.
"""win32com.directsound.test.ds_test
""".split(),
# Level 2 tests.
[],
# Level 3 tests.
[]
]
output_checked_programs = [
# Level 1 tests.
[
("cscript.exe /nologo //E:vbscript testInterp.vbs", "VBScript test worked OK"),
("cscript.exe /nologo //E:vbscript testDictionary.vbs",
"VBScript has successfully tested Python.Dictionary"),
],
# Level 2 tests.
[
],
# Level 3 tests
[
],
]
custom_test_cases = [
# Level 1 tests.
[
PyCOMTest,
PippoTest,
],
# Level 2 tests.
[
],
# Level 3 tests
[
],
]
def get_test_mod_and_func(test_name, import_failures):
if test_name.find(".")>0:
mod_name, func_name = test_name.split(".")
else:
mod_name = test_name
func_name = None
fq_mod_name = "win32com.test." + mod_name
try:
__import__(fq_mod_name)
mod = sys.modules[fq_mod_name]
except:
import_failures.append((mod_name, sys.exc_info()[:2]))
return None, None
if func_name is None:
func = None
else:
func = getattr(mod, func_name)
return mod, func
# Return a test suite all loaded with the tests we want to run
def make_test_suite(test_level = 1):
suite = unittest.TestSuite()
import_failures = []
loader = TestLoader()
for i in range(testLevel):
for mod_name in unittest_modules[i]:
mod, func = get_test_mod_and_func(mod_name, import_failures)
if mod is None:
continue
if func is not None:
test = CapturingFunctionTestCase(func, description=mod_name)
else:
if hasattr(mod, "suite"):
test = mod.suite()
else:
test = loader.loadTestsFromModule(mod)
assert test.countTestCases() > 0, "No tests loaded from %r" % mod
suite.addTest(test)
for cmd, output in output_checked_programs[i]:
suite.addTest(ShellTestCase(cmd, output))
for test_class in custom_test_cases[i]:
suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(test_class))
# other "normal" unittest modules.
for i in range(testLevel):
for mod_name in unittest_other_modules[i]:
try:
__import__(mod_name)
except:
import_failures.append((mod_name, sys.exc_info()[:2]))
continue
mod = sys.modules[mod_name]
if hasattr(mod, "suite"):
test = mod.suite()
else:
test = loader.loadTestsFromModule(mod)
assert test.countTestCases() > 0, "No tests loaded from %r" % mod
suite.addTest(test)
return suite, import_failures
def usage(why):
print why
print
print "win32com test suite"
print "usage: testall [-v] test_level"
print " where test_level is an integer 1-3. Level 1 tests are quick,"
print " level 2 tests invoke Word, IE etc, level 3 take ages!"
sys.exit(1)
if __name__=='__main__':
try:
opts, args = getopt.getopt(sys.argv[1:], "v")
except getopt.error, why:
usage(why)
for opt, val in opts:
if opt=='-v':
verbosity += 1
testLevel = 1 # default to quick test
test_names = []
for arg in args:
try:
testLevel = int(arg)
if testLevel < 0 or testLevel > 3:
raise ValueError("Only levels 1-3 are supported")
except ValueError:
test_names.append(arg)
if test_names:
usage("Test names are not supported yet")
CleanGenerated()
suite, import_failures = make_test_suite(testLevel)
if verbosity:
if hasattr(sys, "gettotalrefcount"):
print "This is a debug build - memory leak tests will also be run."
print "These tests may take *many* minutes to run - be patient!"
print "(running from python.exe will avoid these leak tests)"
print "Executing level %d tests - %d test cases will be run" \
% (testLevel, suite.countTestCases())
if verbosity==1 and suite.countTestCases() < 70:
# A little row of markers so the dots show how close to finished
print '|' * suite.countTestCases()
testRunner = TestRunner(verbosity=verbosity)
testResult = testRunner.run(suite)
if import_failures:
testResult.stream.writeln("*** The following test modules could not be imported ***")
for mod_name, (exc_type, exc_val) in import_failures:
desc = '\n'.join(traceback.format_exception_only(exc_type, exc_val))
testResult.stream.write("%s: %s" % (mod_name, desc))
testResult.stream.writeln("*** %d test(s) could not be run ***" % len(import_failures))
# re-print unit-test error here so it is noticed
if not testResult.wasSuccessful():
print "*" * 20, "- unittest tests FAILED"
CheckClean()
pythoncom.CoUninitialize()
CleanGenerated()