PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/runtests.py

https://code.google.com/p/mango-py/
Python | 282 lines | 271 code | 9 blank | 2 comment | 9 complexity | 83c1f5a3bb68dd3a22b9ee5249ef0ce5 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #!/usr/bin/env python
  2. import os, subprocess, sys
  3. import django.contrib as contrib
  4. CONTRIB_DIR_NAME = 'django.contrib'
  5. MODEL_TESTS_DIR_NAME = 'modeltests'
  6. REGRESSION_TESTS_DIR_NAME = 'regressiontests'
  7. TEST_TEMPLATE_DIR = 'templates'
  8. CONTRIB_DIR = os.path.dirname(contrib.__file__)
  9. MODEL_TEST_DIR = os.path.join(os.path.dirname(__file__), MODEL_TESTS_DIR_NAME)
  10. REGRESSION_TEST_DIR = os.path.join(os.path.dirname(__file__), REGRESSION_TESTS_DIR_NAME)
  11. REGRESSION_SUBDIRS_TO_SKIP = ['locale']
  12. ALWAYS_INSTALLED_APPS = [
  13. 'django.contrib.contenttypes',
  14. 'django.contrib.auth',
  15. 'django.contrib.sites',
  16. 'django.contrib.flatpages',
  17. 'django.contrib.redirects',
  18. 'django.contrib.sessions',
  19. 'django.contrib.messages',
  20. 'django.contrib.comments',
  21. 'django.contrib.admin',
  22. 'django.contrib.admindocs',
  23. 'django.contrib.staticfiles',
  24. ]
  25. def geodjango(settings):
  26. # All databases must have spatial backends to run GeoDjango tests.
  27. spatial_dbs = [name for name, db_dict in settings.DATABASES.items()
  28. if db_dict['ENGINE'].startswith('django.contrib.gis')]
  29. return len(spatial_dbs) == len(settings.DATABASES)
  30. def get_test_modules():
  31. modules = []
  32. for loc, dirpath in (MODEL_TESTS_DIR_NAME, MODEL_TEST_DIR), (REGRESSION_TESTS_DIR_NAME, REGRESSION_TEST_DIR), (CONTRIB_DIR_NAME, CONTRIB_DIR):
  33. for f in os.listdir(dirpath):
  34. if (f.startswith('__init__') or
  35. f.startswith('.') or
  36. f.startswith('sql') or
  37. os.path.basename(f) in REGRESSION_SUBDIRS_TO_SKIP):
  38. continue
  39. modules.append((loc, f))
  40. return modules
  41. def setup(verbosity, test_labels):
  42. from django.conf import settings
  43. state = {
  44. 'INSTALLED_APPS': settings.INSTALLED_APPS,
  45. 'ROOT_URLCONF': getattr(settings, "ROOT_URLCONF", ""),
  46. 'TEMPLATE_DIRS': settings.TEMPLATE_DIRS,
  47. 'USE_I18N': settings.USE_I18N,
  48. 'LOGIN_URL': settings.LOGIN_URL,
  49. 'LANGUAGE_CODE': settings.LANGUAGE_CODE,
  50. 'MIDDLEWARE_CLASSES': settings.MIDDLEWARE_CLASSES,
  51. }
  52. # Redirect some settings for the duration of these tests.
  53. settings.INSTALLED_APPS = ALWAYS_INSTALLED_APPS
  54. settings.ROOT_URLCONF = 'urls'
  55. settings.TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__), TEST_TEMPLATE_DIR),)
  56. settings.USE_I18N = True
  57. settings.LANGUAGE_CODE = 'en'
  58. settings.LOGIN_URL = '/accounts/login/'
  59. settings.MIDDLEWARE_CLASSES = (
  60. 'django.contrib.sessions.middleware.SessionMiddleware',
  61. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  62. 'django.contrib.messages.middleware.MessageMiddleware',
  63. 'django.middleware.common.CommonMiddleware',
  64. )
  65. settings.SITE_ID = 1
  66. # For testing comment-utils, we require the MANAGERS attribute
  67. # to be set, so that a test email is sent out which we catch
  68. # in our tests.
  69. settings.MANAGERS = ("admin@djangoproject.com",)
  70. # Load all the ALWAYS_INSTALLED_APPS.
  71. # (This import statement is intentionally delayed until after we
  72. # access settings because of the USE_I18N dependency.)
  73. from django.db.models.loading import get_apps, load_app
  74. get_apps()
  75. # Load all the test model apps.
  76. test_labels_set = set([label.split('.')[0] for label in test_labels])
  77. test_modules = get_test_modules()
  78. # If GeoDjango, then we'll want to add in the test applications
  79. # that are a part of its test suite.
  80. if geodjango(settings):
  81. from django.contrib.gis.tests import geo_apps
  82. test_modules.extend(geo_apps(runtests=True))
  83. for module_dir, module_name in test_modules:
  84. module_label = '.'.join([module_dir, module_name])
  85. # if the module was named on the command line, or
  86. # no modules were named (i.e., run all), import
  87. # this module and add it to the list to test.
  88. if not test_labels or module_name in test_labels_set:
  89. if verbosity >= 2:
  90. print "Importing application %s" % module_name
  91. mod = load_app(module_label)
  92. if mod:
  93. if module_label not in settings.INSTALLED_APPS:
  94. settings.INSTALLED_APPS.append(module_label)
  95. return state
  96. def teardown(state):
  97. from django.conf import settings
  98. # Restore the old settings.
  99. for key, value in state.items():
  100. setattr(settings, key, value)
  101. def django_tests(verbosity, interactive, failfast, test_labels):
  102. from django.conf import settings
  103. state = setup(verbosity, test_labels)
  104. extra_tests = []
  105. # If GeoDjango is used, add it's tests that aren't a part of
  106. # an application (e.g., GEOS, GDAL, Distance objects).
  107. if geodjango(settings):
  108. from django.contrib.gis.tests import geodjango_suite
  109. extra_tests.append(geodjango_suite(apps=False))
  110. # Run the test suite, including the extra validation tests.
  111. from django.test.utils import get_runner
  112. if not hasattr(settings, 'TEST_RUNNER'):
  113. settings.TEST_RUNNER = 'django.test.simple.DjangoTestSuiteRunner'
  114. TestRunner = get_runner(settings)
  115. if hasattr(TestRunner, 'func_name'):
  116. # Pre 1.2 test runners were just functions,
  117. # and did not support the 'failfast' option.
  118. import warnings
  119. warnings.warn(
  120. 'Function-based test runners are deprecated. Test runners should be classes with a run_tests() method.',
  121. DeprecationWarning
  122. )
  123. failures = TestRunner(test_labels, verbosity=verbosity, interactive=interactive,
  124. extra_tests=extra_tests)
  125. else:
  126. test_runner = TestRunner(verbosity=verbosity, interactive=interactive, failfast=failfast)
  127. failures = test_runner.run_tests(test_labels, extra_tests=extra_tests)
  128. teardown(state)
  129. return failures
  130. def bisect_tests(bisection_label, options, test_labels):
  131. state = setup(int(options.verbosity), test_labels)
  132. if not test_labels:
  133. # Get the full list of test labels to use for bisection
  134. from django.db.models.loading import get_apps
  135. test_labels = [app.__name__.split('.')[-2] for app in get_apps()]
  136. print '***** Bisecting test suite:',' '.join(test_labels)
  137. # Make sure the bisection point isn't in the test list
  138. # Also remove tests that need to be run in specific combinations
  139. for label in [bisection_label, 'model_inheritance_same_model_name']:
  140. try:
  141. test_labels.remove(label)
  142. except ValueError:
  143. pass
  144. subprocess_args = [sys.executable, __file__, '--settings=%s' % options.settings]
  145. if options.failfast:
  146. subprocess_args.append('--failfast')
  147. if options.verbosity:
  148. subprocess_args.append('--verbosity=%s' % options.verbosity)
  149. if not options.interactive:
  150. subprocess_args.append('--noinput')
  151. iteration = 1
  152. while len(test_labels) > 1:
  153. midpoint = len(test_labels)/2
  154. test_labels_a = test_labels[:midpoint] + [bisection_label]
  155. test_labels_b = test_labels[midpoint:] + [bisection_label]
  156. print '***** Pass %da: Running the first half of the test suite' % iteration
  157. print '***** Test labels:',' '.join(test_labels_a)
  158. failures_a = subprocess.call(subprocess_args + test_labels_a)
  159. print '***** Pass %db: Running the second half of the test suite' % iteration
  160. print '***** Test labels:',' '.join(test_labels_b)
  161. print
  162. failures_b = subprocess.call(subprocess_args + test_labels_b)
  163. if failures_a and not failures_b:
  164. print "***** Problem found in first half. Bisecting again..."
  165. iteration = iteration + 1
  166. test_labels = test_labels_a[:-1]
  167. elif failures_b and not failures_a:
  168. print "***** Problem found in second half. Bisecting again..."
  169. iteration = iteration + 1
  170. test_labels = test_labels_b[:-1]
  171. elif failures_a and failures_b:
  172. print "***** Multiple sources of failure found"
  173. break
  174. else:
  175. print "***** No source of failure found... try pair execution (--pair)"
  176. break
  177. if len(test_labels) == 1:
  178. print "***** Source of error:",test_labels[0]
  179. teardown(state)
  180. def paired_tests(paired_test, options, test_labels):
  181. state = setup(int(options.verbosity), test_labels)
  182. if not test_labels:
  183. print ""
  184. # Get the full list of test labels to use for bisection
  185. from django.db.models.loading import get_apps
  186. test_labels = [app.__name__.split('.')[-2] for app in get_apps()]
  187. print '***** Trying paired execution'
  188. # Make sure the constant member of the pair isn't in the test list
  189. # Also remove tests that need to be run in specific combinations
  190. for label in [paired_test, 'model_inheritance_same_model_name']:
  191. try:
  192. test_labels.remove(label)
  193. except ValueError:
  194. pass
  195. subprocess_args = [sys.executable, __file__, '--settings=%s' % options.settings]
  196. if options.failfast:
  197. subprocess_args.append('--failfast')
  198. if options.verbosity:
  199. subprocess_args.append('--verbosity=%s' % options.verbosity)
  200. if not options.interactive:
  201. subprocess_args.append('--noinput')
  202. for i, label in enumerate(test_labels):
  203. print '***** %d of %d: Check test pairing with %s' % (i+1, len(test_labels), label)
  204. failures = subprocess.call(subprocess_args + [label, paired_test])
  205. if failures:
  206. print '***** Found problem pair with',label
  207. return
  208. print '***** No problem pair found'
  209. teardown(state)
  210. if __name__ == "__main__":
  211. from optparse import OptionParser
  212. usage = "%prog [options] [module module module ...]"
  213. parser = OptionParser(usage=usage)
  214. parser.add_option('-v','--verbosity', action='store', dest='verbosity', default='1',
  215. type='choice', choices=['0', '1', '2', '3'],
  216. help='Verbosity level; 0=minimal output, 1=normal output, 2=all output')
  217. parser.add_option('--noinput', action='store_false', dest='interactive', default=True,
  218. help='Tells Django to NOT prompt the user for input of any kind.')
  219. parser.add_option('--failfast', action='store_true', dest='failfast', default=False,
  220. help='Tells Django to stop running the test suite after first failed test.')
  221. parser.add_option('--settings',
  222. help='Python path to settings module, e.g. "myproject.settings". If this isn\'t provided, the DJANGO_SETTINGS_MODULE environment variable will be used.')
  223. parser.add_option('--bisect', action='store', dest='bisect', default=None,
  224. help="Bisect the test suite to discover a test that causes a test failure when combined with the named test.")
  225. parser.add_option('--pair', action='store', dest='pair', default=None,
  226. help="Run the test suite in pairs with the named test to find problem pairs.")
  227. options, args = parser.parse_args()
  228. if options.settings:
  229. os.environ['DJANGO_SETTINGS_MODULE'] = options.settings
  230. elif "DJANGO_SETTINGS_MODULE" not in os.environ:
  231. parser.error("DJANGO_SETTINGS_MODULE is not set in the environment. "
  232. "Set it or use --settings.")
  233. else:
  234. options.settings = os.environ['DJANGO_SETTINGS_MODULE']
  235. if options.bisect:
  236. bisect_tests(options.bisect, options, args)
  237. elif options.pair:
  238. paired_tests(options.pair, options, args)
  239. else:
  240. failures = django_tests(int(options.verbosity), options.interactive, options.failfast, args)
  241. if failures:
  242. sys.exit(bool(failures))