runtests.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. #!/usr/bin/env python
  2. import logging
  3. import os
  4. import shutil
  5. import subprocess
  6. import sys
  7. import tempfile
  8. import warnings
  9. from django import contrib
  10. from django.utils._os import upath
  11. from django.utils import six
  12. CONTRIB_MODULE_PATH = 'django.contrib'
  13. TEST_TEMPLATE_DIR = 'templates'
  14. RUNTESTS_DIR = os.path.abspath(os.path.dirname(upath(__file__)))
  15. CONTRIB_DIR = os.path.dirname(upath(contrib.__file__))
  16. TEMP_DIR = tempfile.mkdtemp(prefix='django_')
  17. os.environ['DJANGO_TEST_TEMP_DIR'] = TEMP_DIR
  18. SUBDIRS_TO_SKIP = [
  19. 'requirements',
  20. 'templates',
  21. 'test_discovery_sample',
  22. 'test_discovery_sample2',
  23. 'test_runner_deprecation_app',
  24. 'test_runner_invalid_app',
  25. ]
  26. ALWAYS_INSTALLED_APPS = [
  27. 'django.contrib.contenttypes',
  28. 'django.contrib.auth',
  29. 'django.contrib.sites',
  30. 'django.contrib.flatpages',
  31. 'django.contrib.redirects',
  32. 'django.contrib.sessions',
  33. 'django.contrib.messages',
  34. 'django.contrib.comments',
  35. 'django.contrib.admin',
  36. 'django.contrib.admindocs',
  37. 'django.contrib.staticfiles',
  38. 'django.contrib.humanize',
  39. 'staticfiles_tests',
  40. 'staticfiles_tests.apps.test',
  41. 'staticfiles_tests.apps.no_label',
  42. ]
  43. def get_test_modules():
  44. modules = []
  45. for modpath, dirpath in (
  46. (None, RUNTESTS_DIR),
  47. (CONTRIB_MODULE_PATH, CONTRIB_DIR)):
  48. for f in os.listdir(dirpath):
  49. if ('.' in f or
  50. # Python 3 byte code dirs (PEP 3147)
  51. f == '__pycache__' or
  52. f.startswith('sql') or
  53. os.path.basename(f) in SUBDIRS_TO_SKIP or
  54. os.path.isfile(f)):
  55. continue
  56. modules.append((modpath, f))
  57. return modules
  58. def get_installed():
  59. from django.db.models.loading import get_apps
  60. return [app.__name__.rsplit('.', 1)[0] for app in get_apps()]
  61. def setup(verbosity, test_labels):
  62. from django.conf import settings
  63. from django.db.models.loading import get_apps, load_app
  64. from django.test.testcases import TransactionTestCase, TestCase
  65. # Force declaring available_apps in TransactionTestCase for faster tests.
  66. def no_available_apps(self):
  67. raise Exception("Please define available_apps in TransactionTestCase "
  68. "and its subclasses.")
  69. TransactionTestCase.available_apps = property(no_available_apps)
  70. TestCase.available_apps = None
  71. state = {
  72. 'INSTALLED_APPS': settings.INSTALLED_APPS,
  73. 'ROOT_URLCONF': getattr(settings, "ROOT_URLCONF", ""),
  74. 'TEMPLATE_DIRS': settings.TEMPLATE_DIRS,
  75. 'LANGUAGE_CODE': settings.LANGUAGE_CODE,
  76. 'STATIC_URL': settings.STATIC_URL,
  77. 'STATIC_ROOT': settings.STATIC_ROOT,
  78. }
  79. # Redirect some settings for the duration of these tests.
  80. settings.INSTALLED_APPS = ALWAYS_INSTALLED_APPS
  81. settings.ROOT_URLCONF = 'urls'
  82. settings.STATIC_URL = '/static/'
  83. settings.STATIC_ROOT = os.path.join(TEMP_DIR, 'static')
  84. settings.TEMPLATE_DIRS = (os.path.join(RUNTESTS_DIR, TEST_TEMPLATE_DIR),)
  85. settings.LANGUAGE_CODE = 'en'
  86. settings.SITE_ID = 1
  87. if verbosity > 0:
  88. # Ensure any warnings captured to logging are piped through a verbose
  89. # logging handler. If any -W options were passed explicitly on command
  90. # line, warnings are not captured, and this has no effect.
  91. logger = logging.getLogger('py.warnings')
  92. handler = logging.StreamHandler()
  93. logger.addHandler(handler)
  94. # Load all the ALWAYS_INSTALLED_APPS.
  95. with warnings.catch_warnings():
  96. warnings.filterwarnings('ignore', 'django.contrib.comments is deprecated and will be removed before Django 1.8.', PendingDeprecationWarning)
  97. get_apps()
  98. # Load all the test model apps.
  99. test_modules = get_test_modules()
  100. # Reduce given test labels to just the app module path
  101. test_labels_set = set()
  102. for label in test_labels:
  103. bits = label.split('.')
  104. if bits[:2] == ['django', 'contrib']:
  105. bits = bits[:3]
  106. else:
  107. bits = bits[:1]
  108. test_labels_set.add('.'.join(bits))
  109. # If GeoDjango, then we'll want to add in the test applications
  110. # that are a part of its test suite.
  111. from django.contrib.gis.tests.utils import HAS_SPATIAL_DB
  112. if HAS_SPATIAL_DB:
  113. from django.contrib.gis.tests import geo_apps
  114. test_modules.extend(geo_apps())
  115. settings.INSTALLED_APPS.extend(['django.contrib.gis', 'django.contrib.sitemaps'])
  116. for modpath, module_name in test_modules:
  117. if modpath:
  118. module_label = '.'.join([modpath, module_name])
  119. else:
  120. module_label = module_name
  121. # if the module (or an ancestor) was named on the command line, or
  122. # no modules were named (i.e., run all), import
  123. # this module and add it to INSTALLED_APPS.
  124. if not test_labels:
  125. module_found_in_labels = True
  126. else:
  127. match = lambda label: (
  128. module_label == label or # exact match
  129. module_label.startswith(label + '.') # ancestor match
  130. )
  131. module_found_in_labels = any(match(l) for l in test_labels_set)
  132. if module_found_in_labels:
  133. if verbosity >= 2:
  134. print("Importing application %s" % module_name)
  135. mod = load_app(module_label)
  136. if mod:
  137. if module_label not in settings.INSTALLED_APPS:
  138. settings.INSTALLED_APPS.append(module_label)
  139. return state
  140. def teardown(state):
  141. from django.conf import settings
  142. try:
  143. # Removing the temporary TEMP_DIR. Ensure we pass in unicode
  144. # so that it will successfully remove temp trees containing
  145. # non-ASCII filenames on Windows. (We're assuming the temp dir
  146. # name itself does not contain non-ASCII characters.)
  147. shutil.rmtree(six.text_type(TEMP_DIR))
  148. except OSError:
  149. print('Failed to remove temp directory: %s' % TEMP_DIR)
  150. # Restore the old settings.
  151. for key, value in state.items():
  152. setattr(settings, key, value)
  153. def django_tests(verbosity, interactive, failfast, test_labels):
  154. from django.conf import settings
  155. state = setup(verbosity, test_labels)
  156. extra_tests = []
  157. # Run the test suite, including the extra validation tests.
  158. from django.test.utils import get_runner
  159. if not hasattr(settings, 'TEST_RUNNER'):
  160. settings.TEST_RUNNER = 'django.test.runner.DiscoverRunner'
  161. TestRunner = get_runner(settings)
  162. test_runner = TestRunner(
  163. verbosity=verbosity,
  164. interactive=interactive,
  165. failfast=failfast,
  166. )
  167. failures = test_runner.run_tests(
  168. test_labels or get_installed(), extra_tests=extra_tests)
  169. teardown(state)
  170. return failures
  171. def bisect_tests(bisection_label, options, test_labels):
  172. state = setup(int(options.verbosity), test_labels)
  173. test_labels = test_labels or get_installed()
  174. print('***** Bisecting test suite: %s' % ' '.join(test_labels))
  175. # Make sure the bisection point isn't in the test list
  176. # Also remove tests that need to be run in specific combinations
  177. for label in [bisection_label, 'model_inheritance_same_model_name']:
  178. try:
  179. test_labels.remove(label)
  180. except ValueError:
  181. pass
  182. subprocess_args = [
  183. sys.executable, upath(__file__), '--settings=%s' % options.settings]
  184. if options.failfast:
  185. subprocess_args.append('--failfast')
  186. if options.verbosity:
  187. subprocess_args.append('--verbosity=%s' % options.verbosity)
  188. if not options.interactive:
  189. subprocess_args.append('--noinput')
  190. iteration = 1
  191. while len(test_labels) > 1:
  192. midpoint = len(test_labels)/2
  193. test_labels_a = test_labels[:midpoint] + [bisection_label]
  194. test_labels_b = test_labels[midpoint:] + [bisection_label]
  195. print('***** Pass %da: Running the first half of the test suite' % iteration)
  196. print('***** Test labels: %s' % ' '.join(test_labels_a))
  197. failures_a = subprocess.call(subprocess_args + test_labels_a)
  198. print('***** Pass %db: Running the second half of the test suite' % iteration)
  199. print('***** Test labels: %s' % ' '.join(test_labels_b))
  200. print('')
  201. failures_b = subprocess.call(subprocess_args + test_labels_b)
  202. if failures_a and not failures_b:
  203. print("***** Problem found in first half. Bisecting again...")
  204. iteration = iteration + 1
  205. test_labels = test_labels_a[:-1]
  206. elif failures_b and not failures_a:
  207. print("***** Problem found in second half. Bisecting again...")
  208. iteration = iteration + 1
  209. test_labels = test_labels_b[:-1]
  210. elif failures_a and failures_b:
  211. print("***** Multiple sources of failure found")
  212. break
  213. else:
  214. print("***** No source of failure found... try pair execution (--pair)")
  215. break
  216. if len(test_labels) == 1:
  217. print("***** Source of error: %s" % test_labels[0])
  218. teardown(state)
  219. def paired_tests(paired_test, options, test_labels):
  220. state = setup(int(options.verbosity), test_labels)
  221. test_labels = test_labels or get_installed()
  222. print('***** Trying paired execution')
  223. # Make sure the constant member of the pair isn't in the test list
  224. # Also remove tests that need to be run in specific combinations
  225. for label in [paired_test, 'model_inheritance_same_model_name']:
  226. try:
  227. test_labels.remove(label)
  228. except ValueError:
  229. pass
  230. subprocess_args = [
  231. sys.executable, upath(__file__), '--settings=%s' % options.settings]
  232. if options.failfast:
  233. subprocess_args.append('--failfast')
  234. if options.verbosity:
  235. subprocess_args.append('--verbosity=%s' % options.verbosity)
  236. if not options.interactive:
  237. subprocess_args.append('--noinput')
  238. for i, label in enumerate(test_labels):
  239. print('***** %d of %d: Check test pairing with %s' % (
  240. i + 1, len(test_labels), label))
  241. failures = subprocess.call(subprocess_args + [label, paired_test])
  242. if failures:
  243. print('***** Found problem pair with %s' % label)
  244. return
  245. print('***** No problem pair found')
  246. teardown(state)
  247. if __name__ == "__main__":
  248. from optparse import OptionParser
  249. usage = "%prog [options] [module module module ...]"
  250. parser = OptionParser(usage=usage)
  251. parser.add_option(
  252. '-v', '--verbosity', action='store', dest='verbosity', default='1',
  253. type='choice', choices=['0', '1', '2', '3'],
  254. help='Verbosity level; 0=minimal output, 1=normal output, 2=all '
  255. 'output')
  256. parser.add_option(
  257. '--noinput', action='store_false', dest='interactive', default=True,
  258. help='Tells Django to NOT prompt the user for input of any kind.')
  259. parser.add_option(
  260. '--failfast', action='store_true', dest='failfast', default=False,
  261. help='Tells Django to stop running the test suite after first failed '
  262. 'test.')
  263. parser.add_option(
  264. '--settings',
  265. help='Python path to settings module, e.g. "myproject.settings". If '
  266. 'this isn\'t provided, the DJANGO_SETTINGS_MODULE environment '
  267. 'variable will be used.')
  268. parser.add_option(
  269. '--bisect', action='store', dest='bisect', default=None,
  270. help='Bisect the test suite to discover a test that causes a test '
  271. 'failure when combined with the named test.')
  272. parser.add_option(
  273. '--pair', action='store', dest='pair', default=None,
  274. help='Run the test suite in pairs with the named test to find problem '
  275. 'pairs.')
  276. parser.add_option(
  277. '--liveserver', action='store', dest='liveserver', default=None,
  278. help='Overrides the default address where the live server (used with '
  279. 'LiveServerTestCase) is expected to run from. The default value '
  280. 'is localhost:8081.')
  281. parser.add_option(
  282. '--selenium', action='store_true', dest='selenium',
  283. default=False,
  284. help='Run the Selenium tests as well (if Selenium is installed)')
  285. options, args = parser.parse_args()
  286. if options.settings:
  287. os.environ['DJANGO_SETTINGS_MODULE'] = options.settings
  288. elif "DJANGO_SETTINGS_MODULE" not in os.environ:
  289. parser.error("DJANGO_SETTINGS_MODULE is not set in the environment. "
  290. "Set it or use --settings.")
  291. else:
  292. options.settings = os.environ['DJANGO_SETTINGS_MODULE']
  293. if options.liveserver is not None:
  294. os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] = options.liveserver
  295. if options.selenium:
  296. os.environ['DJANGO_SELENIUM_TESTS'] = '1'
  297. if options.bisect:
  298. bisect_tests(options.bisect, options, args)
  299. elif options.pair:
  300. paired_tests(options.pair, options, args)
  301. else:
  302. failures = django_tests(int(options.verbosity), options.interactive,
  303. options.failfast, args)
  304. if failures:
  305. sys.exit(bool(failures))