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 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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(err)
  60. self.stream.writeln(self.separator2)
  61. self.stream.writeln(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. def __iter__(self):
  310. return iter(self.subsuites)
  311. class DiscoverRunner:
  312. """A Django test runner that uses unittest2 test discovery."""
  313. test_suite = unittest.TestSuite
  314. parallel_test_suite = ParallelTestSuite
  315. test_runner = unittest.TextTestRunner
  316. test_loader = unittest.defaultTestLoader
  317. reorder_by = (TestCase, SimpleTestCase)
  318. def __init__(self, pattern=None, top_level=None, verbosity=1,
  319. interactive=True, failfast=False, keepdb=False,
  320. reverse=False, debug_mode=False, debug_sql=False, parallel=0,
  321. tags=None, exclude_tags=None, **kwargs):
  322. self.pattern = pattern
  323. self.top_level = top_level
  324. self.verbosity = verbosity
  325. self.interactive = interactive
  326. self.failfast = failfast
  327. self.keepdb = keepdb
  328. self.reverse = reverse
  329. self.debug_mode = debug_mode
  330. self.debug_sql = debug_sql
  331. self.parallel = parallel
  332. self.tags = set(tags or [])
  333. self.exclude_tags = set(exclude_tags or [])
  334. @classmethod
  335. def add_arguments(cls, parser):
  336. parser.add_argument(
  337. '-t', '--top-level-directory', dest='top_level',
  338. help='Top level of project for unittest discovery.',
  339. )
  340. parser.add_argument(
  341. '-p', '--pattern', default="test*.py",
  342. help='The test matching pattern. Defaults to test*.py.',
  343. )
  344. parser.add_argument(
  345. '-k', '--keepdb', action='store_true',
  346. help='Preserves the test DB between runs.'
  347. )
  348. parser.add_argument(
  349. '-r', '--reverse', action='store_true',
  350. help='Reverses test cases order.',
  351. )
  352. parser.add_argument(
  353. '--debug-mode', action='store_true',
  354. help='Sets settings.DEBUG to True.',
  355. )
  356. parser.add_argument(
  357. '-d', '--debug-sql', action='store_true',
  358. help='Prints logged SQL queries on failure.',
  359. )
  360. parser.add_argument(
  361. '--parallel', nargs='?', default=1, type=int,
  362. const=default_test_processes(), metavar='N',
  363. help='Run tests using up to N parallel processes.',
  364. )
  365. parser.add_argument(
  366. '--tag', action='append', dest='tags',
  367. help='Run only tests with the specified tag. Can be used multiple times.',
  368. )
  369. parser.add_argument(
  370. '--exclude-tag', action='append', dest='exclude_tags',
  371. help='Do not run tests with the specified tag. Can be used multiple times.',
  372. )
  373. def setup_test_environment(self, **kwargs):
  374. setup_test_environment(debug=self.debug_mode)
  375. unittest.installHandler()
  376. def build_suite(self, test_labels=None, extra_tests=None, **kwargs):
  377. suite = self.test_suite()
  378. test_labels = test_labels or ['.']
  379. extra_tests = extra_tests or []
  380. discover_kwargs = {}
  381. if self.pattern is not None:
  382. discover_kwargs['pattern'] = self.pattern
  383. if self.top_level is not None:
  384. discover_kwargs['top_level_dir'] = self.top_level
  385. for label in test_labels:
  386. kwargs = discover_kwargs.copy()
  387. tests = None
  388. label_as_path = os.path.abspath(label)
  389. # if a module, or "module.ClassName[.method_name]", just run those
  390. if not os.path.exists(label_as_path):
  391. tests = self.test_loader.loadTestsFromName(label)
  392. elif os.path.isdir(label_as_path) and not self.top_level:
  393. # Try to be a bit smarter than unittest about finding the
  394. # default top-level for a given directory path, to avoid
  395. # breaking relative imports. (Unittest's default is to set
  396. # top-level equal to the path, which means relative imports
  397. # will result in "Attempted relative import in non-package.").
  398. # We'd be happy to skip this and require dotted module paths
  399. # (which don't cause this problem) instead of file paths (which
  400. # do), but in the case of a directory in the cwd, which would
  401. # be equally valid if considered as a top-level module or as a
  402. # directory path, unittest unfortunately prefers the latter.
  403. top_level = label_as_path
  404. while True:
  405. init_py = os.path.join(top_level, '__init__.py')
  406. if os.path.exists(init_py):
  407. try_next = os.path.dirname(top_level)
  408. if try_next == top_level:
  409. # __init__.py all the way down? give up.
  410. break
  411. top_level = try_next
  412. continue
  413. break
  414. kwargs['top_level_dir'] = top_level
  415. if not (tests and tests.countTestCases()) and is_discoverable(label):
  416. # Try discovery if path is a package or directory
  417. tests = self.test_loader.discover(start_dir=label, **kwargs)
  418. # Make unittest forget the top-level dir it calculated from this
  419. # run, to support running tests from two different top-levels.
  420. self.test_loader._top_level_dir = None
  421. suite.addTests(tests)
  422. for test in extra_tests:
  423. suite.addTest(test)
  424. if self.tags or self.exclude_tags:
  425. if self.verbosity >= 2:
  426. if self.tags:
  427. print('Including test tag(s): %s.' % ', '.join(sorted(self.tags)))
  428. if self.exclude_tags:
  429. print('Excluding test tag(s): %s.' % ', '.join(sorted(self.exclude_tags)))
  430. suite = filter_tests_by_tags(suite, self.tags, self.exclude_tags)
  431. suite = reorder_suite(suite, self.reorder_by, self.reverse)
  432. if self.parallel > 1:
  433. parallel_suite = self.parallel_test_suite(suite, self.parallel, self.failfast)
  434. # Since tests are distributed across processes on a per-TestCase
  435. # basis, there's no need for more processes than TestCases.
  436. parallel_units = len(parallel_suite.subsuites)
  437. self.parallel = min(self.parallel, parallel_units)
  438. # If there's only one TestCase, parallelization isn't needed.
  439. if self.parallel > 1:
  440. suite = parallel_suite
  441. return suite
  442. def setup_databases(self, **kwargs):
  443. return _setup_databases(
  444. self.verbosity, self.interactive, self.keepdb, self.debug_sql,
  445. self.parallel, **kwargs
  446. )
  447. def get_resultclass(self):
  448. return DebugSQLTextTestResult if self.debug_sql else None
  449. def get_test_runner_kwargs(self):
  450. return {
  451. 'failfast': self.failfast,
  452. 'resultclass': self.get_resultclass(),
  453. 'verbosity': self.verbosity,
  454. }
  455. def run_checks(self):
  456. # Checks are run after database creation since some checks require
  457. # database access.
  458. call_command('check', verbosity=self.verbosity)
  459. def run_suite(self, suite, **kwargs):
  460. kwargs = self.get_test_runner_kwargs()
  461. runner = self.test_runner(**kwargs)
  462. return runner.run(suite)
  463. def teardown_databases(self, old_config, **kwargs):
  464. """Destroy all the non-mirror databases."""
  465. _teardown_databases(
  466. old_config,
  467. verbosity=self.verbosity,
  468. parallel=self.parallel,
  469. keepdb=self.keepdb,
  470. )
  471. def teardown_test_environment(self, **kwargs):
  472. unittest.removeHandler()
  473. teardown_test_environment()
  474. def suite_result(self, suite, result, **kwargs):
  475. return len(result.failures) + len(result.errors)
  476. def _get_databases(self, suite):
  477. databases = set()
  478. for test in suite:
  479. if isinstance(test, unittest.TestCase):
  480. test_databases = getattr(test, 'databases', None)
  481. if test_databases == '__all__':
  482. return set(connections)
  483. if test_databases:
  484. databases.update(test_databases)
  485. else:
  486. databases.update(self._get_databases(test))
  487. return databases
  488. def get_databases(self, suite):
  489. databases = self._get_databases(suite)
  490. if self.verbosity >= 2:
  491. unused_databases = [alias for alias in connections if alias not in databases]
  492. if unused_databases:
  493. print('Skipping setup of unused database(s): %s.' % ', '.join(sorted(unused_databases)))
  494. return databases
  495. def run_tests(self, test_labels, extra_tests=None, **kwargs):
  496. """
  497. Run the unit tests for all the test labels in the provided list.
  498. Test labels should be dotted Python paths to test modules, test
  499. classes, or test methods.
  500. A list of 'extra' tests may also be provided; these tests
  501. will be added to the test suite.
  502. Return the number of tests that failed.
  503. """
  504. self.setup_test_environment()
  505. suite = self.build_suite(test_labels, extra_tests)
  506. databases = self.get_databases(suite)
  507. old_config = self.setup_databases(aliases=databases)
  508. run_failed = False
  509. try:
  510. self.run_checks()
  511. result = self.run_suite(suite)
  512. except Exception:
  513. run_failed = True
  514. raise
  515. finally:
  516. try:
  517. self.teardown_databases(old_config)
  518. self.teardown_test_environment()
  519. except Exception:
  520. # Silence teardown exceptions if an exception was raised during
  521. # runs to avoid shadowing it.
  522. if not run_failed:
  523. raise
  524. return self.suite_result(suite, result)
  525. def is_discoverable(label):
  526. """
  527. Check if a test label points to a Python package or file directory.
  528. Relative labels like "." and ".." are seen as directories.
  529. """
  530. try:
  531. mod = import_module(label)
  532. except (ImportError, TypeError):
  533. pass
  534. else:
  535. return hasattr(mod, '__path__')
  536. return os.path.isdir(os.path.abspath(label))
  537. def reorder_suite(suite, classes, reverse=False):
  538. """
  539. Reorder a test suite by test type.
  540. `classes` is a sequence of types
  541. All tests of type classes[0] are placed first, then tests of type
  542. classes[1], etc. Tests with no match in classes are placed last.
  543. If `reverse` is True, sort tests within classes in opposite order but
  544. don't reverse test classes.
  545. """
  546. class_count = len(classes)
  547. suite_class = type(suite)
  548. bins = [OrderedSet() for i in range(class_count + 1)]
  549. partition_suite_by_type(suite, classes, bins, reverse=reverse)
  550. reordered_suite = suite_class()
  551. for i in range(class_count + 1):
  552. reordered_suite.addTests(bins[i])
  553. return reordered_suite
  554. def partition_suite_by_type(suite, classes, bins, reverse=False):
  555. """
  556. Partition a test suite by test type. Also prevent duplicated tests.
  557. classes is a sequence of types
  558. bins is a sequence of TestSuites, one more than classes
  559. reverse changes the ordering of tests within bins
  560. Tests of type classes[i] are added to bins[i],
  561. tests with no match found in classes are place in bins[-1]
  562. """
  563. suite_class = type(suite)
  564. if reverse:
  565. suite = reversed(tuple(suite))
  566. for test in suite:
  567. if isinstance(test, suite_class):
  568. partition_suite_by_type(test, classes, bins, reverse=reverse)
  569. else:
  570. for i in range(len(classes)):
  571. if isinstance(test, classes[i]):
  572. bins[i].add(test)
  573. break
  574. else:
  575. bins[-1].add(test)
  576. def partition_suite_by_case(suite):
  577. """Partition a test suite by test case, preserving the order of tests."""
  578. groups = []
  579. suite_class = type(suite)
  580. for test_type, test_group in itertools.groupby(suite, type):
  581. if issubclass(test_type, unittest.TestCase):
  582. groups.append(suite_class(test_group))
  583. else:
  584. for item in test_group:
  585. groups.extend(partition_suite_by_case(item))
  586. return groups
  587. def filter_tests_by_tags(suite, tags, exclude_tags):
  588. suite_class = type(suite)
  589. filtered_suite = suite_class()
  590. for test in suite:
  591. if isinstance(test, suite_class):
  592. filtered_suite.addTests(filter_tests_by_tags(test, tags, exclude_tags))
  593. else:
  594. test_tags = set(getattr(test, 'tags', set()))
  595. test_fn_name = getattr(test, '_testMethodName', str(test))
  596. test_fn = getattr(test, test_fn_name, test)
  597. test_fn_tags = set(getattr(test_fn, 'tags', set()))
  598. all_tags = test_tags.union(test_fn_tags)
  599. matched_tags = all_tags.intersection(tags)
  600. if (matched_tags or not tags) and not all_tags.intersection(exclude_tags):
  601. filtered_suite.addTest(test)
  602. return filtered_suite