Ticket #8363: t8363-r9569.diff

File t8363-r9569.diff, 8.4 KB (added by Ramiro Morales, 16 years ago)

Patch modified to use --exclude option and updated to r9569

  • django/test/simple.py

    diff --git a/django/test/simple.py b/django/test/simple.py
    a b  
    7373    return suite
    7474
    7575def build_test(label):
    76     """Construct a test case a test with the specified label. Label should
    77     be of the form model.TestClass or model.TestClass.test_method. Returns
     76    """Construct a test case with the specified label. Label should
     77    be of the form app.TestClass or app.TestClass.test_method. Returns
    7878    an instantiated test or test suite corresponding to the label provided.
    7979       
    8080    """
     
    9999    else: # label is app.TestClass.test_method
    100100        return TestClass(parts[2])
    101101
    102 def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]):
     102def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=None, exclude_labels=None):
    103103    """
    104104    Run the unit tests for all the test labels in the provided list.
    105105    Labels must be of the form:
     
    115115   
    116116    A list of 'extra' tests may also be provided; these tests
    117117    will be added to the test suite.
    118    
     118
     119    It's also possible to specify a list of labels to exclude from the
     120    test suite by using the exclude_labels parameter.
     121
    119122    Returns the number of tests that failed.
    120123    """
    121124    setup_test_environment()
    122    
    123     settings.DEBUG = False   
     125
     126    if extra_tests is None:
     127        extra_tests = []
     128    if exclude_labels is None:
     129        exclude_labels = []
     130
     131    settings.DEBUG = False
    124132    suite = unittest.TestSuite()
    125133   
    126134    if test_labels:
    127135        for label in test_labels:
     136            if label in exclude_labels:
     137                if verbosity >= 1:
     138                    print 'Skipping test %s' % label
     139                continue
    128140            if '.' in label:
    129141                suite.addTest(build_test(label))
    130142            else:
  • tests/runtests.py

    diff --git a/tests/runtests.py b/tests/runtests.py
    a b  
    3232    'django.contrib.admin',
    3333]
    3434
    35 def get_test_models():
    36     models = []
     35def get_test_modules():
     36    modules = []
    3737    for loc, dirpath in (MODEL_TESTS_DIR_NAME, MODEL_TEST_DIR), (REGRESSION_TESTS_DIR_NAME, REGRESSION_TEST_DIR), (CONTRIB_DIR_NAME, CONTRIB_DIR):
    3838        for f in os.listdir(dirpath):
    3939            if f.startswith('__init__') or f.startswith('.') or f.startswith('sql') or f.startswith('invalid'):
    4040                continue
    41             models.append((loc, f))
    42     return models
     41            modules.append((loc, f))
     42    return modules
    4343
    44 def get_invalid_models():
    45     models = []
     44def get_invalid_tests_modules():
     45    modules = []
    4646    for loc, dirpath in (MODEL_TESTS_DIR_NAME, MODEL_TEST_DIR), (REGRESSION_TESTS_DIR_NAME, REGRESSION_TEST_DIR), (CONTRIB_DIR_NAME, CONTRIB_DIR):
    4747        for f in os.listdir(dirpath):
    4848            if f.startswith('__init__') or f.startswith('.') or f.startswith('sql'):
    4949                continue
    5050            if f.startswith('invalid'):
    51                 models.append((loc, f))
    52     return models
     51                modules.append((loc, f))
     52    return modules
    5353
    5454class InvalidModelTestCase(unittest.TestCase):
    5555    def __init__(self, model_label):
     
    8585        self.assert_(not unexpected, "Unexpected Errors: " + '\n'.join(unexpected))
    8686        self.assert_(not missing, "Missing Errors: " + '\n'.join(missing))
    8787
    88 def django_tests(verbosity, interactive, test_labels):
     88def django_tests(verbosity, interactive, test_labels, exclude_labels=None):
    8989    from django.conf import settings
     90
     91    if exclude_labels is None:
     92        exclude_labels = []
    9093
    9194    old_installed_apps = settings.INSTALLED_APPS
    9295    old_test_database_name = settings.TEST_DATABASE_NAME
     
    118121    get_apps()
    119122
    120123    # Load all the test model apps.
    121     for model_dir, model_name in get_test_models():
    122         model_label = '.'.join([model_dir, model_name])
     124    test_labels_set = set([label.split('.')[0] for label in test_labels if label not in exclude_labels])
     125    for module_dir, module_name in get_test_modules():
     126        module_label = '.'.join([module_dir, module_name])
    123127        try:
    124             # if the model was named on the command line, or
    125             # no models were named (i.e., run all), import
    126             # this model and add it to the list to test.
    127             if not test_labels or model_name in set([label.split('.')[0] for label in test_labels]):
     128            # if the module was named on the command line, or
     129            # no modules were named (i.e., run all), import
     130            # this module and add it to the list to test.
     131            if not test_labels or module_name in test_labels_set:
     132                if module_name in exclude_labels:
     133                    if verbosity >= 1:
     134                        print "Skipping app %s" % module_name
     135                    continue
    128136                if verbosity >= 1:
    129                     print "Importing model %s" % model_name
    130                 mod = load_app(model_label)
     137                    print "Importing app %s" % module_name
     138                mod = load_app(module_label)
    131139                if mod:
    132                     if model_label not in settings.INSTALLED_APPS:
    133                         settings.INSTALLED_APPS.append(model_label)
     140                    if module_label not in settings.INSTALLED_APPS:
     141                        settings.INSTALLED_APPS.append(module_label)
    134142        except Exception, e:
    135             sys.stderr.write("Error while importing %s:" % model_name + ''.join(traceback.format_exception(*sys.exc_info())[1:]))
     143            sys.stderr.write("Error while importing %s:" % module_name + ''.join(traceback.format_exception(*sys.exc_info())[1:]))
    136144            continue
    137145
    138     # Add tests for invalid models.
     146    # Add tests for invalid models apps.
    139147    extra_tests = []
    140     for model_dir, model_name in get_invalid_models():
    141         model_label = '.'.join([model_dir, model_name])
    142         if not test_labels or model_name in test_labels:
    143             extra_tests.append(InvalidModelTestCase(model_label))
     148    for module_dir, module_name in get_invalid_tests_modules():
     149        module_label = '.'.join([module_dir, module_name])
     150        if not test_labels or module_name in test_labels:
     151            extra_tests.append(InvalidModelTestCase(module_label))
    144152            try:
    145                 # Invalid models are not working apps, so we cannot pass them into
    146                 # the test runner with the other test_labels
    147                 test_labels.remove(model_name)
     153                # Invalid models apps are not working apps, so we cannot pass
     154                # them into the test runner with the other test_labels
     155                test_labels.remove(module_name)
    148156            except ValueError:
    149157                pass
    150158
    151     # Run the test suite, including the extra validation tests.
     159    # Run the test suite, including the extra validation tests and skipping
     160    # the test explicitely excluded.
    152161    from django.test.simple import run_tests
    153     failures = run_tests(test_labels, verbosity=verbosity, interactive=interactive, extra_tests=extra_tests)
     162    failures = run_tests(test_labels, verbosity=verbosity, interactive=interactive,
     163        extra_tests=extra_tests, exclude_labels=exclude_labels)
    154164    if failures:
    155165        sys.exit(failures)
    156166
     
    165175
    166176if __name__ == "__main__":
    167177    from optparse import OptionParser
    168     usage = "%prog [options] [model model model ...]"
     178    usage = "%prog [options] [module module module ...]"
    169179    parser = OptionParser(usage=usage)
    170180    parser.add_option('-v','--verbosity', action='store', dest='verbosity', default='0',
    171181        type='choice', choices=['0', '1', '2'],
     
    174184        help='Tells Django to NOT prompt the user for input of any kind.')
    175185    parser.add_option('--settings',
    176186        help='Python path to settings module, e.g. "myproject.settings". If this isn\'t provided, the DJANGO_SETTINGS_MODULE environment variable will be used.')
     187    parser.add_option('-e', '--exclude', action='append', dest='exclude', default=[],
     188        help='Test to exclude (use multiple times to exclude multiple tests).')
    177189    options, args = parser.parse_args()
    178190    if options.settings:
    179191        os.environ['DJANGO_SETTINGS_MODULE'] = options.settings
    180192    elif "DJANGO_SETTINGS_MODULE" not in os.environ:
    181193        parser.error("DJANGO_SETTINGS_MODULE is not set in the environment. "
    182194                      "Set it or use --settings.")
    183     django_tests(int(options.verbosity), options.interactive, args)
     195    django_tests(int(options.verbosity), options.interactive, args, options.exclude)
Back to Top