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