Commit d4dc7756 authored by Jakub Paczkowski's avatar Jakub Paczkowski Committed by Tim Graham
Browse files

Fixed #25735 -- Added support for test tags to DiscoverRunner.

Thanks Carl Meyer, Claude Paroz, and Simon Charette for review.
parent 0db7e610
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -307,6 +307,7 @@ answer newbie questions, and generally made Django that much better:
    Jaap Roes <jaap.roes@gmail.com>
    Jacob Burch <jacobburch@gmail.com>
    Jacob Kaplan-Moss <jacob@jacobian.org>
    Jakub Paczkowski <jakub@paczkowski.eu>
    Jakub Wilk <jwilk@jwilk.net>
    Jakub Wiśniowski <restless.being@gmail.com>
    james_027@yahoo.com
+29 −2
Original line number Diff line number Diff line
@@ -360,11 +360,10 @@ class DiscoverRunner(object):
    def __init__(self, pattern=None, top_level=None, verbosity=1,
                 interactive=True, failfast=False, keepdb=False,
                 reverse=False, debug_sql=False, parallel=0,
                 **kwargs):
                 tags=None, exclude_tags=None, **kwargs):

        self.pattern = pattern
        self.top_level = top_level

        self.verbosity = verbosity
        self.interactive = interactive
        self.failfast = failfast
@@ -372,6 +371,8 @@ class DiscoverRunner(object):
        self.reverse = reverse
        self.debug_sql = debug_sql
        self.parallel = parallel
        self.tags = set(tags or [])
        self.exclude_tags = set(exclude_tags or [])

    @classmethod
    def add_arguments(cls, parser):
@@ -394,6 +395,10 @@ class DiscoverRunner(object):
            '--parallel', dest='parallel', nargs='?', default=1, type=int,
            const=default_test_processes(), metavar='N',
            help='Run tests using up to N parallel processes.')
        parser.add_argument('--tag', action='append', dest='tags',
            help='Run only tests with the specified tag. Can be used multiple times.')
        parser.add_argument('--exclude-tag', action='append', dest='exclude_tags',
            help='Do not run tests with the specified tag. Can be used multiple times.')

    def setup_test_environment(self, **kwargs):
        setup_test_environment()
@@ -459,6 +464,8 @@ class DiscoverRunner(object):
        for test in extra_tests:
            suite.addTest(test)

        if self.tags or self.exclude_tags:
            suite = filter_tests_by_tags(suite, self.tags, self.exclude_tags)
        suite = reorder_suite(suite, self.reorder_by, self.reverse)

        if self.parallel > 1:
@@ -747,3 +754,23 @@ def setup_databases(verbosity, interactive, keepdb=False, debug_sql=False, paral
            connections[alias].force_debug_cursor = True

    return old_names


def filter_tests_by_tags(suite, tags, exclude_tags):
    suite_class = type(suite)
    filtered_suite = suite_class()

    for test in suite:
        if isinstance(test, suite_class):
            filtered_suite.addTests(filter_tests_by_tags(test, tags, exclude_tags))
        else:
            test_tags = set(getattr(test, 'tags', set()))
            test_fn_name = getattr(test, '_testMethodName', str(test))
            test_fn = getattr(test, test_fn_name, test)
            test_fn_tags = set(getattr(test_fn, 'tags', set()))
            all_tags = test_tags.union(test_fn_tags)
            matched_tags = all_tags.intersection(tags)
            if (matched_tags or not tags) and not all_tags.intersection(exclude_tags):
                filtered_suite.addTest(test)

    return filtered_suite
+10 −0
Original line number Diff line number Diff line
@@ -707,3 +707,13 @@ class isolate_apps(TestContextDecorator):

    def disable(self):
        setattr(Options, 'default_apps', self.old_apps)


def tag(*tags):
    """
    Decorator to add tags to a test class or method.
    """
    def decorator(obj):
        setattr(obj, 'tags', set(tags))
        return obj
    return decorator
+14 −0
Original line number Diff line number Diff line
@@ -1348,6 +1348,20 @@ don't.
    in order to exchange them between processes. See
    :ref:`python:pickle-picklable` for details.

.. option:: --tag TAGS

.. versionadded:: 1.10

Runs only tests :ref:`marked with the specified tags <topics-tagging-tests>`.
May be specified multiple times and combined with :option:`test --exclude-tag`.

.. option:: --exclude-tag EXCLUDE_TAGS

.. versionadded:: 1.10

Excludes tests :ref:`marked with the specified tags <topics-tagging-tests>`.
May be specified multiple times and combined with :option:`test --tag`.

``testserver``
--------------

+4 −0
Original line number Diff line number Diff line
@@ -336,6 +336,10 @@ Tests
* To better catch bugs, :class:`~django.test.TestCase` now checks deferrable
  database constraints at the end of each test.

* Tests and test cases can be :ref:`marked with tags <topics-tagging-tests>`
  and run selectively with the new :option:`test --tag` and :option:`test
  --exclude-tag` options.

URLs
~~~~

Loading