PageRenderTime 73ms CodeModel.GetById 38ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 1ms

/tests/runtests.py

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