| 1 |
import pickle |
| 2 |
import sys |
| 3 |
import unittest |
| 4 |
from unittest.case import TestCase |
| 5 |
from unittest.result import TestResult |
| 6 |
from unittest.suite import TestSuite, _ErrorHolder |
| 7 |
|
| 8 |
from django.test import SimpleTestCase |
| 9 |
from django.test.runner import ParallelTestSuite, RemoteTestResult |
| 10 |
from django.utils.version import PY311, PY312 |
| 11 |
|
| 12 |
try: |
| 13 |
import tblib.pickling_support |
| 14 |
except ImportError: |
| 15 |
tblib = None |
| 16 |
|
| 17 |
|
| 18 |
def _test_error_exc_info(): |
| 19 |
try: |
| 20 |
raise ValueError("woops") |
| 21 |
except ValueError: |
| 22 |
return sys.exc_info() |
| 23 |
|
| 24 |
|
| 25 |
class ExceptionThatFailsUnpickling(Exception): |
| 26 |
""" |
| 27 |
After pickling, this class fails unpickling with an error about incorrect |
| 28 |
arguments passed to __init__(). |
| 29 |
""" |
| 30 |
|
| 31 |
def __init__(self, arg): |
| 32 |
super().__init__() |
| 33 |
|
| 34 |
def __reduce__(self): |
| 35 |
# tblib 3.2+ makes exception subclasses picklable by default. |
| 36 |
# Return (cls, ()) so the constructor fails on unpickle, preserving |
| 37 |
# the needed behavior for test_pickle_errors_detection. |
| 38 |
return (self.__class__, ()) |
| 39 |
|
| 40 |
|
| 41 |
class ParallelTestRunnerTest(SimpleTestCase): |
| 42 |
""" |
| 43 |
End-to-end tests of the parallel test runner. |
| 44 |
|
| 45 |
These tests are only meaningful when running tests in parallel using |
| 46 |
the --parallel option, though it doesn't hurt to run them not in |
| 47 |
parallel. |
| 48 |
""" |
| 49 |
|
| 50 |
def test_subtest(self): |
| 51 |
""" |
| 52 |
Passing subtests work. |
| 53 |
""" |
| 54 |
for i in range(2): |
| 55 |
with self.subTest(index=i): |
| 56 |
self.assertEqual(i, i) |
| 57 |
|
| 58 |
|
| 59 |
class SampleFailingSubtest(SimpleTestCase): |
| 60 |
# This method name doesn't begin with "test" to prevent test discovery |
| 61 |
# from seeing it. |
| 62 |
def dummy_test(self): |
| 63 |
""" |
| 64 |
A dummy test for testing subTest failures. |
| 65 |
""" |
| 66 |
for i in range(3): |
| 67 |
with self.subTest(index=i): |
| 68 |
self.assertEqual(i, 1) |
| 69 |
|
| 70 |
# This method name doesn't begin with "test" to prevent test discovery |
| 71 |
# from seeing it. |
| 72 |
def pickle_error_test(self): |
| 73 |
with self.subTest("TypeError: cannot pickle memoryview object"): |
| 74 |
self.x = memoryview(b"") |
| 75 |
self.fail("expected failure") |
| 76 |
|
| 77 |
|
| 78 |
class SampleErrorTest(SimpleTestCase): |
| 79 |
@classmethod |
| 80 |
def setUpClass(cls): |
| 81 |
raise ValueError("woops") |
| 82 |
super().setUpClass() |
| 83 |
|
| 84 |
# This method name doesn't begin with "test" to prevent test discovery |
| 85 |
# from seeing it. |
| 86 |
def dummy_test(self): |
| 87 |
raise AssertionError("SampleErrorTest.dummy_test() was called") |
| 88 |
|
| 89 |
|
| 90 |
class RemoteTestResultTest(SimpleTestCase): |
| 91 |
def test_was_successful_no_events(self): |
| 92 |
result = RemoteTestResult() |
| 93 |
self.assertIs(result.wasSuccessful(), True) |
| 94 |
|
| 95 |
def test_was_successful_one_success(self): |
| 96 |
result = RemoteTestResult() |
| 97 |
test = None |
| 98 |
result.startTest(test) |
| 99 |
try: |
| 100 |
result.addSuccess(test) |
| 101 |
finally: |
| 102 |
result.stopTest(test) |
| 103 |
self.assertIs(result.wasSuccessful(), True) |
| 104 |
|
| 105 |
def test_was_successful_one_expected_failure(self): |
| 106 |
result = RemoteTestResult() |
| 107 |
test = None |
| 108 |
result.startTest(test) |
| 109 |
try: |
| 110 |
result.addExpectedFailure(test, _test_error_exc_info()) |
| 111 |
finally: |
| 112 |
result.stopTest(test) |
| 113 |
self.assertIs(result.wasSuccessful(), True) |
| 114 |
|
| 115 |
def test_was_successful_one_skip(self): |
| 116 |
result = RemoteTestResult() |
| 117 |
test = None |
| 118 |
result.startTest(test) |
| 119 |
try: |
| 120 |
result.addSkip(test, "Skipped") |
| 121 |
finally: |
| 122 |
result.stopTest(test) |
| 123 |
self.assertIs(result.wasSuccessful(), True) |
| 124 |
|
| 125 |
@unittest.skipUnless(tblib is not None, "requires tblib to be installed") |
| 126 |
def test_was_successful_one_error(self): |
| 127 |
result = RemoteTestResult() |
| 128 |
test = None |
| 129 |
result.startTest(test) |
| 130 |
try: |
| 131 |
result.addError(test, _test_error_exc_info()) |
| 132 |
finally: |
| 133 |
result.stopTest(test) |
| 134 |
self.assertIs(result.wasSuccessful(), False) |
| 135 |
|
| 136 |
@unittest.skipUnless(tblib is not None, "requires tblib to be installed") |
| 137 |
def test_was_successful_one_failure(self): |
| 138 |
result = RemoteTestResult() |
| 139 |
test = None |
| 140 |
result.startTest(test) |
| 141 |
try: |
| 142 |
result.addFailure(test, _test_error_exc_info()) |
| 143 |
finally: |
| 144 |
result.stopTest(test) |
| 145 |
self.assertIs(result.wasSuccessful(), False) |
| 146 |
|
| 147 |
@unittest.skipUnless(tblib is not None, "requires tblib to be installed") |
| 148 |
def test_add_error_before_first_test(self): |
| 149 |
result = RemoteTestResult() |
| 150 |
test_id = "test_foo (tests.test_foo.FooTest.test_foo)" |
| 151 |
test = _ErrorHolder(test_id) |
| 152 |
# Call addError() without a call to startTest(). |
| 153 |
result.addError(test, _test_error_exc_info()) |
| 154 |
|
| 155 |
(event,) = result.events |
| 156 |
self.assertEqual(event[0], "addError") |
| 157 |
self.assertEqual(event[1], -1) |
| 158 |
self.assertEqual(event[2], test_id) |
| 159 |
error_type, _, _ = event[3] |
| 160 |
self.assertEqual(error_type, ValueError) |
| 161 |
self.assertIs(result.wasSuccessful(), False) |
| 162 |
|
| 163 |
def test_picklable(self): |
| 164 |
result = RemoteTestResult() |
| 165 |
loaded_result = pickle.loads(pickle.dumps(result)) |
| 166 |
self.assertEqual(result.events, loaded_result.events) |
| 167 |
|
| 168 |
def test_pickle_errors_detection(self): |
| 169 |
picklable_error = RuntimeError("This is fine") |
| 170 |
not_unpicklable_error = ExceptionThatFailsUnpickling("arg") |
| 171 |
|
| 172 |
result = RemoteTestResult() |
| 173 |
result._confirm_picklable(picklable_error) |
| 174 |
|
| 175 |
# The exception can be pickled but not unpickled. |
| 176 |
pickle.dumps(not_unpicklable_error) |
| 177 |
msg = "__init__() missing 1 required positional argument" |
| 178 |
with self.assertRaisesMessage(TypeError, msg): |
| 179 |
result._confirm_picklable(not_unpicklable_error) |
| 180 |
|
| 181 |
@unittest.skipUnless(tblib is not None, "requires tblib to be installed") |
| 182 |
def test_unpicklable_subtest(self): |
| 183 |
result = RemoteTestResult() |
| 184 |
subtest_test = SampleFailingSubtest(methodName="pickle_error_test") |
| 185 |
subtest_test.run(result=result) |
| 186 |
|
| 187 |
events = result.events |
| 188 |
subtest_event = events[1] |
| 189 |
assertion_error = subtest_event[3] |
| 190 |
self.assertEqual(str(assertion_error[1]), "expected failure") |
| 191 |
|
| 192 |
@unittest.skipUnless(tblib is not None, "requires tblib to be installed") |
| 193 |
def test_add_failing_subtests(self): |
| 194 |
""" |
| 195 |
Failing subtests are added correctly using addSubTest(). |
| 196 |
""" |
| 197 |
# Manually run a test with failing subtests to prevent the failures |
| 198 |
# from affecting the actual test run. |
| 199 |
result = RemoteTestResult() |
| 200 |
subtest_test = SampleFailingSubtest(methodName="dummy_test") |
| 201 |
subtest_test.run(result=result) |
| 202 |
|
| 203 |
events = result.events |
| 204 |
# addDurations added in Python 3.12. |
| 205 |
if PY312: |
| 206 |
self.assertEqual(len(events), 5) |
| 207 |
else: |
| 208 |
self.assertEqual(len(events), 4) |
| 209 |
self.assertIs(result.wasSuccessful(), False) |
| 210 |
|
| 211 |
event = events[1] |
| 212 |
self.assertEqual(event[0], "addSubTest") |
| 213 |
self.assertEqual( |
| 214 |
str(event[2]), |
| 215 |
"dummy_test (test_runner.test_parallel.SampleFailingSubtest%s) (index=0)" |
| 216 |
# Python 3.11 uses fully qualified test name in the output. |
| 217 |
% (".dummy_test" if PY311 else ""), |
| 218 |
) |
| 219 |
self.assertEqual(repr(event[3][1]), "AssertionError('0 != 1')") |
| 220 |
|
| 221 |
event = events[2] |
| 222 |
self.assertEqual(repr(event[3][1]), "AssertionError('2 != 1')") |
| 223 |
|
| 224 |
@unittest.skipUnless(PY312, "unittest --durations option requires Python 3.12") |
| 225 |
def test_add_duration(self): |
| 226 |
result = RemoteTestResult() |
| 227 |
result.addDuration(None, 2.3) |
| 228 |
self.assertEqual(result.collectedDurations, [("None", 2.3)]) |
| 229 |
|
| 230 |
|
| 231 |
class ParallelTestSuiteTest(SimpleTestCase): |
| 232 |
@unittest.skipUnless(tblib is not None, "requires tblib to be installed") |
| 233 |
def test_handle_add_error_before_first_test(self): |
| 234 |
dummy_subsuites = [] |
| 235 |
pts = ParallelTestSuite(dummy_subsuites, processes=2) |
| 236 |
result = TestResult() |
| 237 |
remote_result = RemoteTestResult() |
| 238 |
test = SampleErrorTest(methodName="dummy_test") |
| 239 |
suite = TestSuite([test]) |
| 240 |
suite.run(remote_result) |
| 241 |
for event in remote_result.events: |
| 242 |
pts.handle_event(result, tests=list(suite), event=event) |
| 243 |
|
| 244 |
self.assertEqual(len(result.errors), 1) |
| 245 |
actual_test, tb_and_details_str = result.errors[0] |
| 246 |
self.assertIsInstance(actual_test, _ErrorHolder) |
| 247 |
self.assertEqual( |
| 248 |
actual_test.id(), "setUpClass (test_runner.test_parallel.SampleErrorTest)" |
| 249 |
) |
| 250 |
self.assertIn("Traceback (most recent call last):", tb_and_details_str) |
| 251 |
self.assertIn("ValueError: woops", tb_and_details_str) |
| 252 |
|
| 253 |
def test_handle_add_error_during_test(self): |
| 254 |
dummy_subsuites = [] |
| 255 |
pts = ParallelTestSuite(dummy_subsuites, processes=2) |
| 256 |
result = TestResult() |
| 257 |
test = TestCase() |
| 258 |
err = _test_error_exc_info() |
| 259 |
event = ("addError", 0, err) |
| 260 |
pts.handle_event(result, tests=[test], event=event) |
| 261 |
|
| 262 |
self.assertEqual(len(result.errors), 1) |
| 263 |
actual_test, tb_and_details_str = result.errors[0] |
| 264 |
self.assertIsInstance(actual_test, TestCase) |
| 265 |
self.assertEqual(actual_test.id(), "unittest.case.TestCase.runTest") |
| 266 |
self.assertIn("Traceback (most recent call last):", tb_and_details_str) |
| 267 |
self.assertIn("ValueError: woops", tb_and_details_str) |
| 268 |
|
| 269 |
def test_handle_add_failure(self): |
| 270 |
dummy_subsuites = [] |
| 271 |
pts = ParallelTestSuite(dummy_subsuites, processes=2) |
| 272 |
result = TestResult() |
| 273 |
test = TestCase() |
| 274 |
err = _test_error_exc_info() |
| 275 |
event = ("addFailure", 0, err) |
| 276 |
pts.handle_event(result, tests=[test], event=event) |
| 277 |
|
| 278 |
self.assertEqual(len(result.failures), 1) |
| 279 |
actual_test, tb_and_details_str = result.failures[0] |
| 280 |
self.assertIsInstance(actual_test, TestCase) |
| 281 |
self.assertEqual(actual_test.id(), "unittest.case.TestCase.runTest") |
| 282 |
self.assertIn("Traceback (most recent call last):", tb_and_details_str) |
| 283 |
self.assertIn("ValueError: woops", tb_and_details_str) |
| 284 |
|
| 285 |
def test_handle_add_success(self): |
| 286 |
dummy_subsuites = [] |
| 287 |
pts = ParallelTestSuite(dummy_subsuites, processes=2) |
| 288 |
result = TestResult() |
| 289 |
test = TestCase() |
| 290 |
event = ("addSuccess", 0) |
| 291 |
pts.handle_event(result, tests=[test], event=event) |
| 292 |
|
| 293 |
self.assertEqual(len(result.errors), 0) |
| 294 |
self.assertEqual(len(result.failures), 0) |