diff --git a/django/test/runner.py b/django/test/runner.py index aba4fc2b39..46c8c5842d 100644 --- a/django/test/runner.py +++ b/django/test/runner.py @@ -800,18 +800,20 @@ def partition_suite_by_case(suite): ] +def test_match_tags(test, tags, exclude_tags): + test_tags = set(getattr(test, 'tags', [])) + test_fn_name = getattr(test, '_testMethodName', str(test)) + if hasattr(test, test_fn_name): + test_fn = getattr(test, test_fn_name) + test_fn_tags = list(getattr(test_fn, 'tags', [])) + test_tags = test_tags.union(test_fn_tags) + matched_tags = test_tags.intersection(tags) + return (matched_tags or not tags) and not test_tags.intersection(exclude_tags) + + def filter_tests_by_tags(suite, tags, exclude_tags): suite_class = type(suite) - filtered_suite = suite_class() - - for test in iter_test_cases(suite): - 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 + return suite_class( + test for test in iter_test_cases(suite) + if test_match_tags(test, tags, exclude_tags) + )