runtests 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #!/usr/bin/env python
  2. import os.path, sys, warnings
  3. test_modules = [
  4. 'cryptutil',
  5. 'oidutil',
  6. 'dh',
  7. ]
  8. def fixpath():
  9. try:
  10. d = os.path.dirname(__file__)
  11. except NameError:
  12. d = os.path.dirname(sys.argv[0])
  13. parent = os.path.normpath(os.path.join(d, '..'))
  14. if parent not in sys.path:
  15. print "putting %s in sys.path" % (parent,)
  16. sys.path.insert(0, parent)
  17. def otherTests():
  18. failed = []
  19. for module_name in test_modules:
  20. print 'Testing %s...' % (module_name,) ,
  21. sys.stdout.flush()
  22. module_name = 'openid.test.' + module_name
  23. try:
  24. test_mod = __import__(module_name, {}, {}, [None])
  25. except ImportError:
  26. print 'Failed to import test %r' % (module_name,)
  27. failed.append(module_name)
  28. else:
  29. try:
  30. test_mod.test()
  31. except (SystemExit, KeyboardInterrupt):
  32. raise
  33. except:
  34. sys.excepthook(*sys.exc_info())
  35. failed.append(module_name)
  36. else:
  37. print 'Succeeded.'
  38. return failed
  39. def pyunitTests():
  40. import unittest
  41. pyunit_module_names = [
  42. 'server',
  43. 'consumer',
  44. 'message',
  45. 'symbol',
  46. 'etxrd',
  47. 'xri',
  48. 'xrires',
  49. 'association_response',
  50. 'auth_request',
  51. 'negotiation',
  52. 'verifydisco',
  53. 'sreg',
  54. 'ax',
  55. 'pape',
  56. 'pape_draft2',
  57. 'pape_draft5',
  58. 'rpverify',
  59. 'extension',
  60. ]
  61. pyunit_modules = [
  62. __import__('openid.test.test_%s' % (name,), {}, {}, ['unused'])
  63. for name in pyunit_module_names
  64. ]
  65. try:
  66. from openid.test import test_examples
  67. except ImportError, e:
  68. if 'twill' in str(e):
  69. warnings.warn("Could not import twill; skipping test_examples.")
  70. else:
  71. raise
  72. else:
  73. pyunit_modules.append(test_examples)
  74. # Some modules have data-driven tests, and they use custom methods
  75. # to build the test suite:
  76. custom_module_names = [
  77. 'kvform',
  78. 'linkparse',
  79. 'oidutil',
  80. 'storetest',
  81. 'test_accept',
  82. 'test_association',
  83. 'test_discover',
  84. 'test_fetchers',
  85. 'test_htmldiscover',
  86. 'test_nonce',
  87. 'test_openidyadis',
  88. 'test_parsehtml',
  89. 'test_urinorm',
  90. 'test_yadis_discover',
  91. 'trustroot',
  92. ]
  93. loader = unittest.TestLoader()
  94. s = unittest.TestSuite()
  95. for m in pyunit_modules:
  96. s.addTest(loader.loadTestsFromModule(m))
  97. for name in custom_module_names:
  98. m = __import__('openid.test.%s' % (name,), {}, {}, ['unused'])
  99. try:
  100. s.addTest(m.pyUnitTests())
  101. except AttributeError, ex:
  102. # because the AttributeError doesn't actually say which
  103. # object it was.
  104. print "Error loading tests from %s:" % (name,)
  105. raise
  106. runner = unittest.TextTestRunner() # verbosity=2)
  107. return runner.run(s)
  108. def splitDir(d, count):
  109. # in python2.4 and above, it's easier to spell this as
  110. # d.rsplit(os.sep, count)
  111. for i in xrange(count):
  112. d = os.path.dirname(d)
  113. return d
  114. def _import_djopenid():
  115. """Import djopenid from examples/
  116. It's not in sys.path, and I don't really want to put it in sys.path.
  117. """
  118. import types
  119. thisfile = os.path.abspath(sys.modules[__name__].__file__)
  120. topDir = splitDir(thisfile, 2)
  121. djdir = os.path.join(topDir, 'examples', 'djopenid')
  122. djinit = os.path.join(djdir, '__init__.py')
  123. djopenid = types.ModuleType('djopenid')
  124. execfile(djinit, djopenid.__dict__)
  125. djopenid.__file__ = djinit
  126. # __path__ is the magic that makes child modules of the djopenid package
  127. # importable. New feature in python 2.3, see PEP 302.
  128. djopenid.__path__ = [djdir]
  129. sys.modules['djopenid'] = djopenid
  130. def django_tests():
  131. """Runs tests from examples/djopenid.
  132. @returns: number of failed tests.
  133. """
  134. import os
  135. # Django uses this to find out where its settings are.
  136. os.environ['DJANGO_SETTINGS_MODULE'] = 'djopenid.settings'
  137. _import_djopenid()
  138. try:
  139. import django.test.simple
  140. except ImportError, e:
  141. warnings.warn("django.test.simple not found; "
  142. "django examples not tested.")
  143. return 0
  144. import djopenid.server.models, djopenid.consumer.models
  145. print "Testing Django examples:"
  146. # These tests do get put in to a pyunit test suite, so we could run them
  147. # with the other pyunit tests, but django also establishes a test database
  148. # for them, so we let it do that thing instead.
  149. return django.test.simple.run_tests([djopenid.server.models,
  150. djopenid.consumer.models])
  151. try:
  152. bool
  153. except NameError:
  154. def bool(x):
  155. return not not x
  156. def main():
  157. fixpath()
  158. other_failed = otherTests()
  159. pyunit_result = pyunitTests()
  160. django_failures = django_tests()
  161. if other_failed:
  162. print 'Failures:', ', '.join(other_failed)
  163. failed = (bool(other_failed) or
  164. bool(not pyunit_result.wasSuccessful()) or
  165. (django_failures > 0))
  166. return failed
  167. if __name__ == '__main__':
  168. sys.exit(main() and 1 or 0)