Ticket #8363: 8363-r14297.2.diff

File 8363-r14297.2.diff, 7.3 KB (added by Ramiro Morales, 14 years ago)
  • django/test/simple.py

    diff --git a/django/test/simple.py b/django/test/simple.py
    a b  
    1 import sys
    2 import signal
    3 
    41from django.conf import settings
    52from django.db.models import get_app, get_apps
    63from django.test import _doctest as doctest
     
    2623    try:
    2724        app_path = app_module.__name__.split('.')[:-1]
    2825        test_module = __import__('.'.join(app_path + [TEST_MODULE]), {}, {}, TEST_MODULE)
    29     except ImportError, e:
     26    except ImportError:
    3027        # Couldn't import tests.py. Was it due to a missing file, or
    3128        # due to an import error in a tests.py that actually exists?
    3229        import os.path
     
    198195    def build_suite(self, test_labels, extra_tests=None, **kwargs):
    199196        suite = unittest.TestSuite()
    200197
     198        exclude_labels = kwargs.get('exclude_labels')
     199        if exclude_labels is None:
     200            exclude_labels = []
     201
    201202        if test_labels:
    202203            for label in test_labels:
     204                if label in exclude_labels:
     205                    if self.verbosity >= 1:
     206                        if '.' in label:
     207                            print 'Skipping %s test' % label
     208                        else:
     209                            print 'Skipping tests from app %s' % label
     210                    continue
    203211                if '.' in label:
    204212                    suite.addTest(build_test(label))
    205213                else:
     
    269277        A list of 'extra' tests may also be provided; these tests
    270278        will be added to the test suite.
    271279
     280        It's also possible to specify a list of labels to exclude from the
     281        test suite by using the exclude_labels parameter.
     282
    272283        Returns the number of tests that failed.
    273284        """
     285        exclude_labels = kwargs.get('exclude_labels')
    274286        self.setup_test_environment()
    275         suite = self.build_suite(test_labels, extra_tests)
     287        suite = self.build_suite(test_labels, extra_tests, exclude_labels=exclude_labels)
    276288        old_config = self.setup_databases()
    277289        result = self.run_suite(suite)
    278290        self.teardown_databases(old_config)
  • tests/runtests.py

    diff --git a/tests/runtests.py b/tests/runtests.py
    a b  
    11#!/usr/bin/env python
    2 import os, subprocess, sys, traceback
     2import os
     3import subprocess
     4import sys
    35
    46import django.contrib as contrib
    57from django.utils import unittest
     
    6365
    6466        try:
    6567            module = load_app(self.model_label)
    66         except Exception, e:
     68        except Exception:
    6769            self.fail('Unable to load invalid model module')
    6870
    6971        # Make sure sys.stdout is not a tty so that we get errors without
     
    7274        orig_stdout = sys.stdout
    7375        s = StringIO()
    7476        sys.stdout = s
    75         count = get_validation_errors(s, module)
     77        get_validation_errors(s, module)
    7678        sys.stdout = orig_stdout
    7779        s.seek(0)
    7880        error_log = s.read()
     
    8587        self.assert_(not unexpected, "Unexpected Errors: " + '\n'.join(unexpected))
    8688        self.assert_(not missing, "Missing Errors: " + '\n'.join(missing))
    8789
    88 def setup(verbosity, test_labels):
     90def setup(verbosity, test_labels, exclude_labels):
    8991    from django.conf import settings
    9092    state = {
    9193        'INSTALLED_APPS': settings.INSTALLED_APPS,
     
    116118    # in our tests.
    117119    settings.MANAGERS = ("admin@djangoproject.com",)
    118120
     121    if exclude_labels is None:
     122        exclude_labels = []
     123
    119124    # Load all the ALWAYS_INSTALLED_APPS.
    120125    # (This import statement is intentionally delayed until after we
    121126    # access settings because of the USE_I18N dependency.)
    122127    from django.db.models.loading import get_apps, load_app
    123128    get_apps()
    124129
     130    # Only avoid loading an app if it is fully excluded, if testcases or test
     131    # methods names were excluded then load its apps normally
     132    exclude_apps = [label for label in exclude_labels if '.' not in label]
     133
    125134    # Load all the test model apps.
    126     test_labels_set = set([label.split('.')[0] for label in test_labels])
     135    test_labels_set = set([label.split('.')[0] for label in test_labels if label not in exclude_apps])
    127136    for model_dir, model_name in get_test_models():
    128137        model_label = '.'.join([model_dir, model_name])
    129         # if the model was named on the command line, or
    130         # no models were named (i.e., run all), import
    131         # this model and add it to the list to test.
     138        # if the model was named on the command line, or no models were named
     139        # (i.e., run all), import this model and add it to the list to test.
     140        # Also, skip importing the test apps explicitly excluded by the user.
    132141        if not test_labels or model_name in test_labels_set:
     142            if model_name in exclude_apps:
     143                if verbosity >= 2:
     144                    print "Skipping import of app %s" % model_name
     145                continue
    133146            if verbosity >= 2:
    134147                print "Importing model %s" % model_name
    135148            mod = load_app(model_label)
     
    145158    for key, value in state.items():
    146159        setattr(settings, key, value)
    147160
    148 def django_tests(verbosity, interactive, failfast, test_labels):
     161def django_tests(verbosity, interactive, failfast, test_labels, exclude_labels=None):
    149162    from django.conf import settings
    150     state = setup(verbosity, test_labels)
     163    state = setup(verbosity, test_labels, exclude_labels)
    151164
    152165    # Add tests for invalid models.
    153166    extra_tests = []
     
    162175            except ValueError:
    163176                pass
    164177
    165     # Run the test suite, including the extra validation tests.
     178    # Run the test suite, including the extra validation tests and skipping
     179    # the test explicitely excluded.
    166180    from django.test.utils import get_runner
    167181    if not hasattr(settings, 'TEST_RUNNER'):
    168182        settings.TEST_RUNNER = 'django.test.simple.DjangoTestSuiteRunner'
     
    180194            extra_tests=extra_tests)
    181195    else:
    182196        test_runner = TestRunner(verbosity=verbosity, interactive=interactive, failfast=failfast)
    183         failures = test_runner.run_tests(test_labels, extra_tests=extra_tests)
     197        failures = test_runner.run_tests(test_labels, extra_tests=extra_tests, exclude_labels=exclude_labels)
    184198
    185199    teardown(state)
    186200    return failures
     
    299313        help="Bisect the test suite to discover a test that causes a test failure when combined with the named test.")
    300314    parser.add_option('--pair', action='store', dest='pair', default=None,
    301315        help="Run the test suite in pairs with the named test to find problem pairs.")
     316    parser.add_option('-e', '--exclude', action='append', dest='exclude', default=None,
     317        help='Test to exclude (use multiple times to exclude multiple tests).')
    302318    options, args = parser.parse_args()
    303319    if options.settings:
    304320        os.environ['DJANGO_SETTINGS_MODULE'] = options.settings
     
    311327    elif options.pair:
    312328        paired_tests(options.pair, options, args)
    313329    else:
    314         failures = django_tests(int(options.verbosity), options.interactive, options.failfast, args)
     330        failures = django_tests(
     331                int(options.verbosity),
     332                options.interactive,
     333                options.failfast,
     334                args,
     335                options.exclude
     336            )
    315337        if failures:
    316338            sys.exit(bool(failures))
Back to Top