diff --git a/tests/runtests.py b/tests/runtests.py index 359fca2bf5..20189c2d99 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -40,12 +40,12 @@ def get_invalid_models(): if f.startswith('invalid'): models.append((loc, f)) return models - + class InvalidModelTestCase(unittest.TestCase): def __init__(self, model_label): unittest.TestCase.__init__(self) self.model_label = model_label - + def runTest(self): from django.core import management from django.db.models.loading import load_app @@ -55,7 +55,7 @@ class InvalidModelTestCase(unittest.TestCase): module = load_app(self.model_label) except Exception, e: self.fail('Unable to load invalid model module') - + s = StringIO() count = management.get_validation_errors(s, module) s.seek(0) @@ -71,39 +71,43 @@ class InvalidModelTestCase(unittest.TestCase): def django_tests(verbosity, tests_to_run): from django.conf import settings - from django.db.models.loading import get_apps, load_app old_installed_apps = settings.INSTALLED_APPS old_test_database_name = settings.TEST_DATABASE_NAME old_root_urlconf = settings.ROOT_URLCONF old_template_dirs = settings.TEMPLATE_DIRS - + old_use_i18n = settings.USE_I18N + # Redirect some settings for the duration of these tests settings.TEST_DATABASE_NAME = TEST_DATABASE_NAME settings.INSTALLED_APPS = ALWAYS_INSTALLED_APPS settings.ROOT_URLCONF = 'urls' settings.TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__), TEST_TEMPLATE_DIR),) - - # load all the ALWAYS_INSTALLED_APPS + settings.USE_I18N = True + + # Load all the ALWAYS_INSTALLED_APPS. + # (This import statement is intentionally delayed until after we + # access settings because of the USE_I18N dependency.) + from django.db.models.loading import get_apps, load_app get_apps() - + # Load all the test model apps test_models = [] - for model_dir, model_name in get_test_models(): + for model_dir, model_name in get_test_models(): model_label = '.'.join([model_dir, model_name]) try: # if the model was named on the command line, or - # no models were named (i.e., run all), import + # no models were named (i.e., run all), import # this model and add it to the list to test. if not tests_to_run or model_name in tests_to_run: if verbosity >= 1: print "Importing model %s" % model_name mod = load_app(model_label) - settings.INSTALLED_APPS.append(model_label) + settings.INSTALLED_APPS.append(model_label) test_models.append(mod) except Exception, e: sys.stderr.write("Error while importing %s:" % model_name + ''.join(traceback.format_exception(*sys.exc_info())[1:])) - continue + continue # Add tests for invalid models extra_tests = [] @@ -111,28 +115,29 @@ def django_tests(verbosity, tests_to_run): model_label = '.'.join([model_dir, model_name]) if not tests_to_run or model_name in tests_to_run: extra_tests.append(InvalidModelTestCase(model_label)) - + # Run the test suite, including the extra validation tests. from django.test.simple import run_tests run_tests(test_models, verbosity, extra_tests=extra_tests) - + # Restore the old settings settings.INSTALLED_APPS = old_installed_apps settings.TESTS_DATABASE_NAME = old_test_database_name settings.ROOT_URLCONF = old_root_urlconf settings.TEMPLATE_DIRS = old_template_dirs - + settings.USE_I18N = old_use_i18n + if __name__ == "__main__": from optparse import OptionParser usage = "%prog [options] [model model model ...]" parser = OptionParser(usage=usage) parser.add_option('-v','--verbosity', action='store', dest='verbosity', default='0', type='choice', choices=['0', '1', '2'], - help='Verbosity level; 0=minimal output, 1=normal output, 2=all output') + help='Verbosity level; 0=minimal output, 1=normal output, 2=all output') parser.add_option('--settings', help='Python path to settings module, e.g. "myproject.settings". If this isn\'t provided, the DJANGO_SETTINGS_MODULE environment variable will be used.') options, args = parser.parse_args() if options.settings: os.environ['DJANGO_SETTINGS_MODULE'] = options.settings - + django_tests(int(options.verbosity), args)