runner.py 29 KB

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