Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

runner.py 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. import ctypes
  2. import itertools
  3. import logging
  4. import multiprocessing
  5. import os
  6. import pickle
  7. import textwrap
  8. import unittest
  9. from importlib import import_module
  10. from io import StringIO
  11. from django.core.management import call_command
  12. from django.db import connections
  13. from django.test import SimpleTestCase, TestCase
  14. from django.test.utils import (
  15. setup_databases as _setup_databases, setup_test_environment,
  16. teardown_databases as _teardown_databases, teardown_test_environment,
  17. )
  18. from django.utils.datastructures import OrderedSet
  19. try:
  20. import tblib.pickling_support
  21. except ImportError:
  22. tblib = None
  23. class DebugSQLTextTestResult(unittest.TextTestResult):
  24. def __init__(self, stream, descriptions, verbosity):
  25. self.logger = logging.getLogger('django.db.backends')
  26. self.logger.setLevel(logging.DEBUG)
  27. super().__init__(stream, descriptions, verbosity)
  28. def startTest(self, test):
  29. self.debug_sql_stream = StringIO()
  30. self.handler = logging.StreamHandler(self.debug_sql_stream)
  31. self.logger.addHandler(self.handler)
  32. super().startTest(test)
  33. def stopTest(self, test):
  34. super().stopTest(test)
  35. self.logger.removeHandler(self.handler)
  36. if self.showAll:
  37. self.debug_sql_stream.seek(0)
  38. self.stream.write(self.debug_sql_stream.read())
  39. self.stream.writeln(self.separator2)
  40. def addError(self, test, err):
  41. super().addError(test, err)
  42. self.debug_sql_stream.seek(0)
  43. self.errors[-1] = self.errors[-1] + (self.debug_sql_stream.read(),)
  44. def addFailure(self, test, err):
  45. super().addFailure(test, err)
  46. self.debug_sql_stream.seek(0)
  47. self.failures[-1] = self.failures[-1] + (self.debug_sql_stream.read(),)
  48. def addSubTest(self, test, subtest, err):
  49. super().addSubTest(test, subtest, err)
  50. if err is not None:
  51. self.debug_sql_stream.seek(0)
  52. errors = self.failures if issubclass(err[0], test.failureException) else self.errors
  53. errors[-1] = errors[-1] + (self.debug_sql_stream.read(),)
  54. def printErrorList(self, flavour, errors):
  55. for test, err, sql_debug in errors:
  56. self.stream.writeln(self.separator1)
  57. self.stream.writeln("%s: %s" % (flavour, self.getDescription(test)))
  58. self.stream.writeln(self.separator2)
  59. self.stream.writeln("%s" % err)
  60. self.stream.writeln(self.separator2)
  61. self.stream.writeln("%s" % sql_debug)
  62. class RemoteTestResult:
  63. """
  64. Record information about which tests have succeeded and which have failed.
  65. The sole purpose of this class is to record events in the child processes
  66. so they can be replayed in the master process. As a consequence it doesn't
  67. inherit unittest.TestResult and doesn't attempt to implement all its API.
  68. The implementation matches the unpythonic coding style of unittest2.
  69. """
  70. def __init__(self):
  71. if tblib is not None:
  72. tblib.pickling_support.install()
  73. self.events = []
  74. self.failfast = False
  75. self.shouldStop = False
  76. self.testsRun = 0
  77. @property
  78. def test_index(self):
  79. return self.testsRun - 1
  80. def _confirm_picklable(self, obj):
  81. """
  82. Confirm that obj can be pickled and unpickled as multiprocessing will
  83. need to pickle the exception in the child process and unpickle it in
  84. the parent process. Let the exception rise, if not.
  85. """
  86. pickle.loads(pickle.dumps(obj))
  87. def _print_unpicklable_subtest(self, test, subtest, pickle_exc):
  88. print("""
  89. Subtest failed:
  90. test: {}
  91. subtest: {}
  92. Unfortunately, the subtest that failed cannot be pickled, so the parallel
  93. test runner cannot handle it cleanly. Here is the pickling error:
  94. > {}
  95. You should re-run this test with --parallel=1 to reproduce the failure
  96. with a cleaner failure message.
  97. """.format(test, subtest, pickle_exc))
  98. def check_picklable(self, test, err):
  99. # Ensure that sys.exc_info() tuples are picklable. This displays a
  100. # clear multiprocessing.pool.RemoteTraceback generated in the child
  101. # process instead of a multiprocessing.pool.MaybeEncodingError, making
  102. # the root cause easier to figure out for users who aren't familiar
  103. # with the multiprocessing module. Since we're in a forked process,
  104. # our best chance to communicate with them is to print to stdout.
  105. try:
  106. self._confirm_picklable(err)
  107. except Exception as exc:
  108. original_exc_txt = repr(err[1])
  109. original_exc_txt = textwrap.fill(original_exc_txt, 75, initial_indent=' ', subsequent_indent=' ')
  110. pickle_exc_txt = repr(exc)
  111. pickle_exc_txt = textwrap.fill(pickle_exc_txt, 75, initial_indent=' ', subsequent_indent=' ')
  112. if tblib is None:
  113. print("""
  114. {} failed:
  115. {}
  116. Unfortunately, tracebacks cannot be pickled, making it impossible for the
  117. parallel test runner to handle this exception cleanly.
  118. In order to see the traceback, you should install tblib:
  119. pip install tblib
  120. """.format(test, original_exc_txt))
  121. else:
  122. print("""
  123. {} failed:
  124. {}
  125. Unfortunately, the exception it raised cannot be pickled, making it impossible
  126. for the parallel test runner to handle it cleanly.
  127. Here's the error encountered while trying to pickle the exception:
  128. {}
  129. You should re-run this test with the --parallel=1 option to reproduce the
  130. failure and get a correct traceback.
  131. """.format(test, original_exc_txt, pickle_exc_txt))
  132. raise
  133. def check_subtest_picklable(self, test, subtest):
  134. try:
  135. self._confirm_picklable(subtest)
  136. except Exception as exc:
  137. self._print_unpicklable_subtest(test, subtest, exc)
  138. raise
  139. def stop_if_failfast(self):
  140. if self.failfast:
  141. self.stop()
  142. def stop(self):
  143. self.shouldStop = True
  144. def startTestRun(self):
  145. self.events.append(('startTestRun',))
  146. def stopTestRun(self):
  147. self.events.append(('stopTestRun',))
  148. def startTest(self, test):
  149. self.testsRun += 1
  150. self.events.append(('startTest', self.test_index))
  151. def stopTest(self, test):
  152. self.events.append(('stopTest', self.test_index))
  153. def addError(self, test, err):
  154. self.check_picklable(test, err)
  155. self.events.append(('addError', self.test_index, err))
  156. self.stop_if_failfast()
  157. def addFailure(self, test, err):
  158. self.check_picklable(test, err)
  159. self.events.append(('addFailure', self.test_index, err))
  160. self.stop_if_failfast()
  161. def addSubTest(self, test, subtest, err):
  162. # Follow Python 3.5's implementation of unittest.TestResult.addSubTest()
  163. # by not doing anything when a subtest is successful.
  164. if err is not None:
  165. # Call check_picklable() before check_subtest_picklable() since
  166. # check_picklable() performs the tblib check.
  167. self.check_picklable(test, err)
  168. self.check_subtest_picklable(test, subtest)
  169. self.events.append(('addSubTest', self.test_index, subtest, err))
  170. self.stop_if_failfast()
  171. def addSuccess(self, test):
  172. self.events.append(('addSuccess', self.test_index))
  173. def addSkip(self, test, reason):
  174. self.events.append(('addSkip', self.test_index, reason))
  175. def addExpectedFailure(self, test, err):
  176. # If tblib isn't installed, pickling the traceback will always fail.
  177. # However we don't want tblib to be required for running the tests
  178. # when they pass or fail as expected. Drop the traceback when an
  179. # expected failure occurs.
  180. if tblib is None:
  181. err = err[0], err[1], None
  182. self.check_picklable(test, err)
  183. self.events.append(('addExpectedFailure', self.test_index, err))
  184. def addUnexpectedSuccess(self, test):
  185. self.events.append(('addUnexpectedSuccess', self.test_index))
  186. self.stop_if_failfast()
  187. class RemoteTestRunner:
  188. """
  189. Run tests and record everything but don't display anything.
  190. The implementation matches the unpythonic coding style of unittest2.
  191. """
  192. resultclass = RemoteTestResult
  193. def __init__(self, failfast=False, resultclass=None):
  194. self.failfast = failfast
  195. if resultclass is not None:
  196. self.resultclass = resultclass
  197. def run(self, test):
  198. result = self.resultclass()
  199. unittest.registerResult(result)
  200. result.failfast = self.failfast
  201. test(result)
  202. return result
  203. def default_test_processes():
  204. """Default number of test processes when using the --parallel option."""
  205. # The current implementation of the parallel test runner requires
  206. # multiprocessing to start subprocesses with fork().
  207. if multiprocessing.get_start_method() != 'fork':
  208. return 1
  209. try:
  210. return int(os.environ['DJANGO_TEST_PROCESSES'])
  211. except KeyError:
  212. return multiprocessing.cpu_count()
  213. _worker_id = 0
  214. def _init_worker(counter):
  215. """
  216. Switch to databases dedicated to this worker.
  217. This helper lives at module-level because of the multiprocessing module's
  218. requirements.
  219. """
  220. global _worker_id
  221. with counter.get_lock():
  222. counter.value += 1
  223. _worker_id = counter.value
  224. for alias in connections:
  225. connection = connections[alias]
  226. settings_dict = connection.creation.get_test_db_clone_settings(str(_worker_id))
  227. # connection.settings_dict must be updated in place for changes to be
  228. # reflected in django.db.connections. If the following line assigned
  229. # connection.settings_dict = settings_dict, new threads would connect
  230. # to the default database instead of the appropriate clone.
  231. connection.settings_dict.update(settings_dict)
  232. connection.close()
  233. def _run_subsuite(args):
  234. """
  235. Run a suite of tests with a RemoteTestRunner and return a RemoteTestResult.
  236. This helper lives at module-level and its arguments are wrapped in a tuple
  237. because of the multiprocessing module's requirements.
  238. """
  239. runner_class, subsuite_index, subsuite, failfast = args
  240. runner = runner_class(failfast=failfast)
  241. result = runner.run(subsuite)
  242. return subsuite_index, result.events
  243. class ParallelTestSuite(unittest.TestSuite):
  244. """
  245. Run a series of tests in parallel in several processes.
  246. While the unittest module's documentation implies that orchestrating the
  247. execution of tests is the responsibility of the test runner, in practice,
  248. it appears that TestRunner classes are more concerned with formatting and
  249. displaying test results.
  250. Since there are fewer use cases for customizing TestSuite than TestRunner,
  251. implementing parallelization at the level of the TestSuite improves
  252. interoperability with existing custom test runners. A single instance of a
  253. test runner can still collect results from all tests without being aware
  254. that they have been run in parallel.
  255. """
  256. # In case someone wants to modify these in a subclass.
  257. init_worker = _init_worker
  258. run_subsuite = _run_subsuite
  259. runner_class = RemoteTestRunner
  260. def __init__(self, suite, processes, failfast=False):
  261. self.subsuites = partition_suite_by_case(suite)
  262. self.processes = processes
  263. self.failfast = failfast
  264. super().__init__()
  265. def run(self, result):
  266. """
  267. Distribute test cases across workers.
  268. Return an identifier of each test case with its result in order to use
  269. imap_unordered to show results as soon as they're available.
  270. To minimize pickling errors when getting results from workers:
  271. - pass back numeric indexes in self.subsuites instead of tests
  272. - make tracebacks picklable with tblib, if available
  273. Even with tblib, errors may still occur for dynamically created
  274. exception classes which cannot be unpickled.
  275. """
  276. counter = multiprocessing.Value(ctypes.c_int, 0)
  277. pool = multiprocessing.Pool(
  278. processes=self.processes,
  279. initializer=self.init_worker.__func__,
  280. initargs=[counter],
  281. )
  282. args = [
  283. (self.runner_class, index, subsuite, self.failfast)
  284. for index, subsuite in enumerate(self.subsuites)
  285. ]
  286. test_results = pool.imap_unordered(self.run_subsuite.__func__, args)
  287. while True:
  288. if result.shouldStop:
  289. pool.terminate()
  290. break
  291. try:
  292. subsuite_index, events = test_results.next(timeout=0.1)
  293. except multiprocessing.TimeoutError:
  294. continue
  295. except StopIteration:
  296. pool.close()
  297. break
  298. tests = list(self.subsuites[subsuite_index])
  299. for event in events:
  300. event_name = event[0]
  301. handler = getattr(result, event_name, None)
  302. if handler is None:
  303. continue
  304. test = tests[event[1]]
  305. args = event[2:]
  306. handler(test, *args)
  307. pool.join()
  308. return result
  309. class DiscoverRunner:
  310. """A Django test runner that uses unittest2 test discovery."""
  311. test_suite = unittest.TestSuite
  312. parallel_test_suite = ParallelTestSuite
  313. test_runner = unittest.TextTestRunner
  314. test_loader = unittest.defaultTestLoader
  315. reorder_by = (TestCase, SimpleTestCase)
  316. def __init__(self, pattern=None, top_level=None, verbosity=1,
  317. interactive=True, failfast=False, keepdb=False,
  318. reverse=False, debug_mode=False, debug_sql=False, parallel=0,
  319. tags=None, exclude_tags=None, **kwargs):
  320. self.pattern = pattern
  321. self.top_level = top_level
  322. self.verbosity = verbosity
  323. self.interactive = interactive
  324. self.failfast = failfast
  325. self.keepdb = keepdb
  326. self.reverse = reverse
  327. self.debug_mode = debug_mode
  328. self.debug_sql = debug_sql
  329. self.parallel = parallel
  330. self.tags = set(tags or [])
  331. self.exclude_tags = set(exclude_tags or [])
  332. @classmethod
  333. def add_arguments(cls, parser):
  334. parser.add_argument(
  335. '-t', '--top-level-directory', action='store', dest='top_level', default=None,
  336. help='Top level of project for unittest discovery.',
  337. )
  338. parser.add_argument(
  339. '-p', '--pattern', action='store', dest='pattern', default="test*.py",
  340. help='The test matching pattern. Defaults to test*.py.',
  341. )
  342. parser.add_argument(
  343. '-k', '--keepdb', action='store_true', dest='keepdb',
  344. help='Preserves the test DB between runs.'
  345. )
  346. parser.add_argument(
  347. '-r', '--reverse', action='store_true', dest='reverse',
  348. help='Reverses test cases order.',
  349. )
  350. parser.add_argument(
  351. '--debug-mode', action='store_true', dest='debug_mode',
  352. help='Sets settings.DEBUG to True.',
  353. )
  354. parser.add_argument(
  355. '-d', '--debug-sql', action='store_true', dest='debug_sql',
  356. help='Prints logged SQL queries on failure.',
  357. )
  358. parser.add_argument(
  359. '--parallel', dest='parallel', nargs='?', default=1, type=int,
  360. const=default_test_processes(), metavar='N',
  361. help='Run tests using up to N parallel processes.',
  362. )
  363. parser.add_argument(
  364. '--tag', action='append', dest='tags',
  365. help='Run only tests with the specified tag. Can be used multiple times.',
  366. )
  367. parser.add_argument(
  368. '--exclude-tag', action='append', dest='exclude_tags',
  369. help='Do not run tests with the specified tag. Can be used multiple times.',
  370. )
  371. def setup_test_environment(self, **kwargs):
  372. setup_test_environment(debug=self.debug_mode)
  373. unittest.installHandler()
  374. def build_suite(self, test_labels=None, extra_tests=None, **kwargs):
  375. suite = self.test_suite()
  376. test_labels = test_labels or ['.']
  377. extra_tests = extra_tests or []
  378. discover_kwargs = {}
  379. if self.pattern is not None:
  380. discover_kwargs['pattern'] = self.pattern
  381. if self.top_level is not None:
  382. discover_kwargs['top_level_dir'] = self.top_level
  383. for label in test_labels:
  384. kwargs = discover_kwargs.copy()
  385. tests = None
  386. label_as_path = os.path.abspath(label)
  387. # if a module, or "module.ClassName[.method_name]", just run those
  388. if not os.path.exists(label_as_path):
  389. tests = self.test_loader.loadTestsFromName(label)
  390. elif os.path.isdir(label_as_path) and not self.top_level:
  391. # Try to be a bit smarter than unittest about finding the
  392. # default top-level for a given directory path, to avoid
  393. # breaking relative imports. (Unittest's default is to set
  394. # top-level equal to the path, which means relative imports
  395. # will result in "Attempted relative import in non-package.").
  396. # We'd be happy to skip this and require dotted module paths
  397. # (which don't cause this problem) instead of file paths (which
  398. # do), but in the case of a directory in the cwd, which would
  399. # be equally valid if considered as a top-level module or as a
  400. # directory path, unittest unfortunately prefers the latter.
  401. top_level = label_as_path
  402. while True:
  403. init_py = os.path.join(top_level, '__init__.py')
  404. if os.path.exists(init_py):
  405. try_next = os.path.dirname(top_level)
  406. if try_next == top_level:
  407. # __init__.py all the way down? give up.
  408. break
  409. top_level = try_next
  410. continue
  411. break
  412. kwargs['top_level_dir'] = top_level
  413. if not (tests and tests.countTestCases()) and is_discoverable(label):
  414. # Try discovery if path is a package or directory
  415. tests = self.test_loader.discover(start_dir=label, **kwargs)
  416. # Make unittest forget the top-level dir it calculated from this
  417. # run, to support running tests from two different top-levels.
  418. self.test_loader._top_level_dir = None
  419. suite.addTests(tests)
  420. for test in extra_tests:
  421. suite.addTest(test)
  422. if self.tags or self.exclude_tags:
  423. if self.verbosity >= 2:
  424. if self.tags:
  425. print('Including test tag(s): %s.' % ', '.join(sorted(self.tags)))
  426. if self.exclude_tags:
  427. print('Excluding test tag(s): %s.' % ', '.join(sorted(self.exclude_tags)))
  428. suite = filter_tests_by_tags(suite, self.tags, self.exclude_tags)
  429. suite = reorder_suite(suite, self.reorder_by, self.reverse)
  430. if self.parallel > 1:
  431. parallel_suite = self.parallel_test_suite(suite, self.parallel, self.failfast)
  432. # Since tests are distributed across processes on a per-TestCase
  433. # basis, there's no need for more processes than TestCases.
  434. parallel_units = len(parallel_suite.subsuites)
  435. self.parallel = min(self.parallel, parallel_units)
  436. # If there's only one TestCase, parallelization isn't needed.
  437. if self.parallel > 1:
  438. suite = parallel_suite
  439. return suite
  440. def setup_databases(self, **kwargs):
  441. return _setup_databases(
  442. self.verbosity, self.interactive, self.keepdb, self.debug_sql,
  443. self.parallel, **kwargs
  444. )
  445. def get_resultclass(self):
  446. return DebugSQLTextTestResult if self.debug_sql else None
  447. def get_test_runner_kwargs(self):
  448. return {
  449. 'failfast': self.failfast,
  450. 'resultclass': self.get_resultclass(),
  451. 'verbosity': self.verbosity,
  452. }
  453. def run_checks(self):
  454. # Checks are run after database creation since some checks require
  455. # database access.
  456. call_command('check', verbosity=self.verbosity)
  457. def run_suite(self, suite, **kwargs):
  458. kwargs = self.get_test_runner_kwargs()
  459. runner = self.test_runner(**kwargs)
  460. return runner.run(suite)
  461. def teardown_databases(self, old_config, **kwargs):
  462. """Destroy all the non-mirror databases."""
  463. _teardown_databases(
  464. old_config,
  465. verbosity=self.verbosity,
  466. parallel=self.parallel,
  467. keepdb=self.keepdb,
  468. )
  469. def teardown_test_environment(self, **kwargs):
  470. unittest.removeHandler()
  471. teardown_test_environment()
  472. def suite_result(self, suite, result, **kwargs):
  473. return len(result.failures) + len(result.errors)
  474. def run_tests(self, test_labels, extra_tests=None, **kwargs):
  475. """
  476. Run the unit tests for all the test labels in the provided list.
  477. Test labels should be dotted Python paths to test modules, test
  478. classes, or test methods.
  479. A list of 'extra' tests may also be provided; these tests
  480. will be added to the test suite.
  481. Return the number of tests that failed.
  482. """
  483. self.setup_test_environment()
  484. suite = self.build_suite(test_labels, extra_tests)
  485. old_config = self.setup_databases()
  486. self.run_checks()
  487. result = self.run_suite(suite)
  488. self.teardown_databases(old_config)
  489. self.teardown_test_environment()
  490. return self.suite_result(suite, result)
  491. def is_discoverable(label):
  492. """
  493. Check if a test label points to a python package or file directory.
  494. Relative labels like "." and ".." are seen as directories.
  495. """
  496. try:
  497. mod = import_module(label)
  498. except (ImportError, TypeError):
  499. pass
  500. else:
  501. return hasattr(mod, '__path__')
  502. return os.path.isdir(os.path.abspath(label))
  503. def reorder_suite(suite, classes, reverse=False):
  504. """
  505. Reorder a test suite by test type.
  506. `classes` is a sequence of types
  507. All tests of type classes[0] are placed first, then tests of type
  508. classes[1], etc. Tests with no match in classes are placed last.
  509. If `reverse` is True, sort tests within classes in opposite order but
  510. don't reverse test classes.
  511. """
  512. class_count = len(classes)
  513. suite_class = type(suite)
  514. bins = [OrderedSet() for i in range(class_count + 1)]
  515. partition_suite_by_type(suite, classes, bins, reverse=reverse)
  516. reordered_suite = suite_class()
  517. for i in range(class_count + 1):
  518. reordered_suite.addTests(bins[i])
  519. return reordered_suite
  520. def partition_suite_by_type(suite, classes, bins, reverse=False):
  521. """
  522. Partition a test suite by test type. Also prevent duplicated tests.
  523. classes is a sequence of types
  524. bins is a sequence of TestSuites, one more than classes
  525. reverse changes the ordering of tests within bins
  526. Tests of type classes[i] are added to bins[i],
  527. tests with no match found in classes are place in bins[-1]
  528. """
  529. suite_class = type(suite)
  530. if reverse:
  531. suite = reversed(tuple(suite))
  532. for test in suite:
  533. if isinstance(test, suite_class):
  534. partition_suite_by_type(test, classes, bins, reverse=reverse)
  535. else:
  536. for i in range(len(classes)):
  537. if isinstance(test, classes[i]):
  538. bins[i].add(test)
  539. break
  540. else:
  541. bins[-1].add(test)
  542. def partition_suite_by_case(suite):
  543. """Partition a test suite by test case, preserving the order of tests."""
  544. groups = []
  545. suite_class = type(suite)
  546. for test_type, test_group in itertools.groupby(suite, type):
  547. if issubclass(test_type, unittest.TestCase):
  548. groups.append(suite_class(test_group))
  549. else:
  550. for item in test_group:
  551. groups.extend(partition_suite_by_case(item))
  552. return groups
  553. def filter_tests_by_tags(suite, tags, exclude_tags):
  554. suite_class = type(suite)
  555. filtered_suite = suite_class()
  556. for test in suite:
  557. if isinstance(test, suite_class):
  558. filtered_suite.addTests(filter_tests_by_tags(test, tags, exclude_tags))
  559. else:
  560. test_tags = set(getattr(test, 'tags', set()))
  561. test_fn_name = getattr(test, '_testMethodName', str(test))
  562. test_fn = getattr(test, test_fn_name, test)
  563. test_fn_tags = set(getattr(test_fn, 'tags', set()))
  564. all_tags = test_tags.union(test_fn_tags)
  565. matched_tags = all_tags.intersection(tags)
  566. if (matched_tags or not tags) and not all_tags.intersection(exclude_tags):
  567. filtered_suite.addTest(test)
  568. return filtered_suite