tests.py 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. """
  2. Tests for django test runner
  3. """
  4. import collections.abc
  5. import multiprocessing
  6. import os
  7. import sys
  8. import unittest
  9. from unittest import mock
  10. from admin_scripts.tests import AdminScriptTestCase
  11. from django import db
  12. from django.conf import settings
  13. from django.core.exceptions import ImproperlyConfigured
  14. from django.core.management import call_command
  15. from django.core.management.base import SystemCheckError
  16. from django.test import SimpleTestCase, TransactionTestCase, skipUnlessDBFeature
  17. from django.test.runner import (
  18. DiscoverRunner,
  19. Shuffler,
  20. _init_worker,
  21. reorder_test_bin,
  22. reorder_tests,
  23. shuffle_tests,
  24. )
  25. from django.test.testcases import connections_support_transactions
  26. from django.test.utils import (
  27. captured_stderr,
  28. dependency_ordered,
  29. get_unique_databases_and_mirrors,
  30. iter_test_cases,
  31. )
  32. from django.utils.deprecation import RemovedInDjango50Warning
  33. from .models import B, Person, Through
  34. class MySuite:
  35. def __init__(self):
  36. self.tests = []
  37. def addTest(self, test):
  38. self.tests.append(test)
  39. def __iter__(self):
  40. yield from self.tests
  41. class TestSuiteTests(SimpleTestCase):
  42. def build_test_suite(self, test_classes, suite=None, suite_class=None):
  43. if suite_class is None:
  44. suite_class = unittest.TestSuite
  45. if suite is None:
  46. suite = suite_class()
  47. loader = unittest.defaultTestLoader
  48. for test_class in test_classes:
  49. tests = loader.loadTestsFromTestCase(test_class)
  50. subsuite = suite_class()
  51. # Only use addTest() to simplify testing a custom TestSuite.
  52. for test in tests:
  53. subsuite.addTest(test)
  54. suite.addTest(subsuite)
  55. return suite
  56. def make_test_suite(self, suite=None, suite_class=None):
  57. class Tests1(unittest.TestCase):
  58. def test1(self):
  59. pass
  60. def test2(self):
  61. pass
  62. class Tests2(unittest.TestCase):
  63. def test1(self):
  64. pass
  65. def test2(self):
  66. pass
  67. return self.build_test_suite(
  68. (Tests1, Tests2),
  69. suite=suite,
  70. suite_class=suite_class,
  71. )
  72. def assertTestNames(self, tests, expected):
  73. # Each test.id() has a form like the following:
  74. # "test_runner.tests.IterTestCasesTests.test_iter_test_cases.<locals>.Tests1.test1".
  75. # It suffices to check only the last two parts.
  76. names = [".".join(test.id().split(".")[-2:]) for test in tests]
  77. self.assertEqual(names, expected)
  78. def test_iter_test_cases_basic(self):
  79. suite = self.make_test_suite()
  80. tests = iter_test_cases(suite)
  81. self.assertTestNames(
  82. tests,
  83. expected=[
  84. "Tests1.test1",
  85. "Tests1.test2",
  86. "Tests2.test1",
  87. "Tests2.test2",
  88. ],
  89. )
  90. def test_iter_test_cases_string_input(self):
  91. msg = (
  92. "Test 'a' must be a test case or test suite not string (was found "
  93. "in 'abc')."
  94. )
  95. with self.assertRaisesMessage(TypeError, msg):
  96. list(iter_test_cases("abc"))
  97. def test_iter_test_cases_iterable_of_tests(self):
  98. class Tests(unittest.TestCase):
  99. def test1(self):
  100. pass
  101. def test2(self):
  102. pass
  103. tests = list(unittest.defaultTestLoader.loadTestsFromTestCase(Tests))
  104. actual_tests = iter_test_cases(tests)
  105. self.assertTestNames(
  106. actual_tests,
  107. expected=[
  108. "Tests.test1",
  109. "Tests.test2",
  110. ],
  111. )
  112. def test_iter_test_cases_custom_test_suite_class(self):
  113. suite = self.make_test_suite(suite_class=MySuite)
  114. tests = iter_test_cases(suite)
  115. self.assertTestNames(
  116. tests,
  117. expected=[
  118. "Tests1.test1",
  119. "Tests1.test2",
  120. "Tests2.test1",
  121. "Tests2.test2",
  122. ],
  123. )
  124. def test_iter_test_cases_mixed_test_suite_classes(self):
  125. suite = self.make_test_suite(suite=MySuite())
  126. child_suite = list(suite)[0]
  127. self.assertNotIsInstance(child_suite, MySuite)
  128. tests = list(iter_test_cases(suite))
  129. self.assertEqual(len(tests), 4)
  130. self.assertNotIsInstance(tests[0], unittest.TestSuite)
  131. def make_tests(self):
  132. """Return an iterable of tests."""
  133. suite = self.make_test_suite()
  134. return list(iter_test_cases(suite))
  135. def test_shuffle_tests(self):
  136. tests = self.make_tests()
  137. # Choose a seed that shuffles both the classes and methods.
  138. shuffler = Shuffler(seed=9)
  139. shuffled_tests = shuffle_tests(tests, shuffler)
  140. self.assertIsInstance(shuffled_tests, collections.abc.Iterator)
  141. self.assertTestNames(
  142. shuffled_tests,
  143. expected=[
  144. "Tests2.test1",
  145. "Tests2.test2",
  146. "Tests1.test2",
  147. "Tests1.test1",
  148. ],
  149. )
  150. def test_reorder_test_bin_no_arguments(self):
  151. tests = self.make_tests()
  152. reordered_tests = reorder_test_bin(tests)
  153. self.assertIsInstance(reordered_tests, collections.abc.Iterator)
  154. self.assertTestNames(
  155. reordered_tests,
  156. expected=[
  157. "Tests1.test1",
  158. "Tests1.test2",
  159. "Tests2.test1",
  160. "Tests2.test2",
  161. ],
  162. )
  163. def test_reorder_test_bin_reverse(self):
  164. tests = self.make_tests()
  165. reordered_tests = reorder_test_bin(tests, reverse=True)
  166. self.assertIsInstance(reordered_tests, collections.abc.Iterator)
  167. self.assertTestNames(
  168. reordered_tests,
  169. expected=[
  170. "Tests2.test2",
  171. "Tests2.test1",
  172. "Tests1.test2",
  173. "Tests1.test1",
  174. ],
  175. )
  176. def test_reorder_test_bin_random(self):
  177. tests = self.make_tests()
  178. # Choose a seed that shuffles both the classes and methods.
  179. shuffler = Shuffler(seed=9)
  180. reordered_tests = reorder_test_bin(tests, shuffler=shuffler)
  181. self.assertIsInstance(reordered_tests, collections.abc.Iterator)
  182. self.assertTestNames(
  183. reordered_tests,
  184. expected=[
  185. "Tests2.test1",
  186. "Tests2.test2",
  187. "Tests1.test2",
  188. "Tests1.test1",
  189. ],
  190. )
  191. def test_reorder_test_bin_random_and_reverse(self):
  192. tests = self.make_tests()
  193. # Choose a seed that shuffles both the classes and methods.
  194. shuffler = Shuffler(seed=9)
  195. reordered_tests = reorder_test_bin(tests, shuffler=shuffler, reverse=True)
  196. self.assertIsInstance(reordered_tests, collections.abc.Iterator)
  197. self.assertTestNames(
  198. reordered_tests,
  199. expected=[
  200. "Tests1.test1",
  201. "Tests1.test2",
  202. "Tests2.test2",
  203. "Tests2.test1",
  204. ],
  205. )
  206. def test_reorder_tests_same_type_consecutive(self):
  207. """Tests of the same type are made consecutive."""
  208. tests = self.make_tests()
  209. # Move the last item to the front.
  210. tests.insert(0, tests.pop())
  211. self.assertTestNames(
  212. tests,
  213. expected=[
  214. "Tests2.test2",
  215. "Tests1.test1",
  216. "Tests1.test2",
  217. "Tests2.test1",
  218. ],
  219. )
  220. reordered_tests = reorder_tests(tests, classes=[])
  221. self.assertTestNames(
  222. reordered_tests,
  223. expected=[
  224. "Tests2.test2",
  225. "Tests2.test1",
  226. "Tests1.test1",
  227. "Tests1.test2",
  228. ],
  229. )
  230. def test_reorder_tests_random(self):
  231. tests = self.make_tests()
  232. # Choose a seed that shuffles both the classes and methods.
  233. shuffler = Shuffler(seed=9)
  234. reordered_tests = reorder_tests(tests, classes=[], shuffler=shuffler)
  235. self.assertIsInstance(reordered_tests, collections.abc.Iterator)
  236. self.assertTestNames(
  237. reordered_tests,
  238. expected=[
  239. "Tests2.test1",
  240. "Tests2.test2",
  241. "Tests1.test2",
  242. "Tests1.test1",
  243. ],
  244. )
  245. def test_reorder_tests_random_mixed_classes(self):
  246. tests = self.make_tests()
  247. # Move the last item to the front.
  248. tests.insert(0, tests.pop())
  249. shuffler = Shuffler(seed=9)
  250. self.assertTestNames(
  251. tests,
  252. expected=[
  253. "Tests2.test2",
  254. "Tests1.test1",
  255. "Tests1.test2",
  256. "Tests2.test1",
  257. ],
  258. )
  259. reordered_tests = reorder_tests(tests, classes=[], shuffler=shuffler)
  260. self.assertTestNames(
  261. reordered_tests,
  262. expected=[
  263. "Tests2.test1",
  264. "Tests2.test2",
  265. "Tests1.test2",
  266. "Tests1.test1",
  267. ],
  268. )
  269. def test_reorder_tests_reverse_with_duplicates(self):
  270. class Tests1(unittest.TestCase):
  271. def test1(self):
  272. pass
  273. class Tests2(unittest.TestCase):
  274. def test2(self):
  275. pass
  276. def test3(self):
  277. pass
  278. suite = self.build_test_suite((Tests1, Tests2))
  279. subsuite = list(suite)[0]
  280. suite.addTest(subsuite)
  281. tests = list(iter_test_cases(suite))
  282. self.assertTestNames(
  283. tests,
  284. expected=[
  285. "Tests1.test1",
  286. "Tests2.test2",
  287. "Tests2.test3",
  288. "Tests1.test1",
  289. ],
  290. )
  291. reordered_tests = reorder_tests(tests, classes=[])
  292. self.assertTestNames(
  293. reordered_tests,
  294. expected=[
  295. "Tests1.test1",
  296. "Tests2.test2",
  297. "Tests2.test3",
  298. ],
  299. )
  300. reordered_tests = reorder_tests(tests, classes=[], reverse=True)
  301. self.assertTestNames(
  302. reordered_tests,
  303. expected=[
  304. "Tests2.test3",
  305. "Tests2.test2",
  306. "Tests1.test1",
  307. ],
  308. )
  309. class DependencyOrderingTests(unittest.TestCase):
  310. def test_simple_dependencies(self):
  311. raw = [
  312. ("s1", ("s1_db", ["alpha"])),
  313. ("s2", ("s2_db", ["bravo"])),
  314. ("s3", ("s3_db", ["charlie"])),
  315. ]
  316. dependencies = {
  317. "alpha": ["charlie"],
  318. "bravo": ["charlie"],
  319. }
  320. ordered = dependency_ordered(raw, dependencies=dependencies)
  321. ordered_sigs = [sig for sig, value in ordered]
  322. self.assertIn("s1", ordered_sigs)
  323. self.assertIn("s2", ordered_sigs)
  324. self.assertIn("s3", ordered_sigs)
  325. self.assertLess(ordered_sigs.index("s3"), ordered_sigs.index("s1"))
  326. self.assertLess(ordered_sigs.index("s3"), ordered_sigs.index("s2"))
  327. def test_chained_dependencies(self):
  328. raw = [
  329. ("s1", ("s1_db", ["alpha"])),
  330. ("s2", ("s2_db", ["bravo"])),
  331. ("s3", ("s3_db", ["charlie"])),
  332. ]
  333. dependencies = {
  334. "alpha": ["bravo"],
  335. "bravo": ["charlie"],
  336. }
  337. ordered = dependency_ordered(raw, dependencies=dependencies)
  338. ordered_sigs = [sig for sig, value in ordered]
  339. self.assertIn("s1", ordered_sigs)
  340. self.assertIn("s2", ordered_sigs)
  341. self.assertIn("s3", ordered_sigs)
  342. # Explicit dependencies
  343. self.assertLess(ordered_sigs.index("s2"), ordered_sigs.index("s1"))
  344. self.assertLess(ordered_sigs.index("s3"), ordered_sigs.index("s2"))
  345. # Implied dependencies
  346. self.assertLess(ordered_sigs.index("s3"), ordered_sigs.index("s1"))
  347. def test_multiple_dependencies(self):
  348. raw = [
  349. ("s1", ("s1_db", ["alpha"])),
  350. ("s2", ("s2_db", ["bravo"])),
  351. ("s3", ("s3_db", ["charlie"])),
  352. ("s4", ("s4_db", ["delta"])),
  353. ]
  354. dependencies = {
  355. "alpha": ["bravo", "delta"],
  356. "bravo": ["charlie"],
  357. "delta": ["charlie"],
  358. }
  359. ordered = dependency_ordered(raw, dependencies=dependencies)
  360. ordered_sigs = [sig for sig, aliases in ordered]
  361. self.assertIn("s1", ordered_sigs)
  362. self.assertIn("s2", ordered_sigs)
  363. self.assertIn("s3", ordered_sigs)
  364. self.assertIn("s4", ordered_sigs)
  365. # Explicit dependencies
  366. self.assertLess(ordered_sigs.index("s2"), ordered_sigs.index("s1"))
  367. self.assertLess(ordered_sigs.index("s4"), ordered_sigs.index("s1"))
  368. self.assertLess(ordered_sigs.index("s3"), ordered_sigs.index("s2"))
  369. self.assertLess(ordered_sigs.index("s3"), ordered_sigs.index("s4"))
  370. # Implicit dependencies
  371. self.assertLess(ordered_sigs.index("s3"), ordered_sigs.index("s1"))
  372. def test_circular_dependencies(self):
  373. raw = [
  374. ("s1", ("s1_db", ["alpha"])),
  375. ("s2", ("s2_db", ["bravo"])),
  376. ]
  377. dependencies = {
  378. "bravo": ["alpha"],
  379. "alpha": ["bravo"],
  380. }
  381. with self.assertRaises(ImproperlyConfigured):
  382. dependency_ordered(raw, dependencies=dependencies)
  383. def test_own_alias_dependency(self):
  384. raw = [("s1", ("s1_db", ["alpha", "bravo"]))]
  385. dependencies = {"alpha": ["bravo"]}
  386. with self.assertRaises(ImproperlyConfigured):
  387. dependency_ordered(raw, dependencies=dependencies)
  388. # reordering aliases shouldn't matter
  389. raw = [("s1", ("s1_db", ["bravo", "alpha"]))]
  390. with self.assertRaises(ImproperlyConfigured):
  391. dependency_ordered(raw, dependencies=dependencies)
  392. class MockTestRunner:
  393. def __init__(self, *args, **kwargs):
  394. if parallel := kwargs.get("parallel"):
  395. sys.stderr.write(f"parallel={parallel}")
  396. MockTestRunner.run_tests = mock.Mock(return_value=[])
  397. class ManageCommandTests(unittest.TestCase):
  398. def test_custom_test_runner(self):
  399. call_command("test", "sites", testrunner="test_runner.tests.MockTestRunner")
  400. MockTestRunner.run_tests.assert_called_with(("sites",))
  401. def test_bad_test_runner(self):
  402. with self.assertRaises(AttributeError):
  403. call_command("test", "sites", testrunner="test_runner.NonexistentRunner")
  404. def test_time_recorded(self):
  405. with captured_stderr() as stderr:
  406. call_command(
  407. "test",
  408. "--timing",
  409. "sites",
  410. testrunner="test_runner.tests.MockTestRunner",
  411. )
  412. self.assertIn("Total run took", stderr.getvalue())
  413. # Isolate from the real environment.
  414. @mock.patch.dict(os.environ, {}, clear=True)
  415. @mock.patch.object(multiprocessing, "cpu_count", return_value=12)
  416. class ManageCommandParallelTests(SimpleTestCase):
  417. def test_parallel_default(self, *mocked_objects):
  418. with captured_stderr() as stderr:
  419. call_command(
  420. "test",
  421. "--parallel",
  422. testrunner="test_runner.tests.MockTestRunner",
  423. )
  424. self.assertIn("parallel=12", stderr.getvalue())
  425. def test_parallel_auto(self, *mocked_objects):
  426. with captured_stderr() as stderr:
  427. call_command(
  428. "test",
  429. "--parallel=auto",
  430. testrunner="test_runner.tests.MockTestRunner",
  431. )
  432. self.assertIn("parallel=12", stderr.getvalue())
  433. def test_no_parallel(self, *mocked_objects):
  434. with captured_stderr() as stderr:
  435. call_command("test", testrunner="test_runner.tests.MockTestRunner")
  436. # Parallel is disabled by default.
  437. self.assertEqual(stderr.getvalue(), "")
  438. @mock.patch.object(multiprocessing, "get_start_method", return_value="spawn")
  439. def test_parallel_spawn(self, *mocked_objects):
  440. with captured_stderr() as stderr:
  441. call_command(
  442. "test",
  443. "--parallel=auto",
  444. testrunner="test_runner.tests.MockTestRunner",
  445. )
  446. self.assertIn("parallel=1", stderr.getvalue())
  447. @mock.patch.object(multiprocessing, "get_start_method", return_value="spawn")
  448. def test_no_parallel_spawn(self, *mocked_objects):
  449. with captured_stderr() as stderr:
  450. call_command(
  451. "test",
  452. testrunner="test_runner.tests.MockTestRunner",
  453. )
  454. self.assertEqual(stderr.getvalue(), "")
  455. @mock.patch.dict(os.environ, {"DJANGO_TEST_PROCESSES": "7"})
  456. def test_no_parallel_django_test_processes_env(self, *mocked_objects):
  457. with captured_stderr() as stderr:
  458. call_command("test", testrunner="test_runner.tests.MockTestRunner")
  459. self.assertEqual(stderr.getvalue(), "")
  460. @mock.patch.dict(os.environ, {"DJANGO_TEST_PROCESSES": "invalid"})
  461. def test_django_test_processes_env_non_int(self, *mocked_objects):
  462. with self.assertRaises(ValueError):
  463. call_command(
  464. "test",
  465. "--parallel",
  466. testrunner="test_runner.tests.MockTestRunner",
  467. )
  468. @mock.patch.dict(os.environ, {"DJANGO_TEST_PROCESSES": "7"})
  469. def test_django_test_processes_parallel_default(self, *mocked_objects):
  470. for parallel in ["--parallel", "--parallel=auto"]:
  471. with self.subTest(parallel=parallel):
  472. with captured_stderr() as stderr:
  473. call_command(
  474. "test",
  475. parallel,
  476. testrunner="test_runner.tests.MockTestRunner",
  477. )
  478. self.assertIn("parallel=7", stderr.getvalue())
  479. class CustomTestRunnerOptionsSettingsTests(AdminScriptTestCase):
  480. """
  481. Custom runners can add command line arguments. The runner is specified
  482. through a settings file.
  483. """
  484. def setUp(self):
  485. super().setUp()
  486. settings = {
  487. "TEST_RUNNER": "'test_runner.runner.CustomOptionsTestRunner'",
  488. }
  489. self.write_settings("settings.py", sdict=settings)
  490. def test_default_options(self):
  491. args = ["test", "--settings=test_project.settings"]
  492. out, err = self.run_django_admin(args)
  493. self.assertNoOutput(err)
  494. self.assertOutput(out, "1:2:3")
  495. def test_default_and_given_options(self):
  496. args = ["test", "--settings=test_project.settings", "--option_b=foo"]
  497. out, err = self.run_django_admin(args)
  498. self.assertNoOutput(err)
  499. self.assertOutput(out, "1:foo:3")
  500. def test_option_name_and_value_separated(self):
  501. args = ["test", "--settings=test_project.settings", "--option_b", "foo"]
  502. out, err = self.run_django_admin(args)
  503. self.assertNoOutput(err)
  504. self.assertOutput(out, "1:foo:3")
  505. def test_all_options_given(self):
  506. args = [
  507. "test",
  508. "--settings=test_project.settings",
  509. "--option_a=bar",
  510. "--option_b=foo",
  511. "--option_c=31337",
  512. ]
  513. out, err = self.run_django_admin(args)
  514. self.assertNoOutput(err)
  515. self.assertOutput(out, "bar:foo:31337")
  516. class CustomTestRunnerOptionsCmdlineTests(AdminScriptTestCase):
  517. """
  518. Custom runners can add command line arguments when the runner is specified
  519. using --testrunner.
  520. """
  521. def setUp(self):
  522. super().setUp()
  523. self.write_settings("settings.py")
  524. def test_testrunner_option(self):
  525. args = [
  526. "test",
  527. "--testrunner",
  528. "test_runner.runner.CustomOptionsTestRunner",
  529. "--option_a=bar",
  530. "--option_b=foo",
  531. "--option_c=31337",
  532. ]
  533. out, err = self.run_django_admin(args, "test_project.settings")
  534. self.assertNoOutput(err)
  535. self.assertOutput(out, "bar:foo:31337")
  536. def test_testrunner_equals(self):
  537. args = [
  538. "test",
  539. "--testrunner=test_runner.runner.CustomOptionsTestRunner",
  540. "--option_a=bar",
  541. "--option_b=foo",
  542. "--option_c=31337",
  543. ]
  544. out, err = self.run_django_admin(args, "test_project.settings")
  545. self.assertNoOutput(err)
  546. self.assertOutput(out, "bar:foo:31337")
  547. def test_no_testrunner(self):
  548. args = ["test", "--testrunner"]
  549. out, err = self.run_django_admin(args, "test_project.settings")
  550. self.assertIn("usage", err)
  551. self.assertNotIn("Traceback", err)
  552. self.assertNoOutput(out)
  553. class NoInitializeSuiteTestRunnerTests(SimpleTestCase):
  554. @mock.patch.object(multiprocessing, "get_start_method", return_value="spawn")
  555. @mock.patch(
  556. "django.test.runner.ParallelTestSuite.initialize_suite",
  557. side_effect=Exception("initialize_suite() is called."),
  558. )
  559. def test_no_initialize_suite_test_runner(self, *mocked_objects):
  560. """
  561. The test suite's initialize_suite() method must always be called when
  562. using spawn. It cannot rely on a test runner implementation.
  563. """
  564. class NoInitializeSuiteTestRunner(DiscoverRunner):
  565. def setup_test_environment(self, **kwargs):
  566. return
  567. def setup_databases(self, **kwargs):
  568. return
  569. def run_checks(self, databases):
  570. return
  571. def teardown_databases(self, old_config, **kwargs):
  572. return
  573. def teardown_test_environment(self, **kwargs):
  574. return
  575. def run_suite(self, suite, **kwargs):
  576. kwargs = self.get_test_runner_kwargs()
  577. runner = self.test_runner(**kwargs)
  578. return runner.run(suite)
  579. with self.assertRaisesMessage(Exception, "initialize_suite() is called."):
  580. runner = NoInitializeSuiteTestRunner(
  581. verbosity=0, interactive=False, parallel=2
  582. )
  583. runner.run_tests(
  584. [
  585. "test_runner_apps.sample.tests_sample.TestDjangoTestCase",
  586. "test_runner_apps.simple.tests",
  587. ]
  588. )
  589. class TestRunnerInitializerTests(SimpleTestCase):
  590. # Raise an exception to don't actually run tests.
  591. @mock.patch.object(
  592. multiprocessing, "Pool", side_effect=Exception("multiprocessing.Pool()")
  593. )
  594. def test_no_initialize_suite_test_runner(self, mocked_pool):
  595. class StubTestRunner(DiscoverRunner):
  596. def setup_test_environment(self, **kwargs):
  597. return
  598. def setup_databases(self, **kwargs):
  599. return
  600. def run_checks(self, databases):
  601. return
  602. def teardown_databases(self, old_config, **kwargs):
  603. return
  604. def teardown_test_environment(self, **kwargs):
  605. return
  606. def run_suite(self, suite, **kwargs):
  607. kwargs = self.get_test_runner_kwargs()
  608. runner = self.test_runner(**kwargs)
  609. return runner.run(suite)
  610. runner = StubTestRunner(
  611. verbosity=0, interactive=False, parallel=2, debug_mode=True
  612. )
  613. with self.assertRaisesMessage(Exception, "multiprocessing.Pool()"):
  614. runner.run_tests(
  615. [
  616. "test_runner_apps.sample.tests_sample.TestDjangoTestCase",
  617. "test_runner_apps.simple.tests",
  618. ]
  619. )
  620. # Initializer must be a function.
  621. self.assertIs(mocked_pool.call_args.kwargs["initializer"], _init_worker)
  622. initargs = mocked_pool.call_args.kwargs["initargs"]
  623. self.assertEqual(len(initargs), 6)
  624. self.assertEqual(initargs[5], True) # debug_mode
  625. class Ticket17477RegressionTests(AdminScriptTestCase):
  626. def setUp(self):
  627. super().setUp()
  628. self.write_settings("settings.py")
  629. def test_ticket_17477(self):
  630. """'manage.py help test' works after r16352."""
  631. args = ["help", "test"]
  632. out, err = self.run_manage(args)
  633. self.assertNoOutput(err)
  634. class SQLiteInMemoryTestDbs(TransactionTestCase):
  635. available_apps = ["test_runner"]
  636. databases = {"default", "other"}
  637. @unittest.skipUnless(
  638. all(db.connections[conn].vendor == "sqlite" for conn in db.connections),
  639. "This is an sqlite-specific issue",
  640. )
  641. def test_transaction_support(self):
  642. # Assert connections mocking is appropriately applied by preventing
  643. # any attempts at calling create_test_db on the global connection
  644. # objects.
  645. for connection in db.connections.all():
  646. create_test_db = mock.patch.object(
  647. connection.creation,
  648. "create_test_db",
  649. side_effect=AssertionError(
  650. "Global connection object shouldn't be manipulated."
  651. ),
  652. )
  653. create_test_db.start()
  654. self.addCleanup(create_test_db.stop)
  655. for option_key, option_value in (
  656. ("NAME", ":memory:"),
  657. ("TEST", {"NAME": ":memory:"}),
  658. ):
  659. tested_connections = db.ConnectionHandler(
  660. {
  661. "default": {
  662. "ENGINE": "django.db.backends.sqlite3",
  663. option_key: option_value,
  664. },
  665. "other": {
  666. "ENGINE": "django.db.backends.sqlite3",
  667. option_key: option_value,
  668. },
  669. }
  670. )
  671. with mock.patch("django.test.utils.connections", new=tested_connections):
  672. other = tested_connections["other"]
  673. DiscoverRunner(verbosity=0).setup_databases()
  674. msg = (
  675. "DATABASES setting '%s' option set to sqlite3's ':memory:' value "
  676. "shouldn't interfere with transaction support detection."
  677. % option_key
  678. )
  679. # Transaction support is properly initialized for the 'other' DB.
  680. self.assertTrue(other.features.supports_transactions, msg)
  681. # And all the DBs report that they support transactions.
  682. self.assertTrue(connections_support_transactions(), msg)
  683. class DummyBackendTest(unittest.TestCase):
  684. def test_setup_databases(self):
  685. """
  686. setup_databases() doesn't fail with dummy database backend.
  687. """
  688. tested_connections = db.ConnectionHandler({})
  689. with mock.patch("django.test.utils.connections", new=tested_connections):
  690. runner_instance = DiscoverRunner(verbosity=0)
  691. old_config = runner_instance.setup_databases()
  692. runner_instance.teardown_databases(old_config)
  693. class AliasedDefaultTestSetupTest(unittest.TestCase):
  694. def test_setup_aliased_default_database(self):
  695. """
  696. setup_databases() doesn't fail when 'default' is aliased
  697. """
  698. tested_connections = db.ConnectionHandler(
  699. {"default": {"NAME": "dummy"}, "aliased": {"NAME": "dummy"}}
  700. )
  701. with mock.patch("django.test.utils.connections", new=tested_connections):
  702. runner_instance = DiscoverRunner(verbosity=0)
  703. old_config = runner_instance.setup_databases()
  704. runner_instance.teardown_databases(old_config)
  705. class SetupDatabasesTests(unittest.TestCase):
  706. def setUp(self):
  707. self.runner_instance = DiscoverRunner(verbosity=0)
  708. def test_setup_aliased_databases(self):
  709. tested_connections = db.ConnectionHandler(
  710. {
  711. "default": {
  712. "ENGINE": "django.db.backends.dummy",
  713. "NAME": "dbname",
  714. },
  715. "other": {
  716. "ENGINE": "django.db.backends.dummy",
  717. "NAME": "dbname",
  718. },
  719. }
  720. )
  721. with mock.patch(
  722. "django.db.backends.dummy.base.DatabaseWrapper.creation_class"
  723. ) as mocked_db_creation:
  724. with mock.patch("django.test.utils.connections", new=tested_connections):
  725. old_config = self.runner_instance.setup_databases()
  726. self.runner_instance.teardown_databases(old_config)
  727. mocked_db_creation.return_value.destroy_test_db.assert_called_once_with(
  728. "dbname", 0, False
  729. )
  730. def test_setup_test_database_aliases(self):
  731. """
  732. The default database must be the first because data migrations
  733. use the default alias by default.
  734. """
  735. tested_connections = db.ConnectionHandler(
  736. {
  737. "other": {
  738. "ENGINE": "django.db.backends.dummy",
  739. "NAME": "dbname",
  740. },
  741. "default": {
  742. "ENGINE": "django.db.backends.dummy",
  743. "NAME": "dbname",
  744. },
  745. }
  746. )
  747. with mock.patch("django.test.utils.connections", new=tested_connections):
  748. test_databases, _ = get_unique_databases_and_mirrors()
  749. self.assertEqual(
  750. test_databases,
  751. {
  752. ("", "", "django.db.backends.dummy", "test_dbname"): (
  753. "dbname",
  754. ["default", "other"],
  755. ),
  756. },
  757. )
  758. def test_destroy_test_db_restores_db_name(self):
  759. tested_connections = db.ConnectionHandler(
  760. {
  761. "default": {
  762. "ENGINE": settings.DATABASES[db.DEFAULT_DB_ALIAS]["ENGINE"],
  763. "NAME": "xxx_test_database",
  764. },
  765. }
  766. )
  767. # Using the real current name as old_name to not mess with the test suite.
  768. old_name = settings.DATABASES[db.DEFAULT_DB_ALIAS]["NAME"]
  769. with mock.patch("django.db.connections", new=tested_connections):
  770. tested_connections["default"].creation.destroy_test_db(
  771. old_name, verbosity=0, keepdb=True
  772. )
  773. self.assertEqual(
  774. tested_connections["default"].settings_dict["NAME"], old_name
  775. )
  776. def test_serialization(self):
  777. tested_connections = db.ConnectionHandler(
  778. {
  779. "default": {
  780. "ENGINE": "django.db.backends.dummy",
  781. },
  782. }
  783. )
  784. with mock.patch(
  785. "django.db.backends.dummy.base.DatabaseWrapper.creation_class"
  786. ) as mocked_db_creation:
  787. with mock.patch("django.test.utils.connections", new=tested_connections):
  788. self.runner_instance.setup_databases()
  789. mocked_db_creation.return_value.create_test_db.assert_called_once_with(
  790. verbosity=0, autoclobber=False, serialize=True, keepdb=False
  791. )
  792. @skipUnlessDBFeature("supports_sequence_reset")
  793. class AutoIncrementResetTest(TransactionTestCase):
  794. """
  795. Creating the same models in different test methods receive the same PK
  796. values since the sequences are reset before each test method.
  797. """
  798. available_apps = ["test_runner"]
  799. reset_sequences = True
  800. def _test(self):
  801. # Regular model
  802. p = Person.objects.create(first_name="Jack", last_name="Smith")
  803. self.assertEqual(p.pk, 1)
  804. # Auto-created many-to-many through model
  805. p.friends.add(Person.objects.create(first_name="Jacky", last_name="Smith"))
  806. self.assertEqual(p.friends.through.objects.first().pk, 1)
  807. # Many-to-many through model
  808. b = B.objects.create()
  809. t = Through.objects.create(person=p, b=b)
  810. self.assertEqual(t.pk, 1)
  811. def test_autoincrement_reset1(self):
  812. self._test()
  813. def test_autoincrement_reset2(self):
  814. self._test()
  815. class EmptyDefaultDatabaseTest(unittest.TestCase):
  816. def test_empty_default_database(self):
  817. """
  818. An empty default database in settings does not raise an ImproperlyConfigured
  819. error when running a unit test that does not use a database.
  820. """
  821. tested_connections = db.ConnectionHandler({"default": {}})
  822. with mock.patch("django.db.connections", new=tested_connections):
  823. connection = tested_connections[db.utils.DEFAULT_DB_ALIAS]
  824. self.assertEqual(
  825. connection.settings_dict["ENGINE"], "django.db.backends.dummy"
  826. )
  827. connections_support_transactions()
  828. class RunTestsExceptionHandlingTests(unittest.TestCase):
  829. def test_run_checks_raises(self):
  830. """
  831. Teardown functions are run when run_checks() raises SystemCheckError.
  832. """
  833. with mock.patch(
  834. "django.test.runner.DiscoverRunner.setup_test_environment"
  835. ), mock.patch("django.test.runner.DiscoverRunner.setup_databases"), mock.patch(
  836. "django.test.runner.DiscoverRunner.build_suite"
  837. ), mock.patch(
  838. "django.test.runner.DiscoverRunner.run_checks", side_effect=SystemCheckError
  839. ), mock.patch(
  840. "django.test.runner.DiscoverRunner.teardown_databases"
  841. ) as teardown_databases, mock.patch(
  842. "django.test.runner.DiscoverRunner.teardown_test_environment"
  843. ) as teardown_test_environment:
  844. runner = DiscoverRunner(verbosity=0, interactive=False)
  845. with self.assertRaises(SystemCheckError):
  846. runner.run_tests(
  847. ["test_runner_apps.sample.tests_sample.TestDjangoTestCase"]
  848. )
  849. self.assertTrue(teardown_databases.called)
  850. self.assertTrue(teardown_test_environment.called)
  851. def test_run_checks_raises_and_teardown_raises(self):
  852. """
  853. SystemCheckError is surfaced when run_checks() raises SystemCheckError
  854. and teardown databases() raises ValueError.
  855. """
  856. with mock.patch(
  857. "django.test.runner.DiscoverRunner.setup_test_environment"
  858. ), mock.patch("django.test.runner.DiscoverRunner.setup_databases"), mock.patch(
  859. "django.test.runner.DiscoverRunner.build_suite"
  860. ), mock.patch(
  861. "django.test.runner.DiscoverRunner.run_checks", side_effect=SystemCheckError
  862. ), mock.patch(
  863. "django.test.runner.DiscoverRunner.teardown_databases",
  864. side_effect=ValueError,
  865. ) as teardown_databases, mock.patch(
  866. "django.test.runner.DiscoverRunner.teardown_test_environment"
  867. ) as teardown_test_environment:
  868. runner = DiscoverRunner(verbosity=0, interactive=False)
  869. with self.assertRaises(SystemCheckError):
  870. runner.run_tests(
  871. ["test_runner_apps.sample.tests_sample.TestDjangoTestCase"]
  872. )
  873. self.assertTrue(teardown_databases.called)
  874. self.assertFalse(teardown_test_environment.called)
  875. def test_run_checks_passes_and_teardown_raises(self):
  876. """
  877. Exceptions on teardown are surfaced if no exceptions happen during
  878. run_checks().
  879. """
  880. with mock.patch(
  881. "django.test.runner.DiscoverRunner.setup_test_environment"
  882. ), mock.patch("django.test.runner.DiscoverRunner.setup_databases"), mock.patch(
  883. "django.test.runner.DiscoverRunner.build_suite"
  884. ), mock.patch(
  885. "django.test.runner.DiscoverRunner.run_checks"
  886. ), mock.patch(
  887. "django.test.runner.DiscoverRunner.teardown_databases",
  888. side_effect=ValueError,
  889. ) as teardown_databases, mock.patch(
  890. "django.test.runner.DiscoverRunner.teardown_test_environment"
  891. ) as teardown_test_environment:
  892. runner = DiscoverRunner(verbosity=0, interactive=False)
  893. with self.assertRaises(ValueError):
  894. # Suppress the output when running TestDjangoTestCase.
  895. with mock.patch("sys.stderr"):
  896. runner.run_tests(
  897. ["test_runner_apps.sample.tests_sample.TestDjangoTestCase"]
  898. )
  899. self.assertTrue(teardown_databases.called)
  900. self.assertFalse(teardown_test_environment.called)
  901. # RemovedInDjango50Warning
  902. class NoOpTestRunner(DiscoverRunner):
  903. def setup_test_environment(self, **kwargs):
  904. return
  905. def setup_databases(self, **kwargs):
  906. return
  907. def run_checks(self, databases):
  908. return
  909. def teardown_databases(self, old_config, **kwargs):
  910. return
  911. def teardown_test_environment(self, **kwargs):
  912. return
  913. class DiscoverRunnerExtraTestsDeprecationTests(SimpleTestCase):
  914. msg = "The extra_tests argument is deprecated."
  915. def get_runner(self):
  916. return NoOpTestRunner(verbosity=0, interactive=False)
  917. def test_extra_tests_build_suite(self):
  918. runner = self.get_runner()
  919. with self.assertWarnsMessage(RemovedInDjango50Warning, self.msg):
  920. runner.build_suite(extra_tests=[])
  921. def test_extra_tests_run_tests(self):
  922. runner = self.get_runner()
  923. with captured_stderr():
  924. with self.assertWarnsMessage(RemovedInDjango50Warning, self.msg):
  925. runner.run_tests(
  926. test_labels=["test_runner_apps.sample.tests_sample.EmptyTestCase"],
  927. extra_tests=[],
  928. )