PageRenderTime 23ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/django-1.4/tests/runtests.py

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