testall.py 8.6 KB

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