|
| 1 | +#!/usr/bin/env python |
| 2 | +import os |
| 3 | +import shutil |
| 4 | +import sys |
| 5 | +import tempfile |
| 6 | + |
| 7 | +TEST_ROOT = os.path.realpath(os.path.dirname(__file__)) |
| 8 | +sys.path.insert(0, os.path.join(TEST_ROOT, os.pardir)) |
| 9 | + |
| 10 | +RUNTESTS_DIR = os.path.join(TEST_ROOT, 'xtests') |
| 11 | +TEST_TEMPLATE_DIR = 'templates' |
| 12 | +TEMP_DIR = tempfile.mkdtemp(prefix='django_') |
| 13 | +os.environ['DJANGO_TEST_TEMP_DIR'] = TEMP_DIR |
| 14 | + |
| 15 | +ALWAYS_INSTALLED_APPS = [ |
| 16 | + 'django.contrib.auth', |
| 17 | + 'django.contrib.contenttypes', |
| 18 | + 'django.contrib.sessions', |
| 19 | + 'django.contrib.messages', |
| 20 | + 'django.contrib.staticfiles', |
| 21 | + |
| 22 | + 'xadmin', |
| 23 | + 'crispy_forms', |
| 24 | + 'reversion', |
| 25 | +] |
| 26 | + |
| 27 | +def get_test_modules(): |
| 28 | + modules = [] |
| 29 | + for f in os.listdir(RUNTESTS_DIR): |
| 30 | + if (f.startswith('__init__') or |
| 31 | + f.startswith('.') or |
| 32 | + f.startswith('sql')): |
| 33 | + continue |
| 34 | + modules.append(f) |
| 35 | + return modules |
| 36 | + |
| 37 | +def setup(verbosity, test_labels): |
| 38 | + from django.conf import settings |
| 39 | + state = { |
| 40 | + 'INSTALLED_APPS': settings.INSTALLED_APPS, |
| 41 | + 'ROOT_URLCONF': getattr(settings, "ROOT_URLCONF", ""), |
| 42 | + 'TEMPLATE_DIRS': settings.TEMPLATE_DIRS, |
| 43 | + 'USE_I18N': settings.USE_I18N, |
| 44 | + 'LOGIN_URL': settings.LOGIN_URL, |
| 45 | + 'LANGUAGE_CODE': settings.LANGUAGE_CODE, |
| 46 | + 'MIDDLEWARE_CLASSES': settings.MIDDLEWARE_CLASSES, |
| 47 | + 'STATIC_URL': settings.STATIC_URL, |
| 48 | + 'STATIC_ROOT': settings.STATIC_ROOT, |
| 49 | + } |
| 50 | + |
| 51 | + # Redirect some settings for the duration of these tests. |
| 52 | + settings.INSTALLED_APPS = ALWAYS_INSTALLED_APPS |
| 53 | + settings.ROOT_URLCONF = 'urls' |
| 54 | + settings.STATIC_URL = '/static/' |
| 55 | + settings.STATIC_ROOT = os.path.join(TEMP_DIR, 'static') |
| 56 | + settings.TEMPLATE_DIRS = (os.path.join(RUNTESTS_DIR, TEST_TEMPLATE_DIR),) |
| 57 | + settings.USE_I18N = True |
| 58 | + settings.LANGUAGE_CODE = 'en' |
| 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 = ( "[email protected]",) |
| 70 | + |
| 71 | + # Load all the ALWAYS_INSTALLED_APPS. |
| 72 | + # (This import statement is intentionally delayed until after we |
| 73 | + # access settings because of the USE_I18N dependency.) |
| 74 | + from django.db.models.loading import get_apps, load_app |
| 75 | + get_apps() |
| 76 | + |
| 77 | + # Load all the test model apps. |
| 78 | + test_labels_set = set([label.split('.')[0] for label in test_labels]) |
| 79 | + test_modules = get_test_modules() |
| 80 | + |
| 81 | + for module_name in test_modules: |
| 82 | + module_label = '.'.join(['xtests', module_name]) |
| 83 | + # if the module was named on the command line, or |
| 84 | + # no modules were named (i.e., run all), import |
| 85 | + # this module and add it to the list to test. |
| 86 | + if not test_labels or module_name in test_labels_set: |
| 87 | + if verbosity >= 2: |
| 88 | + print "Importing application %s" % module_name |
| 89 | + mod = load_app(module_label) |
| 90 | + if mod: |
| 91 | + if module_label not in settings.INSTALLED_APPS: |
| 92 | + settings.INSTALLED_APPS.append(module_label) |
| 93 | + |
| 94 | + return state |
| 95 | + |
| 96 | +def teardown(state): |
| 97 | + from django.conf import settings |
| 98 | + # Removing the temporary TEMP_DIR. Ensure we pass in unicode |
| 99 | + # so that it will successfully remove temp trees containing |
| 100 | + # non-ASCII filenames on Windows. (We're assuming the temp dir |
| 101 | + # name itself does not contain non-ASCII characters.) |
| 102 | + shutil.rmtree(unicode(TEMP_DIR)) |
| 103 | + # Restore the old settings. |
| 104 | + for key, value in state.items(): |
| 105 | + setattr(settings, key, value) |
| 106 | + |
| 107 | +def django_tests(verbosity, interactive, failfast, test_labels): |
| 108 | + from django.conf import settings |
| 109 | + state = setup(verbosity, test_labels) |
| 110 | + extra_tests = [] |
| 111 | + |
| 112 | + # Run the test suite, including the extra validation tests. |
| 113 | + from django.test.utils import get_runner |
| 114 | + if not hasattr(settings, 'TEST_RUNNER'): |
| 115 | + settings.TEST_RUNNER = 'django.test.simple.DjangoTestSuiteRunner' |
| 116 | + TestRunner = get_runner(settings) |
| 117 | + |
| 118 | + test_runner = TestRunner(verbosity=verbosity, interactive=interactive, |
| 119 | + failfast=failfast) |
| 120 | + failures = test_runner.run_tests(test_labels or get_test_modules(), extra_tests=extra_tests) |
| 121 | + |
| 122 | + teardown(state) |
| 123 | + return failures |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + from optparse import OptionParser |
| 127 | + usage = "%prog [options] [module module module ...]" |
| 128 | + parser = OptionParser(usage=usage) |
| 129 | + parser.add_option( |
| 130 | + '-v','--verbosity', action='store', dest='verbosity', default='1', |
| 131 | + type='choice', choices=['0', '1', '2', '3'], |
| 132 | + help='Verbosity level; 0=minimal output, 1=normal output, 2=all ' |
| 133 | + 'output') |
| 134 | + parser.add_option( |
| 135 | + '--noinput', action='store_false', dest='interactive', default=True, |
| 136 | + help='Tells Django to NOT prompt the user for input of any kind.') |
| 137 | + parser.add_option( |
| 138 | + '--failfast', action='store_true', dest='failfast', default=False, |
| 139 | + help='Tells Django to stop running the test suite after first failed ' |
| 140 | + 'test.') |
| 141 | + parser.add_option( |
| 142 | + '--settings', |
| 143 | + help='Python path to settings module, e.g. "myproject.settings". If ' |
| 144 | + 'this isn\'t provided, the DJANGO_SETTINGS_MODULE environment ' |
| 145 | + 'variable will be used.') |
| 146 | + parser.add_option( |
| 147 | + '--liveserver', action='store', dest='liveserver', default=None, |
| 148 | + help='Overrides the default address where the live server (used with ' |
| 149 | + 'LiveServerTestCase) is expected to run from. The default value ' |
| 150 | + 'is localhost:8081.'), |
| 151 | + options, args = parser.parse_args() |
| 152 | + if options.settings: |
| 153 | + os.environ['DJANGO_SETTINGS_MODULE'] = options.settings |
| 154 | + elif "DJANGO_SETTINGS_MODULE" not in os.environ: |
| 155 | + os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' |
| 156 | + else: |
| 157 | + options.settings = os.environ['DJANGO_SETTINGS_MODULE'] |
| 158 | + |
| 159 | + if options.liveserver is not None: |
| 160 | + os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] = options.liveserver |
| 161 | + |
| 162 | + failures = django_tests(int(options.verbosity), options.interactive, |
| 163 | + options.failfast, args) |
| 164 | + if failures: |
| 165 | + sys.exit(bool(failures)) |
0 commit comments