testall.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. from . 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. testConversionErrors
  81. """.split(),
  82. # Level 2 tests.
  83. """testMSOffice.TestAll testMSOfficeEvents.test testAccess.test
  84. testExplorer.TestAll testExchange.test
  85. """.split(),
  86. # Level 3 tests.
  87. """testmakepy.TestAll
  88. """.split()
  89. ]
  90. # A list of other unittest modules we use - these are fully qualified module
  91. # names and the module is assumed to be unittest based.
  92. unittest_other_modules = [
  93. # Level 1 tests.
  94. """win32com.directsound.test.ds_test
  95. """.split(),
  96. # Level 2 tests.
  97. [],
  98. # Level 3 tests.
  99. []
  100. ]
  101. output_checked_programs = [
  102. # Level 1 tests.
  103. [
  104. ("cscript.exe /nologo //E:vbscript testInterp.vbs", "VBScript test worked OK"),
  105. ("cscript.exe /nologo //E:vbscript testDictionary.vbs",
  106. "VBScript has successfully tested Python.Dictionary"),
  107. ],
  108. # Level 2 tests.
  109. [
  110. ],
  111. # Level 3 tests
  112. [
  113. ],
  114. ]
  115. custom_test_cases = [
  116. # Level 1 tests.
  117. [
  118. PyCOMTest,
  119. PippoTest,
  120. ],
  121. # Level 2 tests.
  122. [
  123. ],
  124. # Level 3 tests
  125. [
  126. ],
  127. ]
  128. def get_test_mod_and_func(test_name, import_failures):
  129. if test_name.find(".")>0:
  130. mod_name, func_name = test_name.split(".")
  131. else:
  132. mod_name = test_name
  133. func_name = None
  134. fq_mod_name = "win32com.test." + mod_name
  135. try:
  136. __import__(fq_mod_name)
  137. mod = sys.modules[fq_mod_name]
  138. except:
  139. import_failures.append((mod_name, sys.exc_info()[:2]))
  140. return None, None
  141. if func_name is None:
  142. func = None
  143. else:
  144. func = getattr(mod, func_name)
  145. return mod, func
  146. # Return a test suite all loaded with the tests we want to run
  147. def make_test_suite(test_level = 1):
  148. suite = unittest.TestSuite()
  149. import_failures = []
  150. loader = TestLoader()
  151. for i in range(testLevel):
  152. for mod_name in unittest_modules[i]:
  153. mod, func = get_test_mod_and_func(mod_name, import_failures)
  154. if mod is None:
  155. continue
  156. if func is not None:
  157. test = CapturingFunctionTestCase(func, description=mod_name)
  158. else:
  159. if hasattr(mod, "suite"):
  160. test = mod.suite()
  161. else:
  162. test = loader.loadTestsFromModule(mod)
  163. assert test.countTestCases() > 0, "No tests loaded from %r" % mod
  164. suite.addTest(test)
  165. for cmd, output in output_checked_programs[i]:
  166. suite.addTest(ShellTestCase(cmd, output))
  167. for test_class in custom_test_cases[i]:
  168. suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(test_class))
  169. # other "normal" unittest modules.
  170. for i in range(testLevel):
  171. for mod_name in unittest_other_modules[i]:
  172. try:
  173. __import__(mod_name)
  174. except:
  175. import_failures.append((mod_name, sys.exc_info()[:2]))
  176. continue
  177. mod = sys.modules[mod_name]
  178. if hasattr(mod, "suite"):
  179. test = mod.suite()
  180. else:
  181. test = loader.loadTestsFromModule(mod)
  182. assert test.countTestCases() > 0, "No tests loaded from %r" % mod
  183. suite.addTest(test)
  184. return suite, import_failures
  185. def usage(why):
  186. print(why)
  187. print()
  188. print("win32com test suite")
  189. print("usage: testall [-v] test_level")
  190. print(" where test_level is an integer 1-3. Level 1 tests are quick,")
  191. print(" level 2 tests invoke Word, IE etc, level 3 take ages!")
  192. sys.exit(1)
  193. if __name__=='__main__':
  194. try:
  195. opts, args = getopt.getopt(sys.argv[1:], "v")
  196. except getopt.error as why:
  197. usage(why)
  198. for opt, val in opts:
  199. if opt=='-v':
  200. verbosity += 1
  201. testLevel = 1 # default to quick test
  202. test_names = []
  203. for arg in args:
  204. try:
  205. testLevel = int(arg)
  206. if testLevel < 0 or testLevel > 3:
  207. raise ValueError("Only levels 1-3 are supported")
  208. except ValueError:
  209. test_names.append(arg)
  210. if test_names:
  211. usage("Test names are not supported yet")
  212. CleanGenerated()
  213. suite, import_failures = make_test_suite(testLevel)
  214. if verbosity:
  215. if hasattr(sys, "gettotalrefcount"):
  216. print("This is a debug build - memory leak tests will also be run.")
  217. print("These tests may take *many* minutes to run - be patient!")
  218. print("(running from python.exe will avoid these leak tests)")
  219. print("Executing level %d tests - %d test cases will be run" \
  220. % (testLevel, suite.countTestCases()))
  221. if verbosity==1 and suite.countTestCases() < 70:
  222. # A little row of markers so the dots show how close to finished
  223. print('|' * suite.countTestCases())
  224. testRunner = TestRunner(verbosity=verbosity)
  225. testResult = testRunner.run(suite)
  226. if import_failures:
  227. testResult.stream.writeln("*** The following test modules could not be imported ***")
  228. for mod_name, (exc_type, exc_val) in import_failures:
  229. desc = '\n'.join(traceback.format_exception_only(exc_type, exc_val))
  230. testResult.stream.write("%s: %s" % (mod_name, desc))
  231. testResult.stream.writeln("*** %d test(s) could not be run ***" % len(import_failures))
  232. # re-print unit-test error here so it is noticed
  233. if not testResult.wasSuccessful():
  234. print("*" * 20, "- unittest tests FAILED")
  235. CheckClean()
  236. pythoncom.CoUninitialize()
  237. CleanGenerated()