| 1 |
""" |
| 2 |
Tests for django test runner |
| 3 |
""" |
| 4 |
|
| 5 |
import collections.abc |
| 6 |
import multiprocessing |
| 7 |
import os |
| 8 |
import sys |
| 9 |
import unittest |
| 10 |
from unittest import mock |
| 11 |
|
| 12 |
from admin_scripts.tests import AdminScriptTestCase |
| 13 |
|
| 14 |
from django import db |
| 15 |
from django.conf import settings |
| 16 |
from django.core.exceptions import ImproperlyConfigured |
| 17 |
from django.core.management import call_command |
| 18 |
from django.core.management.base import CommandError, SystemCheckError |
| 19 |
from django.test import SimpleTestCase, TransactionTestCase, skipUnlessDBFeature |
| 20 |
from django.test.runner import ( |
| 21 |
DiscoverRunner, |
| 22 |
Shuffler, |
| 23 |
_init_worker, |
| 24 |
reorder_test_bin, |
| 25 |
reorder_tests, |
| 26 |
shuffle_tests, |
| 27 |
) |
| 28 |
from django.test.testcases import connections_support_transactions |
| 29 |
from django.test.utils import ( |
| 30 |
captured_stderr, |
| 31 |
dependency_ordered, |
| 32 |
get_unique_databases_and_mirrors, |
| 33 |
iter_test_cases, |
| 34 |
) |
| 35 |
from django.utils.version import PY312 |
| 36 |
|
| 37 |
from .models import B, Person, Through |
| 38 |
|
| 39 |
|
| 40 |
class MySuite: |
| 41 |
def __init__(self): |
| 42 |
self.tests = [] |
| 43 |
|
| 44 |
def addTest(self, test): |
| 45 |
self.tests.append(test) |
| 46 |
|
| 47 |
def __iter__(self): |
| 48 |
yield from self.tests |
| 49 |
|
| 50 |
|
| 51 |
class TestSuiteTests(SimpleTestCase): |
| 52 |
def build_test_suite(self, test_classes, suite=None, suite_class=None): |
| 53 |
if suite_class is None: |
| 54 |
suite_class = unittest.TestSuite |
| 55 |
if suite is None: |
| 56 |
suite = suite_class() |
| 57 |
|
| 58 |
loader = unittest.defaultTestLoader |
| 59 |
for test_class in test_classes: |
| 60 |
tests = loader.loadTestsFromTestCase(test_class) |
| 61 |
subsuite = suite_class() |
| 62 |
# Only use addTest() to simplify testing a custom TestSuite. |
| 63 |
for test in tests: |
| 64 |
subsuite.addTest(test) |
| 65 |
suite.addTest(subsuite) |
| 66 |
|
| 67 |
return suite |
| 68 |
|
| 69 |
def make_test_suite(self, suite=None, suite_class=None): |
| 70 |
class Tests1(unittest.TestCase): |
| 71 |
def test1(self): |
| 72 |
pass |
| 73 |
|
| 74 |
def test2(self): |
| 75 |
pass |
| 76 |
|
| 77 |
class Tests2(unittest.TestCase): |
| 78 |
def test1(self): |
| 79 |
pass |
| 80 |
|
| 81 |
def test2(self): |
| 82 |
pass |
| 83 |
|
| 84 |
return self.build_test_suite( |
| 85 |
(Tests1, Tests2), |
| 86 |
suite=suite, |
| 87 |
suite_class=suite_class, |
| 88 |
) |
| 89 |
|
| 90 |
def assertTestNames(self, tests, expected): |
| 91 |
# Each test.id() has a form like the following: |
| 92 |
# "test_runner.tests.IterTestCasesTests.test_iter_test_cases.<locals>.Tests1.test1". |
| 93 |
# It suffices to check only the last two parts. |
| 94 |
names = [".".join(test.id().split(".")[-2:]) for test in tests] |
| 95 |
self.assertEqual(names, expected) |
| 96 |
|
| 97 |
def test_iter_test_cases_basic(self): |
| 98 |
suite = self.make_test_suite() |
| 99 |
tests = iter_test_cases(suite) |
| 100 |
self.assertTestNames( |
| 101 |
tests, |
| 102 |
expected=[ |
| 103 |
"Tests1.test1", |
| 104 |
"Tests1.test2", |
| 105 |
"Tests2.test1", |
| 106 |
"Tests2.test2", |
| 107 |
], |
| 108 |
) |
| 109 |
|
| 110 |
def test_iter_test_cases_string_input(self): |
| 111 |
msg = ( |
| 112 |
"Test 'a' must be a test case or test suite not string (was found " |
| 113 |
"in 'abc')." |
| 114 |
) |
| 115 |
with self.assertRaisesMessage(TypeError, msg): |
| 116 |
list(iter_test_cases("abc")) |
| 117 |
|
| 118 |
def test_iter_test_cases_iterable_of_tests(self): |
| 119 |
class Tests(unittest.TestCase): |
| 120 |
def test1(self): |
| 121 |
pass |
| 122 |
|
| 123 |
def test2(self): |
| 124 |
pass |
| 125 |
|
| 126 |
tests = list(unittest.defaultTestLoader.loadTestsFromTestCase(Tests)) |
| 127 |
actual_tests = iter_test_cases(tests) |
| 128 |
self.assertTestNames( |
| 129 |
actual_tests, |
| 130 |
expected=[ |
| 131 |
"Tests.test1", |
| 132 |
"Tests.test2", |
| 133 |
], |
| 134 |
) |
| 135 |
|
| 136 |
def test_iter_test_cases_custom_test_suite_class(self): |
| 137 |
suite = self.make_test_suite(suite_class=MySuite) |
| 138 |
tests = iter_test_cases(suite) |
| 139 |
self.assertTestNames( |
| 140 |
tests, |
| 141 |
expected=[ |
| 142 |
"Tests1.test1", |
| 143 |
"Tests1.test2", |
| 144 |
"Tests2.test1", |
| 145 |
"Tests2.test2", |
| 146 |
], |
| 147 |
) |
| 148 |
|
| 149 |
def test_iter_test_cases_mixed_test_suite_classes(self): |
| 150 |
suite = self.make_test_suite(suite=MySuite()) |
| 151 |
child_suite = list(suite)[0] |
| 152 |
self.assertNotIsInstance(child_suite, MySuite) |
| 153 |
tests = list(iter_test_cases(suite)) |
| 154 |
self.assertEqual(len(tests), 4) |
| 155 |
self.assertNotIsInstance(tests[0], unittest.TestSuite) |
| 156 |
|
| 157 |
def make_tests(self): |
| 158 |
"""Return an iterable of tests.""" |
| 159 |
suite = self.make_test_suite() |
| 160 |
return list(iter_test_cases(suite)) |
| 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 |
if durations := kwargs.get("durations"): |
| 457 |
sys.stderr.write(f"durations={durations}") |
| 458 |
|
| 459 |
|
| 460 |
MockTestRunner.run_tests = mock.Mock(return_value=[]) |
| 461 |
|
| 462 |
|
| 463 |
class ManageCommandTests(unittest.TestCase): |
| 464 |
def test_custom_test_runner(self): |
| 465 |
call_command("test", "sites", testrunner="test_runner.tests.MockTestRunner") |
| 466 |
MockTestRunner.run_tests.assert_called_with(("sites",)) |
| 467 |
|
| 468 |
def test_bad_test_runner(self): |
| 469 |
with self.assertRaises(AttributeError): |
| 470 |
call_command("test", "sites", testrunner="test_runner.NonexistentRunner") |
| 471 |
|
| 472 |
def test_time_recorded(self): |
| 473 |
with captured_stderr() as stderr: |
| 474 |
call_command( |
| 475 |
"test", |
| 476 |
"--timing", |
| 477 |
"sites", |
| 478 |
testrunner="test_runner.tests.MockTestRunner", |
| 479 |
) |
| 480 |
self.assertIn("Total run took", stderr.getvalue()) |
| 481 |
|
| 482 |
@unittest.skipUnless(PY312, "unittest --durations option requires Python 3.12") |
| 483 |
def test_durations(self): |
| 484 |
with captured_stderr() as stderr: |
| 485 |
call_command( |
| 486 |
"test", |
| 487 |
"--durations=10", |
| 488 |
"sites", |
| 489 |
testrunner="test_runner.tests.MockTestRunner", |
| 490 |
) |
| 491 |
self.assertIn("durations=10", stderr.getvalue()) |
| 492 |
|
| 493 |
@unittest.skipIf(PY312, "unittest --durations option requires Python 3.12") |
| 494 |
def test_durations_lt_py312(self): |
| 495 |
msg = "Error: unrecognized arguments: --durations=10" |
| 496 |
with self.assertRaises(CommandError, msg=msg): |
| 497 |
call_command( |
| 498 |
"test", |
| 499 |
"--durations=10", |
| 500 |
"sites", |
| 501 |
testrunner="test_runner.tests.MockTestRunner", |
| 502 |
) |
| 503 |
|
| 504 |
|
| 505 |
# Isolate from the real environment. |
| 506 |
@mock.patch.dict(os.environ, {}, clear=True) |
| 507 |
@mock.patch.object(multiprocessing, "cpu_count", return_value=12) |
| 508 |
class ManageCommandParallelTests(SimpleTestCase): |
| 509 |
@mock.patch.object(multiprocessing, "get_start_method", return_value="fork") |
| 510 |
def test_parallel_default(self, *mocked_objects): |
| 511 |
with captured_stderr() as stderr: |
| 512 |
call_command( |
| 513 |
"test", |
| 514 |
"--parallel", |
| 515 |
testrunner="test_runner.tests.MockTestRunner", |
| 516 |
) |
| 517 |
self.assertIn("parallel=12", stderr.getvalue()) |
| 518 |
|
| 519 |
@mock.patch.object(multiprocessing, "get_start_method", return_value="fork") |
| 520 |
def test_parallel_auto(self, *mocked_objects): |
| 521 |
with captured_stderr() as stderr: |
| 522 |
call_command( |
| 523 |
"test", |
| 524 |
"--parallel=auto", |
| 525 |
testrunner="test_runner.tests.MockTestRunner", |
| 526 |
) |
| 527 |
self.assertIn("parallel=12", stderr.getvalue()) |
| 528 |
|
| 529 |
def test_no_parallel(self, *mocked_objects): |
| 530 |
with captured_stderr() as stderr: |
| 531 |
call_command("test", testrunner="test_runner.tests.MockTestRunner") |
| 532 |
# Parallel is disabled by default. |
| 533 |
self.assertEqual(stderr.getvalue(), "") |
| 534 |
|
| 535 |
@mock.patch.object(multiprocessing, "get_start_method", return_value="spawn") |
| 536 |
def test_parallel_spawn(self, *mocked_objects): |
| 537 |
with captured_stderr() as stderr: |
| 538 |
call_command( |
| 539 |
"test", |
| 540 |
"--parallel=auto", |
| 541 |
testrunner="test_runner.tests.MockTestRunner", |
| 542 |
) |
| 543 |
self.assertIn("parallel=1", stderr.getvalue()) |
| 544 |
|
| 545 |
@mock.patch.object(multiprocessing, "get_start_method", return_value="spawn") |
| 546 |
def test_no_parallel_spawn(self, *mocked_objects): |
| 547 |
with captured_stderr() as stderr: |
| 548 |
call_command( |
| 549 |
"test", |
| 550 |
testrunner="test_runner.tests.MockTestRunner", |
| 551 |
) |
| 552 |
self.assertEqual(stderr.getvalue(), "") |
| 553 |
|
| 554 |
@mock.patch.dict(os.environ, {"DJANGO_TEST_PROCESSES": "7"}) |
| 555 |
@mock.patch.object(multiprocessing, "get_start_method", return_value="fork") |
| 556 |
def test_no_parallel_django_test_processes_env(self, *mocked_objects): |
| 557 |
with captured_stderr() as stderr: |
| 558 |
call_command("test", testrunner="test_runner.tests.MockTestRunner") |
| 559 |
self.assertEqual(stderr.getvalue(), "") |
| 560 |
|
| 561 |
@mock.patch.dict(os.environ, {"DJANGO_TEST_PROCESSES": "invalid"}) |
| 562 |
@mock.patch.object(multiprocessing, "get_start_method", return_value="fork") |
| 563 |
def test_django_test_processes_env_non_int(self, *mocked_objects): |
| 564 |
with self.assertRaises(ValueError): |
| 565 |
call_command( |
| 566 |
"test", |
| 567 |
"--parallel", |
| 568 |
testrunner="test_runner.tests.MockTestRunner", |
| 569 |
) |
| 570 |
|
| 571 |
@mock.patch.dict(os.environ, {"DJANGO_TEST_PROCESSES": "7"}) |
| 572 |
@mock.patch.object(multiprocessing, "get_start_method", return_value="fork") |
| 573 |
def test_django_test_processes_parallel_default(self, *mocked_objects): |
| 574 |
for parallel in ["--parallel", "--parallel=auto"]: |
| 575 |
with self.subTest(parallel=parallel): |
| 576 |
with captured_stderr() as stderr: |
| 577 |
call_command( |
| 578 |
"test", |
| 579 |
parallel, |
| 580 |
testrunner="test_runner.tests.MockTestRunner", |
| 581 |
) |
| 582 |
self.assertIn("parallel=7", stderr.getvalue()) |
| 583 |
|
| 584 |
|
| 585 |
class CustomTestRunnerOptionsSettingsTests(AdminScriptTestCase): |
| 586 |
""" |
| 587 |
Custom runners can add command line arguments. The runner is specified |
| 588 |
through a settings file. |
| 589 |
""" |
| 590 |
|
| 591 |
def setUp(self): |
| 592 |
super().setUp() |
| 593 |
settings = { |
| 594 |
"TEST_RUNNER": "'test_runner.runner.CustomOptionsTestRunner'", |
| 595 |
} |
| 596 |
self.write_settings("settings.py", sdict=settings) |
| 597 |
|
| 598 |
def test_default_options(self): |
| 599 |
args = ["test", "--settings=test_project.settings"] |
| 600 |
out, err = self.run_django_admin(args) |
| 601 |
self.assertNoOutput(err) |
| 602 |
self.assertOutput(out, "1:2:3") |
| 603 |
|
| 604 |
def test_default_and_given_options(self): |
| 605 |
args = ["test", "--settings=test_project.settings", "--option_b=foo"] |
| 606 |
out, err = self.run_django_admin(args) |
| 607 |
self.assertNoOutput(err) |
| 608 |
self.assertOutput(out, "1:foo:3") |
| 609 |
|
| 610 |
def test_option_name_and_value_separated(self): |
| 611 |
args = ["test", "--settings=test_project.settings", "--option_b", "foo"] |
| 612 |
out, err = self.run_django_admin(args) |
| 613 |
self.assertNoOutput(err) |
| 614 |
self.assertOutput(out, "1:foo:3") |
| 615 |
|
| 616 |
def test_all_options_given(self): |
| 617 |
args = [ |
| 618 |
"test", |
| 619 |
"--settings=test_project.settings", |
| 620 |
"--option_a=bar", |
| 621 |
"--option_b=foo", |
| 622 |
"--option_c=31337", |
| 623 |
] |
| 624 |
out, err = self.run_django_admin(args) |
| 625 |
self.assertNoOutput(err) |
| 626 |
self.assertOutput(out, "bar:foo:31337") |
| 627 |
|
| 628 |
|
| 629 |
class CustomTestRunnerOptionsCmdlineTests(AdminScriptTestCase): |
| 630 |
""" |
| 631 |
Custom runners can add command line arguments when the runner is specified |
| 632 |
using --testrunner. |
| 633 |
""" |
| 634 |
|
| 635 |
def setUp(self): |
| 636 |
super().setUp() |
| 637 |
self.write_settings("settings.py") |
| 638 |
|
| 639 |
def test_testrunner_option(self): |
| 640 |
args = [ |
| 641 |
"test", |
| 642 |
"--testrunner", |
| 643 |
"test_runner.runner.CustomOptionsTestRunner", |
| 644 |
"--option_a=bar", |
| 645 |
"--option_b=foo", |
| 646 |
"--option_c=31337", |
| 647 |
] |
| 648 |
out, err = self.run_django_admin(args, "test_project.settings") |
| 649 |
self.assertNoOutput(err) |
| 650 |
self.assertOutput(out, "bar:foo:31337") |
| 651 |
|
| 652 |
def test_testrunner_equals(self): |
| 653 |
args = [ |
| 654 |
"test", |
| 655 |
"--testrunner=test_runner.runner.CustomOptionsTestRunner", |
| 656 |
"--option_a=bar", |
| 657 |
"--option_b=foo", |
| 658 |
"--option_c=31337", |
| 659 |
] |
| 660 |
out, err = self.run_django_admin(args, "test_project.settings") |
| 661 |
self.assertNoOutput(err) |
| 662 |
self.assertOutput(out, "bar:foo:31337") |
| 663 |
|
| 664 |
def test_no_testrunner(self): |
| 665 |
args = ["test", "--testrunner"] |
| 666 |
out, err = self.run_django_admin(args, "test_project.settings") |
| 667 |
self.assertIn("usage", err) |
| 668 |
self.assertNotIn("Traceback", err) |
| 669 |
self.assertNoOutput(out) |
| 670 |
|
| 671 |
|
| 672 |
class NoInitializeSuiteTestRunnerTests(SimpleTestCase): |
| 673 |
@mock.patch.object(multiprocessing, "get_start_method", return_value="spawn") |
| 674 |
@mock.patch( |
| 675 |
"django.test.runner.ParallelTestSuite.initialize_suite", |
| 676 |
side_effect=Exception("initialize_suite() is called."), |
| 677 |
) |
| 678 |
def test_no_initialize_suite_test_runner(self, *mocked_objects): |
| 679 |
""" |
| 680 |
The test suite's initialize_suite() method must always be called when |
| 681 |
using spawn. It cannot rely on a test runner implementation. |
| 682 |
""" |
| 683 |
|
| 684 |
class NoInitializeSuiteTestRunner(DiscoverRunner): |
| 685 |
def setup_test_environment(self, **kwargs): |
| 686 |
return |
| 687 |
|
| 688 |
def setup_databases(self, **kwargs): |
| 689 |
return |
| 690 |
|
| 691 |
def run_checks(self, databases): |
| 692 |
return |
| 693 |
|
| 694 |
def teardown_databases(self, old_config, **kwargs): |
| 695 |
return |
| 696 |
|
| 697 |
def teardown_test_environment(self, **kwargs): |
| 698 |
return |
| 699 |
|
| 700 |
def run_suite(self, suite, **kwargs): |
| 701 |
kwargs = self.get_test_runner_kwargs() |
| 702 |
runner = self.test_runner(**kwargs) |
| 703 |
return runner.run(suite) |
| 704 |
|
| 705 |
with self.assertRaisesMessage(Exception, "initialize_suite() is called."): |
| 706 |
runner = NoInitializeSuiteTestRunner( |
| 707 |
verbosity=0, interactive=False, parallel=2 |
| 708 |
) |
| 709 |
runner.run_tests( |
| 710 |
[ |
| 711 |
"test_runner_apps.sample.tests_sample.TestDjangoTestCase", |
| 712 |
"test_runner_apps.simple.tests", |
| 713 |
] |
| 714 |
) |
| 715 |
|
| 716 |
|
| 717 |
class TestRunnerInitializerTests(SimpleTestCase): |
| 718 |
# Raise an exception to don't actually run tests. |
| 719 |
@mock.patch.object( |
| 720 |
multiprocessing, "Pool", side_effect=Exception("multiprocessing.Pool()") |
| 721 |
) |
| 722 |
def test_no_initialize_suite_test_runner(self, mocked_pool): |
| 723 |
class StubTestRunner(DiscoverRunner): |
| 724 |
def setup_test_environment(self, **kwargs): |
| 725 |
return |
| 726 |
|
| 727 |
def setup_databases(self, **kwargs): |
| 728 |
return |
| 729 |
|
| 730 |
def run_checks(self, databases): |
| 731 |
return |
| 732 |
|
| 733 |
def teardown_databases(self, old_config, **kwargs): |
| 734 |
return |
| 735 |
|
| 736 |
def teardown_test_environment(self, **kwargs): |
| 737 |
return |
| 738 |
|
| 739 |
def run_suite(self, suite, **kwargs): |
| 740 |
kwargs = self.get_test_runner_kwargs() |
| 741 |
runner = self.test_runner(**kwargs) |
| 742 |
return runner.run(suite) |
| 743 |
|
| 744 |
runner = StubTestRunner( |
| 745 |
verbosity=0, interactive=False, parallel=2, debug_mode=True |
| 746 |
) |
| 747 |
with self.assertRaisesMessage(Exception, "multiprocessing.Pool()"): |
| 748 |
runner.run_tests( |
| 749 |
[ |
| 750 |
"test_runner_apps.sample.tests_sample.TestDjangoTestCase", |
| 751 |
"test_runner_apps.simple.tests", |
| 752 |
] |
| 753 |
) |
| 754 |
# Initializer must be a function. |
| 755 |
self.assertIs(mocked_pool.call_args.kwargs["initializer"], _init_worker) |
| 756 |
initargs = mocked_pool.call_args.kwargs["initargs"] |
| 757 |
self.assertEqual(len(initargs), 7) |
| 758 |
self.assertEqual(initargs[5], True) # debug_mode |
| 759 |
self.assertEqual(initargs[6], {db.DEFAULT_DB_ALIAS}) # Used database aliases. |
| 760 |
|
| 761 |
|
| 762 |
class Ticket17477RegressionTests(AdminScriptTestCase): |
| 763 |
def setUp(self): |
| 764 |
super().setUp() |
| 765 |
self.write_settings("settings.py") |
| 766 |
|
| 767 |
def test_ticket_17477(self): |
| 768 |
"""'manage.py help test' works after r16352.""" |
| 769 |
args = ["help", "test"] |
| 770 |
out, err = self.run_manage(args) |
| 771 |
self.assertNoOutput(err) |
| 772 |
|
| 773 |
|
| 774 |
class SQLiteInMemoryTestDbs(TransactionTestCase): |
| 775 |
available_apps = ["test_runner"] |
| 776 |
databases = {"default", "other"} |
| 777 |
|
| 778 |
@unittest.skipUnless( |
| 779 |
all(db.connections[conn].vendor == "sqlite" for conn in db.connections), |
| 780 |
"This is an sqlite-specific issue", |
| 781 |
) |
| 782 |
def test_transaction_support(self): |
| 783 |
# Assert connections mocking is appropriately applied by preventing |
| 784 |
# any attempts at calling create_test_db on the global connection |
| 785 |
# objects. |
| 786 |
for connection in db.connections.all(): |
| 787 |
create_test_db = mock.patch.object( |
| 788 |
connection.creation, |
| 789 |
"create_test_db", |
| 790 |
side_effect=AssertionError( |
| 791 |
"Global connection object shouldn't be manipulated." |
| 792 |
), |
| 793 |
) |
| 794 |
create_test_db.start() |
| 795 |
self.addCleanup(create_test_db.stop) |
| 796 |
for option_key, option_value in ( |
| 797 |
("NAME", ":memory:"), |
| 798 |
("TEST", {"NAME": ":memory:"}), |
| 799 |
): |
| 800 |
tested_connections = db.ConnectionHandler( |
| 801 |
{ |
| 802 |
"default": { |
| 803 |
"ENGINE": "django.db.backends.sqlite3", |
| 804 |
option_key: option_value, |
| 805 |
}, |
| 806 |
"other": { |
| 807 |
"ENGINE": "django.db.backends.sqlite3", |
| 808 |
option_key: option_value, |
| 809 |
}, |
| 810 |
} |
| 811 |
) |
| 812 |
with mock.patch("django.test.utils.connections", new=tested_connections): |
| 813 |
other = tested_connections["other"] |
| 814 |
try: |
| 815 |
new_test_connections = DiscoverRunner(verbosity=0).setup_databases() |
| 816 |
msg = ( |
| 817 |
f"DATABASES setting '{option_key}' option set to sqlite3's " |
| 818 |
"':memory:' value shouldn't interfere with transaction support " |
| 819 |
"detection." |
| 820 |
) |
| 821 |
# Transaction support is properly initialized for the |
| 822 |
# 'other' DB. |
| 823 |
self.assertTrue(other.features.supports_transactions, msg) |
| 824 |
# And all the DBs report that they support transactions. |
| 825 |
self.assertTrue(connections_support_transactions(), msg) |
| 826 |
finally: |
| 827 |
for test_connection, _, _ in new_test_connections: |
| 828 |
test_connection._close() |
| 829 |
|
| 830 |
|
| 831 |
class DummyBackendTest(unittest.TestCase): |
| 832 |
def test_setup_databases(self): |
| 833 |
""" |
| 834 |
setup_databases() doesn't fail with dummy database backend. |
| 835 |
""" |
| 836 |
tested_connections = db.ConnectionHandler({}) |
| 837 |
with mock.patch("django.test.utils.connections", new=tested_connections): |
| 838 |
runner_instance = DiscoverRunner(verbosity=0) |
| 839 |
old_config = runner_instance.setup_databases() |
| 840 |
runner_instance.teardown_databases(old_config) |
| 841 |
|
| 842 |
|
| 843 |
class AliasedDefaultTestSetupTest(unittest.TestCase): |
| 844 |
def test_setup_aliased_default_database(self): |
| 845 |
""" |
| 846 |
setup_databases() doesn't fail when 'default' is aliased |
| 847 |
""" |
| 848 |
tested_connections = db.ConnectionHandler( |
| 849 |
{"default": {"NAME": "dummy"}, "aliased": {"NAME": "dummy"}} |
| 850 |
) |
| 851 |
with mock.patch("django.test.utils.connections", new=tested_connections): |
| 852 |
runner_instance = DiscoverRunner(verbosity=0) |
| 853 |
old_config = runner_instance.setup_databases() |
| 854 |
runner_instance.teardown_databases(old_config) |
| 855 |
|
| 856 |
|
| 857 |
class SetupDatabasesTests(unittest.TestCase): |
| 858 |
def setUp(self): |
| 859 |
self.runner_instance = DiscoverRunner(verbosity=0) |
| 860 |
|
| 861 |
def test_setup_aliased_databases(self): |
| 862 |
tested_connections = db.ConnectionHandler( |
| 863 |
{ |
| 864 |
"default": { |
| 865 |
"ENGINE": "django.db.backends.dummy", |
| 866 |
"NAME": "dbname", |
| 867 |
}, |
| 868 |
"other": { |
| 869 |
"ENGINE": "django.db.backends.dummy", |
| 870 |
"NAME": "dbname", |
| 871 |
}, |
| 872 |
} |
| 873 |
) |
| 874 |
|
| 875 |
with mock.patch( |
| 876 |
"django.db.backends.dummy.base.DatabaseWrapper.creation_class" |
| 877 |
) as mocked_db_creation: |
| 878 |
with mock.patch("django.test.utils.connections", new=tested_connections): |
| 879 |
old_config = self.runner_instance.setup_databases() |
| 880 |
self.runner_instance.teardown_databases(old_config) |
| 881 |
mocked_db_creation.return_value.destroy_test_db.assert_called_once_with( |
| 882 |
"dbname", 0, False |
| 883 |
) |
| 884 |
|
| 885 |
def test_setup_test_database_aliases(self): |
| 886 |
""" |
| 887 |
The default database must be the first because data migrations |
| 888 |
use the default alias by default. |
| 889 |
""" |
| 890 |
tested_connections = db.ConnectionHandler( |
| 891 |
{ |
| 892 |
"other": { |
| 893 |
"ENGINE": "django.db.backends.dummy", |
| 894 |
"NAME": "dbname", |
| 895 |
}, |
| 896 |
"default": { |
| 897 |
"ENGINE": "django.db.backends.dummy", |
| 898 |
"NAME": "dbname", |
| 899 |
}, |
| 900 |
} |
| 901 |
) |
| 902 |
with mock.patch("django.test.utils.connections", new=tested_connections): |
| 903 |
test_databases, _ = get_unique_databases_and_mirrors() |
| 904 |
self.assertEqual( |
| 905 |
test_databases, |
| 906 |
{ |
| 907 |
("", "", "django.db.backends.dummy", "test_dbname"): ( |
| 908 |
"dbname", |
| 909 |
["default", "other"], |
| 910 |
), |
| 911 |
}, |
| 912 |
) |
| 913 |
|
| 914 |
def test_destroy_test_db_restores_db_name(self): |
| 915 |
tested_connections = db.ConnectionHandler( |
| 916 |
{ |
| 917 |
"default": { |
| 918 |
"ENGINE": settings.DATABASES[db.DEFAULT_DB_ALIAS]["ENGINE"], |
| 919 |
"NAME": "xxx_test_database", |
| 920 |
}, |
| 921 |
} |
| 922 |
) |
| 923 |
# Using the real current name as old_name to not mess with the test suite. |
| 924 |
old_name = settings.DATABASES[db.DEFAULT_DB_ALIAS]["NAME"] |
| 925 |
with mock.patch("django.db.connections", new=tested_connections): |
| 926 |
tested_connections["default"].creation.destroy_test_db( |
| 927 |
old_name, verbosity=0, keepdb=True |
| 928 |
) |
| 929 |
self.assertEqual( |
| 930 |
tested_connections["default"].settings_dict["NAME"], old_name |
| 931 |
) |
| 932 |
|
| 933 |
def test_serialization(self): |
| 934 |
tested_connections = db.ConnectionHandler( |
| 935 |
{ |
| 936 |
"default": { |
| 937 |
"ENGINE": "django.db.backends.dummy", |
| 938 |
}, |
| 939 |
} |
| 940 |
) |
| 941 |
with mock.patch( |
| 942 |
"django.db.backends.dummy.base.DatabaseWrapper.creation_class" |
| 943 |
) as mocked_db_creation: |
| 944 |
with mock.patch("django.test.utils.connections", new=tested_connections): |
| 945 |
self.runner_instance.setup_databases() |
| 946 |
mocked_db_creation.return_value.create_test_db.assert_called_once_with( |
| 947 |
verbosity=0, autoclobber=False, serialize=False, keepdb=False |
| 948 |
) |
| 949 |
mocked_db_creation.return_value.serialize_db_to_string.assert_called_once_with() |
| 950 |
|
| 951 |
|
| 952 |
@skipUnlessDBFeature("supports_sequence_reset") |
| 953 |
class AutoIncrementResetTest(TransactionTestCase): |
| 954 |
""" |
| 955 |
Creating the same models in different test methods receive the same PK |
| 956 |
values since the sequences are reset before each test method. |
| 957 |
""" |
| 958 |
|
| 959 |
available_apps = ["test_runner"] |
| 960 |
|
| 961 |
reset_sequences = True |
| 962 |
|
| 963 |
def _test(self): |
| 964 |
# Regular model |
| 965 |
p = Person.objects.create(first_name="Jack", last_name="Smith") |
| 966 |
self.assertEqual(p.pk, 1) |
| 967 |
# Auto-created many-to-many through model |
| 968 |
p.friends.add(Person.objects.create(first_name="Jacky", last_name="Smith")) |
| 969 |
self.assertEqual(p.friends.through.objects.first().pk, 1) |
| 970 |
# Many-to-many through model |
| 971 |
b = B.objects.create() |
| 972 |
t = Through.objects.create(person=p, b=b) |
| 973 |
self.assertEqual(t.pk, 1) |
| 974 |
|
| 975 |
def test_autoincrement_reset1(self): |
| 976 |
self._test() |
| 977 |
|
| 978 |
def test_autoincrement_reset2(self): |
| 979 |
self._test() |
| 980 |
|
| 981 |
|
| 982 |
class EmptyDefaultDatabaseTest(unittest.TestCase): |
| 983 |
def test_empty_default_database(self): |
| 984 |
""" |
| 985 |
An empty default database in settings does not raise an ImproperlyConfigured |
| 986 |
error when running a unit test that does not use a database. |
| 987 |
""" |
| 988 |
tested_connections = db.ConnectionHandler({"default": {}}) |
| 989 |
with mock.patch("django.db.connections", new=tested_connections): |
| 990 |
connection = tested_connections[db.utils.DEFAULT_DB_ALIAS] |
| 991 |
self.assertEqual( |
| 992 |
connection.settings_dict["ENGINE"], "django.db.backends.dummy" |
| 993 |
) |
| 994 |
connections_support_transactions() |
| 995 |
|
| 996 |
|
| 997 |
class RunTestsExceptionHandlingTests(unittest.TestCase): |
| 998 |
def test_run_checks_raises(self): |
| 999 |
""" |
| 1000 |
Teardown functions are run when run_checks() raises SystemCheckError. |
| 1001 |
""" |
| 1002 |
with ( |
| 1003 |
mock.patch("django.test.runner.DiscoverRunner.setup_test_environment"), |
| 1004 |
mock.patch("django.test.runner.DiscoverRunner.setup_databases"), |
| 1005 |
mock.patch("django.test.runner.DiscoverRunner.build_suite"), |
| 1006 |
mock.patch( |
| 1007 |
"django.test.runner.DiscoverRunner.run_checks", |
| 1008 |
side_effect=SystemCheckError, |
| 1009 |
), |
| 1010 |
mock.patch( |
| 1011 |
"django.test.runner.DiscoverRunner.teardown_databases" |
| 1012 |
) as teardown_databases, |
| 1013 |
mock.patch( |
| 1014 |
"django.test.runner.DiscoverRunner.teardown_test_environment" |
| 1015 |
) as teardown_test_environment, |
| 1016 |
): |
| 1017 |
runner = DiscoverRunner(verbosity=0, interactive=False) |
| 1018 |
with self.assertRaises(SystemCheckError): |
| 1019 |
runner.run_tests( |
| 1020 |
["test_runner_apps.sample.tests_sample.TestDjangoTestCase"] |
| 1021 |
) |
| 1022 |
self.assertTrue(teardown_databases.called) |
| 1023 |
self.assertTrue(teardown_test_environment.called) |
| 1024 |
|
| 1025 |
def test_run_checks_raises_and_teardown_raises(self): |
| 1026 |
""" |
| 1027 |
SystemCheckError is surfaced when run_checks() raises SystemCheckError |
| 1028 |
and teardown databases() raises ValueError. |
| 1029 |
""" |
| 1030 |
with ( |
| 1031 |
mock.patch("django.test.runner.DiscoverRunner.setup_test_environment"), |
| 1032 |
mock.patch("django.test.runner.DiscoverRunner.setup_databases"), |
| 1033 |
mock.patch("django.test.runner.DiscoverRunner.build_suite"), |
| 1034 |
mock.patch( |
| 1035 |
"django.test.runner.DiscoverRunner.run_checks", |
| 1036 |
side_effect=SystemCheckError, |
| 1037 |
), |
| 1038 |
mock.patch( |
| 1039 |
"django.test.runner.DiscoverRunner.teardown_databases", |
| 1040 |
side_effect=ValueError, |
| 1041 |
) as teardown_databases, |
| 1042 |
mock.patch( |
| 1043 |
"django.test.runner.DiscoverRunner.teardown_test_environment" |
| 1044 |
) as teardown_test_environment, |
| 1045 |
): |
| 1046 |
runner = DiscoverRunner(verbosity=0, interactive=False) |
| 1047 |
with self.assertRaises(SystemCheckError): |
| 1048 |
runner.run_tests( |
| 1049 |
["test_runner_apps.sample.tests_sample.TestDjangoTestCase"] |
| 1050 |
) |
| 1051 |
self.assertTrue(teardown_databases.called) |
| 1052 |
self.assertFalse(teardown_test_environment.called) |
| 1053 |
|
| 1054 |
def test_run_checks_passes_and_teardown_raises(self): |
| 1055 |
""" |
| 1056 |
Exceptions on teardown are surfaced if no exceptions happen during |
| 1057 |
run_checks(). |
| 1058 |
""" |
| 1059 |
with ( |
| 1060 |
mock.patch("django.test.runner.DiscoverRunner.setup_test_environment"), |
| 1061 |
mock.patch("django.test.runner.DiscoverRunner.setup_databases"), |
| 1062 |
mock.patch("django.test.runner.DiscoverRunner.build_suite"), |
| 1063 |
mock.patch("django.test.runner.DiscoverRunner.run_checks"), |
| 1064 |
mock.patch( |
| 1065 |
"django.test.runner.DiscoverRunner.teardown_databases", |
| 1066 |
side_effect=ValueError, |
| 1067 |
) as teardown_databases, |
| 1068 |
mock.patch( |
| 1069 |
"django.test.runner.DiscoverRunner.teardown_test_environment" |
| 1070 |
) as teardown_test_environment, |
| 1071 |
): |
| 1072 |
runner = DiscoverRunner(verbosity=0, interactive=False) |
| 1073 |
with self.assertRaises(ValueError): |
| 1074 |
# Suppress the output when running TestDjangoTestCase. |
| 1075 |
with mock.patch("sys.stderr"): |
| 1076 |
runner.run_tests( |
| 1077 |
["test_runner_apps.sample.tests_sample.TestDjangoTestCase"] |
| 1078 |
) |
| 1079 |
self.assertTrue(teardown_databases.called) |
| 1080 |
self.assertFalse(teardown_test_environment.called) |