2009-12-13 17:08:36 +00:00
|
|
|
"""
|
|
|
|
Tests for django test runner
|
|
|
|
"""
|
2021-04-24 23:46:16 +00:00
|
|
|
import collections.abc
|
2021-08-06 06:36:59 +00:00
|
|
|
import multiprocessing
|
|
|
|
import os
|
|
|
|
import sys
|
2013-07-01 12:22:27 +00:00
|
|
|
import unittest
|
2017-01-19 17:16:04 +00:00
|
|
|
from unittest import mock
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2015-01-28 12:35:27 +00:00
|
|
|
from admin_scripts.tests import AdminScriptTestCase
|
|
|
|
|
2014-09-15 15:17:12 +00:00
|
|
|
from django import db
|
2014-10-24 12:53:58 +00:00
|
|
|
from django.conf import settings
|
2010-12-05 00:44:34 +00:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2011-06-10 08:26:05 +00:00
|
|
|
from django.core.management import call_command
|
2018-10-18 23:04:28 +00:00
|
|
|
from django.core.management.base import SystemCheckError
|
2021-02-15 03:42:47 +00:00
|
|
|
from django.test import SimpleTestCase, TransactionTestCase, skipUnlessDBFeature
|
2021-04-24 23:46:16 +00:00
|
|
|
from django.test.runner import (
|
|
|
|
DiscoverRunner,
|
|
|
|
Shuffler,
|
2022-05-19 08:20:51 +00:00
|
|
|
_init_worker,
|
2021-04-24 23:46:16 +00:00
|
|
|
reorder_test_bin,
|
|
|
|
reorder_tests,
|
|
|
|
shuffle_tests,
|
|
|
|
)
|
2012-03-13 17:52:48 +00:00
|
|
|
from django.test.testcases import connections_support_transactions
|
2021-02-15 13:42:00 +00:00
|
|
|
from django.test.utils import (
|
|
|
|
captured_stderr,
|
|
|
|
dependency_ordered,
|
|
|
|
get_unique_databases_and_mirrors,
|
2021-03-04 09:53:45 +00:00
|
|
|
iter_test_cases,
|
2021-02-15 13:42:00 +00:00
|
|
|
)
|
2021-02-15 03:42:47 +00:00
|
|
|
from django.utils.deprecation import RemovedInDjango50Warning
|
2011-06-12 12:34:10 +00:00
|
|
|
|
2018-07-11 20:37:56 +00:00
|
|
|
from .models import B, Person, Through
|
2009-12-22 06:00:57 +00:00
|
|
|
|
2011-04-02 13:26:39 +00:00
|
|
|
|
2021-03-04 09:53:45 +00:00
|
|
|
class MySuite:
|
|
|
|
def __init__(self):
|
|
|
|
self.tests = []
|
|
|
|
|
|
|
|
def addTest(self, test):
|
|
|
|
self.tests.append(test)
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
yield from self.tests
|
|
|
|
|
|
|
|
|
2021-06-29 17:02:57 +00:00
|
|
|
class TestSuiteTests(SimpleTestCase):
|
2021-03-08 15:06:03 +00:00
|
|
|
def build_test_suite(self, test_classes, suite=None, suite_class=None):
|
2021-03-04 09:53:45 +00:00
|
|
|
if suite_class is None:
|
|
|
|
suite_class = unittest.TestSuite
|
|
|
|
if suite is None:
|
|
|
|
suite = suite_class()
|
|
|
|
|
2021-03-08 15:06:03 +00:00
|
|
|
loader = unittest.defaultTestLoader
|
|
|
|
for test_class in test_classes:
|
|
|
|
tests = loader.loadTestsFromTestCase(test_class)
|
|
|
|
subsuite = suite_class()
|
|
|
|
# Only use addTest() to simplify testing a custom TestSuite.
|
|
|
|
for test in tests:
|
|
|
|
subsuite.addTest(test)
|
|
|
|
suite.addTest(subsuite)
|
|
|
|
|
|
|
|
return suite
|
|
|
|
|
|
|
|
def make_test_suite(self, suite=None, suite_class=None):
|
2021-03-04 09:53:45 +00:00
|
|
|
class Tests1(unittest.TestCase):
|
|
|
|
def test1(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test2(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class Tests2(unittest.TestCase):
|
|
|
|
def test1(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test2(self):
|
|
|
|
pass
|
|
|
|
|
2021-03-08 15:06:03 +00:00
|
|
|
return self.build_test_suite(
|
|
|
|
(Tests1, Tests2),
|
|
|
|
suite=suite,
|
|
|
|
suite_class=suite_class,
|
|
|
|
)
|
2021-03-04 09:53:45 +00:00
|
|
|
|
|
|
|
def assertTestNames(self, tests, expected):
|
|
|
|
# Each test.id() has a form like the following:
|
|
|
|
# "test_runner.tests.IterTestCasesTests.test_iter_test_cases.<locals>.Tests1.test1".
|
|
|
|
# It suffices to check only the last two parts.
|
|
|
|
names = [".".join(test.id().split(".")[-2:]) for test in tests]
|
|
|
|
self.assertEqual(names, expected)
|
|
|
|
|
2021-03-08 15:06:03 +00:00
|
|
|
def test_iter_test_cases_basic(self):
|
2021-03-04 09:53:45 +00:00
|
|
|
suite = self.make_test_suite()
|
|
|
|
tests = iter_test_cases(suite)
|
|
|
|
self.assertTestNames(
|
|
|
|
tests,
|
|
|
|
expected=[
|
|
|
|
"Tests1.test1",
|
|
|
|
"Tests1.test2",
|
|
|
|
"Tests2.test1",
|
|
|
|
"Tests2.test2",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2021-06-29 17:02:57 +00:00
|
|
|
def test_iter_test_cases_string_input(self):
|
|
|
|
msg = (
|
|
|
|
"Test 'a' must be a test case or test suite not string (was found "
|
|
|
|
"in 'abc')."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
|
|
|
list(iter_test_cases("abc"))
|
|
|
|
|
2021-03-10 07:12:44 +00:00
|
|
|
def test_iter_test_cases_iterable_of_tests(self):
|
|
|
|
class Tests(unittest.TestCase):
|
|
|
|
def test1(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test2(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
tests = list(unittest.defaultTestLoader.loadTestsFromTestCase(Tests))
|
|
|
|
actual_tests = iter_test_cases(tests)
|
|
|
|
self.assertTestNames(
|
|
|
|
actual_tests,
|
|
|
|
expected=[
|
|
|
|
"Tests.test1",
|
|
|
|
"Tests.test2",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2021-03-08 15:06:03 +00:00
|
|
|
def test_iter_test_cases_custom_test_suite_class(self):
|
2021-03-04 09:53:45 +00:00
|
|
|
suite = self.make_test_suite(suite_class=MySuite)
|
|
|
|
tests = iter_test_cases(suite)
|
|
|
|
self.assertTestNames(
|
|
|
|
tests,
|
|
|
|
expected=[
|
|
|
|
"Tests1.test1",
|
|
|
|
"Tests1.test2",
|
|
|
|
"Tests2.test1",
|
|
|
|
"Tests2.test2",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2021-03-08 15:06:03 +00:00
|
|
|
def test_iter_test_cases_mixed_test_suite_classes(self):
|
2021-03-04 09:53:45 +00:00
|
|
|
suite = self.make_test_suite(suite=MySuite())
|
|
|
|
child_suite = list(suite)[0]
|
|
|
|
self.assertNotIsInstance(child_suite, MySuite)
|
|
|
|
tests = list(iter_test_cases(suite))
|
|
|
|
self.assertEqual(len(tests), 4)
|
|
|
|
self.assertNotIsInstance(tests[0], unittest.TestSuite)
|
|
|
|
|
2021-04-24 23:46:16 +00:00
|
|
|
def make_tests(self):
|
|
|
|
"""Return an iterable of tests."""
|
|
|
|
suite = self.make_test_suite()
|
|
|
|
tests = list(iter_test_cases(suite))
|
|
|
|
return tests
|
|
|
|
|
|
|
|
def test_shuffle_tests(self):
|
|
|
|
tests = self.make_tests()
|
|
|
|
# Choose a seed that shuffles both the classes and methods.
|
|
|
|
shuffler = Shuffler(seed=9)
|
|
|
|
shuffled_tests = shuffle_tests(tests, shuffler)
|
|
|
|
self.assertIsInstance(shuffled_tests, collections.abc.Iterator)
|
|
|
|
self.assertTestNames(
|
|
|
|
shuffled_tests,
|
|
|
|
expected=[
|
|
|
|
"Tests2.test1",
|
|
|
|
"Tests2.test2",
|
|
|
|
"Tests1.test2",
|
|
|
|
"Tests1.test1",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_reorder_test_bin_no_arguments(self):
|
|
|
|
tests = self.make_tests()
|
|
|
|
reordered_tests = reorder_test_bin(tests)
|
|
|
|
self.assertIsInstance(reordered_tests, collections.abc.Iterator)
|
|
|
|
self.assertTestNames(
|
|
|
|
reordered_tests,
|
|
|
|
expected=[
|
|
|
|
"Tests1.test1",
|
|
|
|
"Tests1.test2",
|
|
|
|
"Tests2.test1",
|
|
|
|
"Tests2.test2",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_reorder_test_bin_reverse(self):
|
|
|
|
tests = self.make_tests()
|
|
|
|
reordered_tests = reorder_test_bin(tests, reverse=True)
|
|
|
|
self.assertIsInstance(reordered_tests, collections.abc.Iterator)
|
|
|
|
self.assertTestNames(
|
|
|
|
reordered_tests,
|
|
|
|
expected=[
|
|
|
|
"Tests2.test2",
|
|
|
|
"Tests2.test1",
|
|
|
|
"Tests1.test2",
|
|
|
|
"Tests1.test1",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_reorder_test_bin_random(self):
|
|
|
|
tests = self.make_tests()
|
|
|
|
# Choose a seed that shuffles both the classes and methods.
|
|
|
|
shuffler = Shuffler(seed=9)
|
|
|
|
reordered_tests = reorder_test_bin(tests, shuffler=shuffler)
|
|
|
|
self.assertIsInstance(reordered_tests, collections.abc.Iterator)
|
|
|
|
self.assertTestNames(
|
|
|
|
reordered_tests,
|
|
|
|
expected=[
|
|
|
|
"Tests2.test1",
|
|
|
|
"Tests2.test2",
|
|
|
|
"Tests1.test2",
|
|
|
|
"Tests1.test1",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_reorder_test_bin_random_and_reverse(self):
|
|
|
|
tests = self.make_tests()
|
|
|
|
# Choose a seed that shuffles both the classes and methods.
|
|
|
|
shuffler = Shuffler(seed=9)
|
|
|
|
reordered_tests = reorder_test_bin(tests, shuffler=shuffler, reverse=True)
|
|
|
|
self.assertIsInstance(reordered_tests, collections.abc.Iterator)
|
|
|
|
self.assertTestNames(
|
|
|
|
reordered_tests,
|
|
|
|
expected=[
|
|
|
|
"Tests1.test1",
|
|
|
|
"Tests1.test2",
|
|
|
|
"Tests2.test2",
|
|
|
|
"Tests2.test1",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2021-07-09 20:12:17 +00:00
|
|
|
def test_reorder_tests_same_type_consecutive(self):
|
|
|
|
"""Tests of the same type are made consecutive."""
|
|
|
|
tests = self.make_tests()
|
|
|
|
# Move the last item to the front.
|
|
|
|
tests.insert(0, tests.pop())
|
|
|
|
self.assertTestNames(
|
|
|
|
tests,
|
|
|
|
expected=[
|
|
|
|
"Tests2.test2",
|
|
|
|
"Tests1.test1",
|
|
|
|
"Tests1.test2",
|
|
|
|
"Tests2.test1",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
reordered_tests = reorder_tests(tests, classes=[])
|
|
|
|
self.assertTestNames(
|
|
|
|
reordered_tests,
|
|
|
|
expected=[
|
|
|
|
"Tests2.test2",
|
|
|
|
"Tests2.test1",
|
|
|
|
"Tests1.test1",
|
|
|
|
"Tests1.test2",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2021-04-24 23:46:16 +00:00
|
|
|
def test_reorder_tests_random(self):
|
|
|
|
tests = self.make_tests()
|
|
|
|
# Choose a seed that shuffles both the classes and methods.
|
|
|
|
shuffler = Shuffler(seed=9)
|
|
|
|
reordered_tests = reorder_tests(tests, classes=[], shuffler=shuffler)
|
|
|
|
self.assertIsInstance(reordered_tests, collections.abc.Iterator)
|
|
|
|
self.assertTestNames(
|
|
|
|
reordered_tests,
|
|
|
|
expected=[
|
|
|
|
"Tests2.test1",
|
|
|
|
"Tests2.test2",
|
|
|
|
"Tests1.test2",
|
|
|
|
"Tests1.test1",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2021-07-09 20:12:17 +00:00
|
|
|
def test_reorder_tests_random_mixed_classes(self):
|
|
|
|
tests = self.make_tests()
|
|
|
|
# Move the last item to the front.
|
|
|
|
tests.insert(0, tests.pop())
|
|
|
|
shuffler = Shuffler(seed=9)
|
|
|
|
self.assertTestNames(
|
|
|
|
tests,
|
|
|
|
expected=[
|
|
|
|
"Tests2.test2",
|
|
|
|
"Tests1.test1",
|
|
|
|
"Tests1.test2",
|
|
|
|
"Tests2.test1",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
reordered_tests = reorder_tests(tests, classes=[], shuffler=shuffler)
|
|
|
|
self.assertTestNames(
|
|
|
|
reordered_tests,
|
|
|
|
expected=[
|
|
|
|
"Tests2.test1",
|
|
|
|
"Tests2.test2",
|
|
|
|
"Tests1.test2",
|
|
|
|
"Tests1.test1",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2021-03-08 16:29:44 +00:00
|
|
|
def test_reorder_tests_reverse_with_duplicates(self):
|
2021-03-08 15:06:03 +00:00
|
|
|
class Tests1(unittest.TestCase):
|
|
|
|
def test1(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class Tests2(unittest.TestCase):
|
|
|
|
def test2(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test3(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
suite = self.build_test_suite((Tests1, Tests2))
|
|
|
|
subsuite = list(suite)[0]
|
|
|
|
suite.addTest(subsuite)
|
2021-03-08 16:29:44 +00:00
|
|
|
tests = list(iter_test_cases(suite))
|
|
|
|
self.assertTestNames(
|
|
|
|
tests,
|
|
|
|
expected=[
|
2021-03-08 15:06:03 +00:00
|
|
|
"Tests1.test1",
|
|
|
|
"Tests2.test2",
|
|
|
|
"Tests2.test3",
|
|
|
|
"Tests1.test1",
|
|
|
|
],
|
|
|
|
)
|
2021-03-08 16:29:44 +00:00
|
|
|
reordered_tests = reorder_tests(tests, classes=[])
|
|
|
|
self.assertTestNames(
|
|
|
|
reordered_tests,
|
|
|
|
expected=[
|
2021-03-08 15:06:03 +00:00
|
|
|
"Tests1.test1",
|
|
|
|
"Tests2.test2",
|
|
|
|
"Tests2.test3",
|
|
|
|
],
|
|
|
|
)
|
2021-03-08 16:29:44 +00:00
|
|
|
reordered_tests = reorder_tests(tests, classes=[], reverse=True)
|
|
|
|
self.assertTestNames(
|
|
|
|
reordered_tests,
|
|
|
|
expected=[
|
2021-03-08 15:06:03 +00:00
|
|
|
"Tests2.test3",
|
|
|
|
"Tests2.test2",
|
|
|
|
"Tests1.test1",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2021-03-04 09:53:45 +00:00
|
|
|
|
2010-12-05 00:44:34 +00:00
|
|
|
class DependencyOrderingTests(unittest.TestCase):
|
|
|
|
def test_simple_dependencies(self):
|
|
|
|
raw = [
|
2011-02-02 14:02:14 +00:00
|
|
|
("s1", ("s1_db", ["alpha"])),
|
|
|
|
("s2", ("s2_db", ["bravo"])),
|
|
|
|
("s3", ("s3_db", ["charlie"])),
|
2010-12-05 00:44:34 +00:00
|
|
|
]
|
|
|
|
dependencies = {
|
|
|
|
"alpha": ["charlie"],
|
|
|
|
"bravo": ["charlie"],
|
|
|
|
}
|
|
|
|
|
2014-11-15 19:03:31 +00:00
|
|
|
ordered = dependency_ordered(raw, dependencies=dependencies)
|
2013-10-26 19:15:03 +00:00
|
|
|
ordered_sigs = [sig for sig, value in ordered]
|
2010-12-05 00:44:34 +00:00
|
|
|
|
|
|
|
self.assertIn("s1", ordered_sigs)
|
|
|
|
self.assertIn("s2", ordered_sigs)
|
|
|
|
self.assertIn("s3", ordered_sigs)
|
|
|
|
self.assertLess(ordered_sigs.index("s3"), ordered_sigs.index("s1"))
|
|
|
|
self.assertLess(ordered_sigs.index("s3"), ordered_sigs.index("s2"))
|
|
|
|
|
|
|
|
def test_chained_dependencies(self):
|
|
|
|
raw = [
|
2011-02-02 14:02:14 +00:00
|
|
|
("s1", ("s1_db", ["alpha"])),
|
|
|
|
("s2", ("s2_db", ["bravo"])),
|
|
|
|
("s3", ("s3_db", ["charlie"])),
|
2010-12-05 00:44:34 +00:00
|
|
|
]
|
|
|
|
dependencies = {
|
|
|
|
"alpha": ["bravo"],
|
|
|
|
"bravo": ["charlie"],
|
|
|
|
}
|
|
|
|
|
2014-11-15 19:03:31 +00:00
|
|
|
ordered = dependency_ordered(raw, dependencies=dependencies)
|
2013-10-26 19:15:03 +00:00
|
|
|
ordered_sigs = [sig for sig, value in ordered]
|
2010-12-05 00:44:34 +00:00
|
|
|
|
|
|
|
self.assertIn("s1", ordered_sigs)
|
|
|
|
self.assertIn("s2", ordered_sigs)
|
|
|
|
self.assertIn("s3", ordered_sigs)
|
|
|
|
|
|
|
|
# Explicit dependencies
|
|
|
|
self.assertLess(ordered_sigs.index("s2"), ordered_sigs.index("s1"))
|
|
|
|
self.assertLess(ordered_sigs.index("s3"), ordered_sigs.index("s2"))
|
|
|
|
|
|
|
|
# Implied dependencies
|
|
|
|
self.assertLess(ordered_sigs.index("s3"), ordered_sigs.index("s1"))
|
|
|
|
|
|
|
|
def test_multiple_dependencies(self):
|
|
|
|
raw = [
|
2011-02-02 14:02:14 +00:00
|
|
|
("s1", ("s1_db", ["alpha"])),
|
|
|
|
("s2", ("s2_db", ["bravo"])),
|
|
|
|
("s3", ("s3_db", ["charlie"])),
|
|
|
|
("s4", ("s4_db", ["delta"])),
|
2010-12-05 00:44:34 +00:00
|
|
|
]
|
|
|
|
dependencies = {
|
2013-10-26 19:15:03 +00:00
|
|
|
"alpha": ["bravo", "delta"],
|
2010-12-05 00:44:34 +00:00
|
|
|
"bravo": ["charlie"],
|
|
|
|
"delta": ["charlie"],
|
|
|
|
}
|
|
|
|
|
2014-11-15 19:03:31 +00:00
|
|
|
ordered = dependency_ordered(raw, dependencies=dependencies)
|
2013-10-26 19:15:03 +00:00
|
|
|
ordered_sigs = [sig for sig, aliases in ordered]
|
2010-12-05 00:44:34 +00:00
|
|
|
|
|
|
|
self.assertIn("s1", ordered_sigs)
|
|
|
|
self.assertIn("s2", ordered_sigs)
|
|
|
|
self.assertIn("s3", ordered_sigs)
|
|
|
|
self.assertIn("s4", ordered_sigs)
|
|
|
|
|
|
|
|
# Explicit dependencies
|
|
|
|
self.assertLess(ordered_sigs.index("s2"), ordered_sigs.index("s1"))
|
|
|
|
self.assertLess(ordered_sigs.index("s4"), ordered_sigs.index("s1"))
|
|
|
|
self.assertLess(ordered_sigs.index("s3"), ordered_sigs.index("s2"))
|
|
|
|
self.assertLess(ordered_sigs.index("s3"), ordered_sigs.index("s4"))
|
|
|
|
|
|
|
|
# Implicit dependencies
|
|
|
|
self.assertLess(ordered_sigs.index("s3"), ordered_sigs.index("s1"))
|
|
|
|
|
|
|
|
def test_circular_dependencies(self):
|
|
|
|
raw = [
|
2011-02-02 14:02:14 +00:00
|
|
|
("s1", ("s1_db", ["alpha"])),
|
|
|
|
("s2", ("s2_db", ["bravo"])),
|
2010-12-05 00:44:34 +00:00
|
|
|
]
|
|
|
|
dependencies = {
|
|
|
|
"bravo": ["alpha"],
|
|
|
|
"alpha": ["bravo"],
|
|
|
|
}
|
|
|
|
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ImproperlyConfigured):
|
|
|
|
dependency_ordered(raw, dependencies=dependencies)
|
2010-12-05 00:44:34 +00:00
|
|
|
|
2012-04-24 16:05:47 +00:00
|
|
|
def test_own_alias_dependency(self):
|
|
|
|
raw = [("s1", ("s1_db", ["alpha", "bravo"]))]
|
|
|
|
dependencies = {"alpha": ["bravo"]}
|
|
|
|
|
|
|
|
with self.assertRaises(ImproperlyConfigured):
|
2014-11-15 19:03:31 +00:00
|
|
|
dependency_ordered(raw, dependencies=dependencies)
|
2012-04-24 16:05:47 +00:00
|
|
|
|
|
|
|
# reordering aliases shouldn't matter
|
|
|
|
raw = [("s1", ("s1_db", ["bravo", "alpha"]))]
|
|
|
|
|
|
|
|
with self.assertRaises(ImproperlyConfigured):
|
2014-11-15 19:03:31 +00:00
|
|
|
dependency_ordered(raw, dependencies=dependencies)
|
2012-04-24 16:05:47 +00:00
|
|
|
|
2011-06-10 08:26:05 +00:00
|
|
|
|
2017-01-19 07:39:46 +00:00
|
|
|
class MockTestRunner:
|
2011-06-10 08:26:05 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
2021-08-06 06:36:59 +00:00
|
|
|
if parallel := kwargs.get("parallel"):
|
|
|
|
sys.stderr.write(f"parallel={parallel}")
|
2011-06-10 08:26:05 +00:00
|
|
|
|
2016-11-12 17:11:23 +00:00
|
|
|
|
2014-11-29 16:45:06 +00:00
|
|
|
MockTestRunner.run_tests = mock.Mock(return_value=[])
|
2011-06-10 08:26:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ManageCommandTests(unittest.TestCase):
|
|
|
|
def test_custom_test_runner(self):
|
2013-02-26 12:19:18 +00:00
|
|
|
call_command("test", "sites", testrunner="test_runner.tests.MockTestRunner")
|
2014-11-29 16:45:06 +00:00
|
|
|
MockTestRunner.run_tests.assert_called_with(("sites",))
|
2011-06-10 08:26:05 +00:00
|
|
|
|
2013-02-16 12:26:36 +00:00
|
|
|
def test_bad_test_runner(self):
|
|
|
|
with self.assertRaises(AttributeError):
|
2017-02-03 01:43:21 +00:00
|
|
|
call_command("test", "sites", testrunner="test_runner.NonexistentRunner")
|
2013-02-16 12:26:36 +00:00
|
|
|
|
2020-07-22 15:37:52 +00:00
|
|
|
def test_time_recorded(self):
|
|
|
|
with captured_stderr() as stderr:
|
|
|
|
call_command(
|
|
|
|
"test",
|
|
|
|
"--timing",
|
|
|
|
"sites",
|
|
|
|
testrunner="test_runner.tests.MockTestRunner",
|
|
|
|
)
|
|
|
|
self.assertIn("Total run took", stderr.getvalue())
|
|
|
|
|
2011-06-10 08:26:05 +00:00
|
|
|
|
2021-08-06 06:36:59 +00:00
|
|
|
# Isolate from the real environment.
|
|
|
|
@mock.patch.dict(os.environ, {}, clear=True)
|
|
|
|
@mock.patch.object(multiprocessing, "cpu_count", return_value=12)
|
|
|
|
class ManageCommandParallelTests(SimpleTestCase):
|
|
|
|
def test_parallel_default(self, *mocked_objects):
|
|
|
|
with captured_stderr() as stderr:
|
|
|
|
call_command(
|
|
|
|
"test",
|
|
|
|
"--parallel",
|
|
|
|
testrunner="test_runner.tests.MockTestRunner",
|
|
|
|
)
|
|
|
|
self.assertIn("parallel=12", stderr.getvalue())
|
|
|
|
|
|
|
|
def test_parallel_auto(self, *mocked_objects):
|
|
|
|
with captured_stderr() as stderr:
|
|
|
|
call_command(
|
|
|
|
"test",
|
|
|
|
"--parallel=auto",
|
|
|
|
testrunner="test_runner.tests.MockTestRunner",
|
|
|
|
)
|
|
|
|
self.assertIn("parallel=12", stderr.getvalue())
|
|
|
|
|
|
|
|
def test_no_parallel(self, *mocked_objects):
|
|
|
|
with captured_stderr() as stderr:
|
|
|
|
call_command("test", testrunner="test_runner.tests.MockTestRunner")
|
|
|
|
# Parallel is disabled by default.
|
|
|
|
self.assertEqual(stderr.getvalue(), "")
|
|
|
|
|
2022-02-12 20:40:12 +00:00
|
|
|
@mock.patch.object(multiprocessing, "get_start_method", return_value="spawn")
|
|
|
|
def test_parallel_spawn(self, *mocked_objects):
|
2021-08-06 06:36:59 +00:00
|
|
|
with captured_stderr() as stderr:
|
|
|
|
call_command(
|
|
|
|
"test",
|
|
|
|
"--parallel=auto",
|
|
|
|
testrunner="test_runner.tests.MockTestRunner",
|
|
|
|
)
|
|
|
|
self.assertIn("parallel=1", stderr.getvalue())
|
|
|
|
|
2022-02-12 20:40:12 +00:00
|
|
|
@mock.patch.object(multiprocessing, "get_start_method", return_value="spawn")
|
|
|
|
def test_no_parallel_spawn(self, *mocked_objects):
|
2021-08-06 06:36:59 +00:00
|
|
|
with captured_stderr() as stderr:
|
|
|
|
call_command(
|
|
|
|
"test",
|
|
|
|
testrunner="test_runner.tests.MockTestRunner",
|
|
|
|
)
|
|
|
|
self.assertEqual(stderr.getvalue(), "")
|
|
|
|
|
2021-08-04 08:49:30 +00:00
|
|
|
@mock.patch.dict(os.environ, {"DJANGO_TEST_PROCESSES": "7"})
|
|
|
|
def test_no_parallel_django_test_processes_env(self, *mocked_objects):
|
|
|
|
with captured_stderr() as stderr:
|
|
|
|
call_command("test", testrunner="test_runner.tests.MockTestRunner")
|
|
|
|
self.assertEqual(stderr.getvalue(), "")
|
|
|
|
|
2021-08-06 06:36:59 +00:00
|
|
|
@mock.patch.dict(os.environ, {"DJANGO_TEST_PROCESSES": "invalid"})
|
|
|
|
def test_django_test_processes_env_non_int(self, *mocked_objects):
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
call_command(
|
|
|
|
"test",
|
|
|
|
"--parallel",
|
|
|
|
testrunner="test_runner.tests.MockTestRunner",
|
|
|
|
)
|
|
|
|
|
2021-08-04 08:49:30 +00:00
|
|
|
@mock.patch.dict(os.environ, {"DJANGO_TEST_PROCESSES": "7"})
|
|
|
|
def test_django_test_processes_parallel_default(self, *mocked_objects):
|
|
|
|
for parallel in ["--parallel", "--parallel=auto"]:
|
|
|
|
with self.subTest(parallel=parallel):
|
|
|
|
with captured_stderr() as stderr:
|
|
|
|
call_command(
|
|
|
|
"test",
|
|
|
|
parallel,
|
|
|
|
testrunner="test_runner.tests.MockTestRunner",
|
|
|
|
)
|
|
|
|
self.assertIn("parallel=7", stderr.getvalue())
|
|
|
|
|
2021-08-06 06:36:59 +00:00
|
|
|
|
2018-08-23 13:22:05 +00:00
|
|
|
class CustomTestRunnerOptionsSettingsTests(AdminScriptTestCase):
|
|
|
|
"""
|
|
|
|
Custom runners can add command line arguments. The runner is specified
|
|
|
|
through a settings file.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2011-06-10 08:26:05 +00:00
|
|
|
def setUp(self):
|
2019-02-05 17:11:54 +00:00
|
|
|
super().setUp()
|
2011-06-10 08:26:05 +00:00
|
|
|
settings = {
|
2015-02-09 21:41:57 +00:00
|
|
|
"TEST_RUNNER": "'test_runner.runner.CustomOptionsTestRunner'",
|
2011-06-10 08:26:05 +00:00
|
|
|
}
|
|
|
|
self.write_settings("settings.py", sdict=settings)
|
|
|
|
|
|
|
|
def test_default_options(self):
|
2013-02-26 15:02:24 +00:00
|
|
|
args = ["test", "--settings=test_project.settings"]
|
2011-06-10 08:26:05 +00:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(err)
|
|
|
|
self.assertOutput(out, "1:2:3")
|
|
|
|
|
|
|
|
def test_default_and_given_options(self):
|
2013-02-26 15:02:24 +00:00
|
|
|
args = ["test", "--settings=test_project.settings", "--option_b=foo"]
|
2011-06-10 08:26:05 +00:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(err)
|
|
|
|
self.assertOutput(out, "1:foo:3")
|
|
|
|
|
|
|
|
def test_option_name_and_value_separated(self):
|
2013-02-26 15:02:24 +00:00
|
|
|
args = ["test", "--settings=test_project.settings", "--option_b", "foo"]
|
2011-06-10 08:26:05 +00:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(err)
|
|
|
|
self.assertOutput(out, "1:foo:3")
|
|
|
|
|
|
|
|
def test_all_options_given(self):
|
2013-02-26 15:02:24 +00:00
|
|
|
args = [
|
|
|
|
"test",
|
|
|
|
"--settings=test_project.settings",
|
|
|
|
"--option_a=bar",
|
|
|
|
"--option_b=foo",
|
|
|
|
"--option_c=31337",
|
|
|
|
]
|
2011-06-10 08:26:05 +00:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(err)
|
|
|
|
self.assertOutput(out, "bar:foo:31337")
|
2011-06-12 12:34:10 +00:00
|
|
|
|
|
|
|
|
2018-08-23 13:22:05 +00:00
|
|
|
class CustomTestRunnerOptionsCmdlineTests(AdminScriptTestCase):
|
|
|
|
"""
|
|
|
|
Custom runners can add command line arguments when the runner is specified
|
|
|
|
using --testrunner.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2018-08-23 13:22:05 +00:00
|
|
|
def setUp(self):
|
2019-02-05 17:11:54 +00:00
|
|
|
super().setUp()
|
2018-08-23 13:22:05 +00:00
|
|
|
self.write_settings("settings.py")
|
|
|
|
|
2018-08-16 19:49:03 +00:00
|
|
|
def test_testrunner_option(self):
|
|
|
|
args = [
|
|
|
|
"test",
|
|
|
|
"--testrunner",
|
|
|
|
"test_runner.runner.CustomOptionsTestRunner",
|
|
|
|
"--option_a=bar",
|
|
|
|
"--option_b=foo",
|
|
|
|
"--option_c=31337",
|
|
|
|
]
|
|
|
|
out, err = self.run_django_admin(args, "test_project.settings")
|
|
|
|
self.assertNoOutput(err)
|
|
|
|
self.assertOutput(out, "bar:foo:31337")
|
|
|
|
|
2018-08-23 13:22:05 +00:00
|
|
|
def test_testrunner_equals(self):
|
|
|
|
args = [
|
|
|
|
"test",
|
|
|
|
"--testrunner=test_runner.runner.CustomOptionsTestRunner",
|
|
|
|
"--option_a=bar",
|
|
|
|
"--option_b=foo",
|
|
|
|
"--option_c=31337",
|
|
|
|
]
|
|
|
|
out, err = self.run_django_admin(args, "test_project.settings")
|
|
|
|
self.assertNoOutput(err)
|
|
|
|
self.assertOutput(out, "bar:foo:31337")
|
|
|
|
|
|
|
|
def test_no_testrunner(self):
|
|
|
|
args = ["test", "--testrunner"]
|
|
|
|
out, err = self.run_django_admin(args, "test_project.settings")
|
|
|
|
self.assertIn("usage", err)
|
|
|
|
self.assertNotIn("Traceback", err)
|
|
|
|
self.assertNoOutput(out)
|
|
|
|
|
|
|
|
|
2022-03-16 20:47:58 +00:00
|
|
|
class NoInitializeSuiteTestRunnerTests(SimpleTestCase):
|
|
|
|
@mock.patch.object(multiprocessing, "get_start_method", return_value="spawn")
|
|
|
|
@mock.patch(
|
|
|
|
"django.test.runner.ParallelTestSuite.initialize_suite",
|
|
|
|
side_effect=Exception("initialize_suite() is called."),
|
|
|
|
)
|
|
|
|
def test_no_initialize_suite_test_runner(self, *mocked_objects):
|
|
|
|
"""
|
|
|
|
The test suite's initialize_suite() method must always be called when
|
|
|
|
using spawn. It cannot rely on a test runner implementation.
|
|
|
|
"""
|
|
|
|
|
|
|
|
class NoInitializeSuiteTestRunner(DiscoverRunner):
|
|
|
|
def setup_test_environment(self, **kwargs):
|
|
|
|
return
|
|
|
|
|
|
|
|
def setup_databases(self, **kwargs):
|
|
|
|
return
|
|
|
|
|
|
|
|
def run_checks(self, databases):
|
|
|
|
return
|
|
|
|
|
|
|
|
def teardown_databases(self, old_config, **kwargs):
|
|
|
|
return
|
|
|
|
|
|
|
|
def teardown_test_environment(self, **kwargs):
|
|
|
|
return
|
|
|
|
|
|
|
|
def run_suite(self, suite, **kwargs):
|
|
|
|
kwargs = self.get_test_runner_kwargs()
|
|
|
|
runner = self.test_runner(**kwargs)
|
|
|
|
return runner.run(suite)
|
|
|
|
|
|
|
|
with self.assertRaisesMessage(Exception, "initialize_suite() is called."):
|
|
|
|
runner = NoInitializeSuiteTestRunner(
|
|
|
|
verbosity=0, interactive=False, parallel=2
|
|
|
|
)
|
|
|
|
runner.run_tests(
|
|
|
|
[
|
|
|
|
"test_runner_apps.sample.tests_sample.TestDjangoTestCase",
|
|
|
|
"test_runner_apps.simple.tests",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-05-19 08:20:51 +00:00
|
|
|
class TestRunnerInitializerTests(SimpleTestCase):
|
|
|
|
|
|
|
|
# Raise an exception to don't actually run tests.
|
|
|
|
@mock.patch.object(
|
|
|
|
multiprocessing, "Pool", side_effect=Exception("multiprocessing.Pool()")
|
|
|
|
)
|
|
|
|
def test_no_initialize_suite_test_runner(self, mocked_pool):
|
|
|
|
class StubTestRunner(DiscoverRunner):
|
|
|
|
def setup_test_environment(self, **kwargs):
|
|
|
|
return
|
|
|
|
|
|
|
|
def setup_databases(self, **kwargs):
|
|
|
|
return
|
|
|
|
|
|
|
|
def run_checks(self, databases):
|
|
|
|
return
|
|
|
|
|
|
|
|
def teardown_databases(self, old_config, **kwargs):
|
|
|
|
return
|
|
|
|
|
|
|
|
def teardown_test_environment(self, **kwargs):
|
|
|
|
return
|
|
|
|
|
|
|
|
def run_suite(self, suite, **kwargs):
|
|
|
|
kwargs = self.get_test_runner_kwargs()
|
|
|
|
runner = self.test_runner(**kwargs)
|
|
|
|
return runner.run(suite)
|
|
|
|
|
|
|
|
runner = StubTestRunner(verbosity=0, interactive=False, parallel=2)
|
|
|
|
with self.assertRaisesMessage(Exception, "multiprocessing.Pool()"):
|
|
|
|
runner.run_tests(
|
|
|
|
[
|
|
|
|
"test_runner_apps.sample.tests_sample.TestDjangoTestCase",
|
|
|
|
"test_runner_apps.simple.tests",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
# Initializer must be a function.
|
|
|
|
self.assertIs(mocked_pool.call_args.kwargs["initializer"], _init_worker)
|
|
|
|
|
|
|
|
|
2011-12-29 01:18:30 +00:00
|
|
|
class Ticket17477RegressionTests(AdminScriptTestCase):
|
|
|
|
def setUp(self):
|
2019-02-05 17:11:54 +00:00
|
|
|
super().setUp()
|
2011-12-29 01:18:30 +00:00
|
|
|
self.write_settings("settings.py")
|
|
|
|
|
|
|
|
def test_ticket_17477(self):
|
|
|
|
"""'manage.py help test' works after r16352."""
|
|
|
|
args = ["help", "test"]
|
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(err)
|
|
|
|
|
|
|
|
|
2018-12-22 23:11:36 +00:00
|
|
|
class SQLiteInMemoryTestDbs(TransactionTestCase):
|
2018-12-08 00:55:47 +00:00
|
|
|
available_apps = ["test_runner"]
|
2018-07-12 04:12:20 +00:00
|
|
|
databases = {"default", "other"}
|
2013-06-14 21:11:51 +00:00
|
|
|
|
2012-03-14 10:58:23 +00:00
|
|
|
@unittest.skipUnless(
|
|
|
|
all(db.connections[conn].vendor == "sqlite" for conn in db.connections),
|
2013-09-05 22:23:48 +00:00
|
|
|
"This is an sqlite-specific issue",
|
|
|
|
)
|
2012-03-13 17:52:48 +00:00
|
|
|
def test_transaction_support(self):
|
2018-12-24 18:23:47 +00:00
|
|
|
# Assert connections mocking is appropriately applied by preventing
|
|
|
|
# any attempts at calling create_test_db on the global connection
|
|
|
|
# objects.
|
|
|
|
for connection in db.connections.all():
|
|
|
|
create_test_db = mock.patch.object(
|
|
|
|
connection.creation,
|
|
|
|
"create_test_db",
|
|
|
|
side_effect=AssertionError(
|
|
|
|
"Global connection object shouldn't be manipulated."
|
2022-02-03 19:24:19 +00:00
|
|
|
),
|
2018-12-24 18:23:47 +00:00
|
|
|
)
|
|
|
|
create_test_db.start()
|
|
|
|
self.addCleanup(create_test_db.stop)
|
2014-03-09 20:37:05 +00:00
|
|
|
for option_key, option_value in (
|
|
|
|
("NAME", ":memory:"),
|
|
|
|
("TEST", {"NAME": ":memory:"}),
|
|
|
|
):
|
2015-05-09 14:33:58 +00:00
|
|
|
tested_connections = db.ConnectionHandler(
|
|
|
|
{
|
|
|
|
"default": {
|
|
|
|
"ENGINE": "django.db.backends.sqlite3",
|
|
|
|
option_key: option_value,
|
|
|
|
},
|
|
|
|
"other": {
|
|
|
|
"ENGINE": "django.db.backends.sqlite3",
|
|
|
|
option_key: option_value,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
2018-12-24 18:23:47 +00:00
|
|
|
with mock.patch("django.test.utils.connections", new=tested_connections):
|
|
|
|
other = tested_connections["other"]
|
|
|
|
DiscoverRunner(verbosity=0).setup_databases()
|
|
|
|
msg = (
|
|
|
|
"DATABASES setting '%s' option set to sqlite3's ':memory:' value "
|
|
|
|
"shouldn't interfere with transaction support detection."
|
|
|
|
% option_key
|
|
|
|
)
|
|
|
|
# Transaction support is properly initialized for the 'other' DB.
|
|
|
|
self.assertTrue(other.features.supports_transactions, msg)
|
|
|
|
# And all the DBs report that they support transactions.
|
|
|
|
self.assertTrue(connections_support_transactions(), msg)
|
2012-04-24 16:47:31 +00:00
|
|
|
|
|
|
|
|
2013-01-04 12:55:20 +00:00
|
|
|
class DummyBackendTest(unittest.TestCase):
|
|
|
|
def test_setup_databases(self):
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
setup_databases() doesn't fail with dummy database backend.
|
2013-01-04 12:55:20 +00:00
|
|
|
"""
|
2015-05-09 14:33:58 +00:00
|
|
|
tested_connections = db.ConnectionHandler({})
|
2016-07-02 22:20:14 +00:00
|
|
|
with mock.patch("django.test.utils.connections", new=tested_connections):
|
2015-05-09 14:33:58 +00:00
|
|
|
runner_instance = DiscoverRunner(verbosity=0)
|
2016-06-28 15:21:26 +00:00
|
|
|
old_config = runner_instance.setup_databases()
|
|
|
|
runner_instance.teardown_databases(old_config)
|
2013-07-04 23:58:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AliasedDefaultTestSetupTest(unittest.TestCase):
|
|
|
|
def test_setup_aliased_default_database(self):
|
|
|
|
"""
|
2019-08-05 15:47:50 +00:00
|
|
|
setup_databases() doesn't fail when 'default' is aliased
|
2013-07-04 23:58:58 +00:00
|
|
|
"""
|
2015-05-09 14:33:58 +00:00
|
|
|
tested_connections = db.ConnectionHandler(
|
|
|
|
{"default": {"NAME": "dummy"}, "aliased": {"NAME": "dummy"}}
|
|
|
|
)
|
2016-07-02 22:20:14 +00:00
|
|
|
with mock.patch("django.test.utils.connections", new=tested_connections):
|
2015-05-09 14:33:58 +00:00
|
|
|
runner_instance = DiscoverRunner(verbosity=0)
|
2016-06-28 15:21:26 +00:00
|
|
|
old_config = runner_instance.setup_databases()
|
|
|
|
runner_instance.teardown_databases(old_config)
|
2013-01-04 12:55:20 +00:00
|
|
|
|
|
|
|
|
2021-02-15 03:42:47 +00:00
|
|
|
class SetupDatabasesTests(SimpleTestCase):
|
2014-09-15 15:17:12 +00:00
|
|
|
def setUp(self):
|
2014-11-15 19:03:31 +00:00
|
|
|
self.runner_instance = DiscoverRunner(verbosity=0)
|
2013-07-13 21:46:05 +00:00
|
|
|
|
2014-09-15 15:17:12 +00:00
|
|
|
def test_setup_aliased_databases(self):
|
2015-05-09 14:33:58 +00:00
|
|
|
tested_connections = db.ConnectionHandler(
|
|
|
|
{
|
2014-09-15 15:17:12 +00:00
|
|
|
"default": {
|
|
|
|
"ENGINE": "django.db.backends.dummy",
|
|
|
|
"NAME": "dbname",
|
|
|
|
},
|
|
|
|
"other": {
|
|
|
|
"ENGINE": "django.db.backends.dummy",
|
|
|
|
"NAME": "dbname",
|
2022-02-03 19:24:19 +00:00
|
|
|
},
|
2014-09-15 15:17:12 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-09-08 20:33:36 +00:00
|
|
|
with mock.patch(
|
|
|
|
"django.db.backends.dummy.base.DatabaseWrapper.creation_class"
|
|
|
|
) as mocked_db_creation:
|
2016-07-02 22:20:14 +00:00
|
|
|
with mock.patch("django.test.utils.connections", new=tested_connections):
|
2015-05-09 14:33:58 +00:00
|
|
|
old_config = self.runner_instance.setup_databases()
|
|
|
|
self.runner_instance.teardown_databases(old_config)
|
|
|
|
mocked_db_creation.return_value.destroy_test_db.assert_called_once_with(
|
|
|
|
"dbname", 0, False
|
|
|
|
)
|
2014-09-15 15:17:12 +00:00
|
|
|
|
2021-02-15 13:42:00 +00:00
|
|
|
def test_setup_test_database_aliases(self):
|
|
|
|
"""
|
|
|
|
The default database must be the first because data migrations
|
|
|
|
use the default alias by default.
|
|
|
|
"""
|
|
|
|
tested_connections = db.ConnectionHandler(
|
|
|
|
{
|
|
|
|
"other": {
|
|
|
|
"ENGINE": "django.db.backends.dummy",
|
|
|
|
"NAME": "dbname",
|
|
|
|
},
|
|
|
|
"default": {
|
|
|
|
"ENGINE": "django.db.backends.dummy",
|
|
|
|
"NAME": "dbname",
|
2022-02-03 19:24:19 +00:00
|
|
|
},
|
2021-02-15 13:42:00 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
with mock.patch("django.test.utils.connections", new=tested_connections):
|
|
|
|
test_databases, _ = get_unique_databases_and_mirrors()
|
|
|
|
self.assertEqual(
|
|
|
|
test_databases,
|
|
|
|
{
|
|
|
|
("", "", "django.db.backends.dummy", "test_dbname"): (
|
|
|
|
"dbname",
|
|
|
|
["default", "other"],
|
|
|
|
),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2014-10-24 12:53:58 +00:00
|
|
|
def test_destroy_test_db_restores_db_name(self):
|
2015-05-09 14:33:58 +00:00
|
|
|
tested_connections = db.ConnectionHandler(
|
|
|
|
{
|
2014-10-24 12:53:58 +00:00
|
|
|
"default": {
|
|
|
|
"ENGINE": settings.DATABASES[db.DEFAULT_DB_ALIAS]["ENGINE"],
|
|
|
|
"NAME": "xxx_test_database",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
# Using the real current name as old_name to not mess with the test suite.
|
|
|
|
old_name = settings.DATABASES[db.DEFAULT_DB_ALIAS]["NAME"]
|
2015-05-09 14:33:58 +00:00
|
|
|
with mock.patch("django.db.connections", new=tested_connections):
|
|
|
|
tested_connections["default"].creation.destroy_test_db(
|
|
|
|
old_name, verbosity=0, keepdb=True
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
tested_connections["default"].settings_dict["NAME"], old_name
|
|
|
|
)
|
2014-10-24 12:53:58 +00:00
|
|
|
|
2014-09-15 15:17:12 +00:00
|
|
|
def test_serialization(self):
|
2015-05-09 14:33:58 +00:00
|
|
|
tested_connections = db.ConnectionHandler(
|
|
|
|
{
|
2014-09-15 15:17:12 +00:00
|
|
|
"default": {
|
|
|
|
"ENGINE": "django.db.backends.dummy",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
2016-09-08 20:33:36 +00:00
|
|
|
with mock.patch(
|
|
|
|
"django.db.backends.dummy.base.DatabaseWrapper.creation_class"
|
|
|
|
) as mocked_db_creation:
|
2016-07-02 22:20:14 +00:00
|
|
|
with mock.patch("django.test.utils.connections", new=tested_connections):
|
2015-05-09 14:33:58 +00:00
|
|
|
self.runner_instance.setup_databases()
|
|
|
|
mocked_db_creation.return_value.create_test_db.assert_called_once_with(
|
2015-02-09 21:00:09 +00:00
|
|
|
verbosity=0, autoclobber=False, serialize=True, keepdb=False
|
2015-05-09 14:33:58 +00:00
|
|
|
)
|
2014-09-15 15:17:12 +00:00
|
|
|
|
|
|
|
def test_serialized_off(self):
|
2015-05-09 14:33:58 +00:00
|
|
|
tested_connections = db.ConnectionHandler(
|
|
|
|
{
|
2014-09-15 15:17:12 +00:00
|
|
|
"default": {
|
|
|
|
"ENGINE": "django.db.backends.dummy",
|
|
|
|
"TEST": {"SERIALIZE": False},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
2021-02-15 03:42:47 +00:00
|
|
|
msg = (
|
|
|
|
"The SERIALIZE test database setting is deprecated as it can be "
|
|
|
|
"inferred from the TestCase/TransactionTestCase.databases that "
|
|
|
|
"enable the serialized_rollback feature."
|
|
|
|
)
|
2016-09-08 20:33:36 +00:00
|
|
|
with mock.patch(
|
|
|
|
"django.db.backends.dummy.base.DatabaseWrapper.creation_class"
|
|
|
|
) as mocked_db_creation:
|
2016-07-02 22:20:14 +00:00
|
|
|
with mock.patch("django.test.utils.connections", new=tested_connections):
|
2021-02-15 03:42:47 +00:00
|
|
|
with self.assertWarnsMessage(RemovedInDjango50Warning, msg):
|
|
|
|
self.runner_instance.setup_databases()
|
2015-05-09 14:33:58 +00:00
|
|
|
mocked_db_creation.return_value.create_test_db.assert_called_once_with(
|
2015-02-09 21:00:09 +00:00
|
|
|
verbosity=0, autoclobber=False, serialize=False, keepdb=False
|
2015-05-09 14:33:58 +00:00
|
|
|
)
|
2013-07-13 21:46:05 +00:00
|
|
|
|
|
|
|
|
2018-07-11 20:37:56 +00:00
|
|
|
@skipUnlessDBFeature("supports_sequence_reset")
|
2012-04-24 16:47:31 +00:00
|
|
|
class AutoIncrementResetTest(TransactionTestCase):
|
|
|
|
"""
|
2018-07-11 20:37:56 +00:00
|
|
|
Creating the same models in different test methods receive the same PK
|
|
|
|
values since the sequences are reset before each test method.
|
2012-04-24 16:47:31 +00:00
|
|
|
"""
|
2012-07-24 20:24:16 +00:00
|
|
|
|
2013-06-04 06:09:29 +00:00
|
|
|
available_apps = ["test_runner"]
|
|
|
|
|
2012-07-24 20:24:16 +00:00
|
|
|
reset_sequences = True
|
|
|
|
|
2018-07-11 20:37:56 +00:00
|
|
|
def _test(self):
|
|
|
|
# Regular model
|
2012-04-24 16:47:31 +00:00
|
|
|
p = Person.objects.create(first_name="Jack", last_name="Smith")
|
2012-05-03 14:39:16 +00:00
|
|
|
self.assertEqual(p.pk, 1)
|
2018-07-11 08:18:41 +00:00
|
|
|
# Auto-created many-to-many through model
|
|
|
|
p.friends.add(Person.objects.create(first_name="Jacky", last_name="Smith"))
|
|
|
|
self.assertEqual(p.friends.through.objects.first().pk, 1)
|
2018-07-11 20:37:56 +00:00
|
|
|
# Many-to-many through model
|
|
|
|
b = B.objects.create()
|
|
|
|
t = Through.objects.create(person=p, b=b)
|
|
|
|
self.assertEqual(t.pk, 1)
|
|
|
|
|
|
|
|
def test_autoincrement_reset1(self):
|
|
|
|
self._test()
|
2012-04-24 16:47:31 +00:00
|
|
|
|
|
|
|
def test_autoincrement_reset2(self):
|
2018-07-11 20:37:56 +00:00
|
|
|
self._test()
|
2015-03-12 16:17:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EmptyDefaultDatabaseTest(unittest.TestCase):
|
|
|
|
def test_empty_default_database(self):
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
An empty default database in settings does not raise an ImproperlyConfigured
|
2015-03-12 16:17:15 +00:00
|
|
|
error when running a unit test that does not use a database.
|
|
|
|
"""
|
2020-12-10 17:48:07 +00:00
|
|
|
tested_connections = db.ConnectionHandler({"default": {}})
|
|
|
|
with mock.patch("django.db.connections", new=tested_connections):
|
|
|
|
connection = tested_connections[db.utils.DEFAULT_DB_ALIAS]
|
|
|
|
self.assertEqual(
|
|
|
|
connection.settings_dict["ENGINE"], "django.db.backends.dummy"
|
|
|
|
)
|
|
|
|
connections_support_transactions()
|
2018-10-18 23:04:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RunTestsExceptionHandlingTests(unittest.TestCase):
|
|
|
|
def test_run_checks_raises(self):
|
|
|
|
"""
|
|
|
|
Teardown functions are run when run_checks() raises SystemCheckError.
|
|
|
|
"""
|
|
|
|
with mock.patch(
|
|
|
|
"django.test.runner.DiscoverRunner.setup_test_environment"
|
|
|
|
), mock.patch("django.test.runner.DiscoverRunner.setup_databases"), mock.patch(
|
|
|
|
"django.test.runner.DiscoverRunner.build_suite"
|
|
|
|
), mock.patch(
|
|
|
|
"django.test.runner.DiscoverRunner.run_checks", side_effect=SystemCheckError
|
|
|
|
), mock.patch(
|
|
|
|
"django.test.runner.DiscoverRunner.teardown_databases"
|
|
|
|
) as teardown_databases, mock.patch(
|
|
|
|
"django.test.runner.DiscoverRunner.teardown_test_environment"
|
|
|
|
) as teardown_test_environment:
|
|
|
|
runner = DiscoverRunner(verbosity=0, interactive=False)
|
|
|
|
with self.assertRaises(SystemCheckError):
|
|
|
|
runner.run_tests(
|
|
|
|
["test_runner_apps.sample.tests_sample.TestDjangoTestCase"]
|
|
|
|
)
|
|
|
|
self.assertTrue(teardown_databases.called)
|
|
|
|
self.assertTrue(teardown_test_environment.called)
|
|
|
|
|
|
|
|
def test_run_checks_raises_and_teardown_raises(self):
|
|
|
|
"""
|
|
|
|
SystemCheckError is surfaced when run_checks() raises SystemCheckError
|
|
|
|
and teardown databases() raises ValueError.
|
|
|
|
"""
|
|
|
|
with mock.patch(
|
|
|
|
"django.test.runner.DiscoverRunner.setup_test_environment"
|
|
|
|
), mock.patch("django.test.runner.DiscoverRunner.setup_databases"), mock.patch(
|
|
|
|
"django.test.runner.DiscoverRunner.build_suite"
|
|
|
|
), mock.patch(
|
|
|
|
"django.test.runner.DiscoverRunner.run_checks", side_effect=SystemCheckError
|
|
|
|
), mock.patch(
|
|
|
|
"django.test.runner.DiscoverRunner.teardown_databases",
|
|
|
|
side_effect=ValueError,
|
|
|
|
) as teardown_databases, mock.patch(
|
|
|
|
"django.test.runner.DiscoverRunner.teardown_test_environment"
|
|
|
|
) as teardown_test_environment:
|
|
|
|
runner = DiscoverRunner(verbosity=0, interactive=False)
|
|
|
|
with self.assertRaises(SystemCheckError):
|
|
|
|
runner.run_tests(
|
|
|
|
["test_runner_apps.sample.tests_sample.TestDjangoTestCase"]
|
|
|
|
)
|
|
|
|
self.assertTrue(teardown_databases.called)
|
|
|
|
self.assertFalse(teardown_test_environment.called)
|
|
|
|
|
|
|
|
def test_run_checks_passes_and_teardown_raises(self):
|
|
|
|
"""
|
|
|
|
Exceptions on teardown are surfaced if no exceptions happen during
|
|
|
|
run_checks().
|
|
|
|
"""
|
|
|
|
with mock.patch(
|
|
|
|
"django.test.runner.DiscoverRunner.setup_test_environment"
|
|
|
|
), mock.patch("django.test.runner.DiscoverRunner.setup_databases"), mock.patch(
|
|
|
|
"django.test.runner.DiscoverRunner.build_suite"
|
|
|
|
), mock.patch(
|
|
|
|
"django.test.runner.DiscoverRunner.run_checks"
|
|
|
|
), mock.patch(
|
|
|
|
"django.test.runner.DiscoverRunner.teardown_databases",
|
|
|
|
side_effect=ValueError,
|
|
|
|
) as teardown_databases, mock.patch(
|
|
|
|
"django.test.runner.DiscoverRunner.teardown_test_environment"
|
|
|
|
) as teardown_test_environment:
|
|
|
|
runner = DiscoverRunner(verbosity=0, interactive=False)
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
# Suppress the output when running TestDjangoTestCase.
|
|
|
|
with mock.patch("sys.stderr"):
|
|
|
|
runner.run_tests(
|
|
|
|
["test_runner_apps.sample.tests_sample.TestDjangoTestCase"]
|
|
|
|
)
|
|
|
|
self.assertTrue(teardown_databases.called)
|
|
|
|
self.assertFalse(teardown_test_environment.called)
|
2021-07-15 21:56:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
# RemovedInDjango50Warning
|
|
|
|
class NoOpTestRunner(DiscoverRunner):
|
|
|
|
def setup_test_environment(self, **kwargs):
|
|
|
|
return
|
|
|
|
|
|
|
|
def setup_databases(self, **kwargs):
|
|
|
|
return
|
|
|
|
|
|
|
|
def run_checks(self, databases):
|
|
|
|
return
|
|
|
|
|
|
|
|
def teardown_databases(self, old_config, **kwargs):
|
|
|
|
return
|
|
|
|
|
|
|
|
def teardown_test_environment(self, **kwargs):
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
class DiscoverRunnerExtraTestsDeprecationTests(SimpleTestCase):
|
|
|
|
msg = "The extra_tests argument is deprecated."
|
|
|
|
|
|
|
|
def get_runner(self):
|
|
|
|
return NoOpTestRunner(verbosity=0, interactive=False)
|
|
|
|
|
|
|
|
def test_extra_tests_build_suite(self):
|
|
|
|
runner = self.get_runner()
|
|
|
|
with self.assertWarnsMessage(RemovedInDjango50Warning, self.msg):
|
|
|
|
runner.build_suite(extra_tests=[])
|
|
|
|
|
|
|
|
def test_extra_tests_run_tests(self):
|
|
|
|
runner = self.get_runner()
|
|
|
|
with captured_stderr():
|
|
|
|
with self.assertWarnsMessage(RemovedInDjango50Warning, self.msg):
|
|
|
|
runner.run_tests(
|
|
|
|
test_labels=["test_runner_apps.sample.tests_sample.EmptyTestCase"],
|
|
|
|
extra_tests=[],
|
|
|
|
)
|