2021-03-16 12:41:00 +00:00
|
|
|
import pickle
|
2021-03-25 09:33:54 +00:00
|
|
|
import sys
|
2016-08-11 10:47:12 +00:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from django.test import SimpleTestCase
|
|
|
|
from django.test.runner import RemoteTestResult
|
2022-04-07 05:02:21 +00:00
|
|
|
from django.utils.version import PY311
|
2016-08-11 10:47:12 +00:00
|
|
|
|
|
|
|
try:
|
2021-03-25 09:33:54 +00:00
|
|
|
import tblib.pickling_support
|
2016-08-11 10:47:12 +00:00
|
|
|
except ImportError:
|
|
|
|
tblib = None
|
|
|
|
|
|
|
|
|
2016-10-06 19:54:38 +00:00
|
|
|
class ExceptionThatFailsUnpickling(Exception):
|
|
|
|
"""
|
|
|
|
After pickling, this class fails unpickling with an error about incorrect
|
|
|
|
arguments passed to __init__().
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2016-10-06 19:54:38 +00:00
|
|
|
def __init__(self, arg):
|
2017-01-21 13:13:44 +00:00
|
|
|
super().__init__()
|
2016-10-06 19:54:38 +00:00
|
|
|
|
|
|
|
|
2016-08-11 10:47:12 +00:00
|
|
|
class ParallelTestRunnerTest(SimpleTestCase):
|
|
|
|
"""
|
|
|
|
End-to-end tests of the parallel test runner.
|
|
|
|
|
|
|
|
These tests are only meaningful when running tests in parallel using
|
|
|
|
the --parallel option, though it doesn't hurt to run them not in
|
|
|
|
parallel.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_subtest(self):
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
Passing subtests work.
|
2016-08-11 10:47:12 +00:00
|
|
|
"""
|
|
|
|
for i in range(2):
|
|
|
|
with self.subTest(index=i):
|
|
|
|
self.assertEqual(i, i)
|
|
|
|
|
|
|
|
|
|
|
|
class SampleFailingSubtest(SimpleTestCase):
|
|
|
|
|
|
|
|
# This method name doesn't begin with "test" to prevent test discovery
|
|
|
|
# from seeing it.
|
|
|
|
def dummy_test(self):
|
|
|
|
"""
|
|
|
|
A dummy test for testing subTest failures.
|
|
|
|
"""
|
|
|
|
for i in range(3):
|
|
|
|
with self.subTest(index=i):
|
|
|
|
self.assertEqual(i, 1)
|
|
|
|
|
|
|
|
|
|
|
|
class RemoteTestResultTest(SimpleTestCase):
|
2021-03-25 09:33:54 +00:00
|
|
|
def _test_error_exc_info(self):
|
|
|
|
try:
|
|
|
|
raise ValueError("woops")
|
|
|
|
except ValueError:
|
|
|
|
return sys.exc_info()
|
|
|
|
|
2021-03-16 12:43:23 +00:00
|
|
|
def test_was_successful_no_events(self):
|
|
|
|
result = RemoteTestResult()
|
|
|
|
self.assertIs(result.wasSuccessful(), True)
|
|
|
|
|
|
|
|
def test_was_successful_one_success(self):
|
|
|
|
result = RemoteTestResult()
|
|
|
|
result.addSuccess(None)
|
|
|
|
self.assertIs(result.wasSuccessful(), True)
|
|
|
|
|
|
|
|
def test_was_successful_one_expected_failure(self):
|
|
|
|
result = RemoteTestResult()
|
2021-03-25 09:33:54 +00:00
|
|
|
result.addExpectedFailure(None, self._test_error_exc_info())
|
2021-03-16 12:43:23 +00:00
|
|
|
self.assertIs(result.wasSuccessful(), True)
|
|
|
|
|
|
|
|
def test_was_successful_one_skip(self):
|
|
|
|
result = RemoteTestResult()
|
|
|
|
result.addSkip(None, "Skipped")
|
|
|
|
self.assertIs(result.wasSuccessful(), True)
|
|
|
|
|
2021-03-25 09:33:54 +00:00
|
|
|
@unittest.skipUnless(tblib is not None, "requires tblib to be installed")
|
2021-03-16 12:43:23 +00:00
|
|
|
def test_was_successful_one_error(self):
|
|
|
|
result = RemoteTestResult()
|
2021-03-25 09:33:54 +00:00
|
|
|
result.addError(None, self._test_error_exc_info())
|
2021-03-16 12:43:23 +00:00
|
|
|
self.assertIs(result.wasSuccessful(), False)
|
|
|
|
|
2021-03-25 09:33:54 +00:00
|
|
|
@unittest.skipUnless(tblib is not None, "requires tblib to be installed")
|
2021-03-16 12:43:23 +00:00
|
|
|
def test_was_successful_one_failure(self):
|
|
|
|
result = RemoteTestResult()
|
2021-03-25 09:33:54 +00:00
|
|
|
result.addFailure(None, self._test_error_exc_info())
|
2021-03-16 12:43:23 +00:00
|
|
|
self.assertIs(result.wasSuccessful(), False)
|
|
|
|
|
2021-03-16 12:41:00 +00:00
|
|
|
def test_picklable(self):
|
|
|
|
result = RemoteTestResult()
|
|
|
|
loaded_result = pickle.loads(pickle.dumps(result))
|
|
|
|
self.assertEqual(result.events, loaded_result.events)
|
|
|
|
|
2016-10-06 19:54:38 +00:00
|
|
|
def test_pickle_errors_detection(self):
|
|
|
|
picklable_error = RuntimeError("This is fine")
|
|
|
|
not_unpicklable_error = ExceptionThatFailsUnpickling("arg")
|
|
|
|
|
|
|
|
result = RemoteTestResult()
|
|
|
|
result._confirm_picklable(picklable_error)
|
|
|
|
|
|
|
|
msg = "__init__() missing 1 required positional argument"
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
|
|
|
result._confirm_picklable(not_unpicklable_error)
|
|
|
|
|
2016-12-01 10:38:01 +00:00
|
|
|
@unittest.skipUnless(tblib is not None, "requires tblib to be installed")
|
2016-08-11 10:47:12 +00:00
|
|
|
def test_add_failing_subtests(self):
|
|
|
|
"""
|
|
|
|
Failing subtests are added correctly using addSubTest().
|
|
|
|
"""
|
|
|
|
# Manually run a test with failing subtests to prevent the failures
|
|
|
|
# from affecting the actual test run.
|
|
|
|
result = RemoteTestResult()
|
|
|
|
subtest_test = SampleFailingSubtest(methodName="dummy_test")
|
|
|
|
subtest_test.run(result=result)
|
|
|
|
|
|
|
|
events = result.events
|
|
|
|
self.assertEqual(len(events), 4)
|
2021-03-16 12:43:23 +00:00
|
|
|
self.assertIs(result.wasSuccessful(), False)
|
2016-08-11 10:47:12 +00:00
|
|
|
|
|
|
|
event = events[1]
|
|
|
|
self.assertEqual(event[0], "addSubTest")
|
|
|
|
self.assertEqual(
|
|
|
|
str(event[2]),
|
2022-04-07 05:02:21 +00:00
|
|
|
"dummy_test (test_runner.test_parallel.SampleFailingSubtest%s) (index=0)"
|
|
|
|
# Python 3.11 uses fully qualified test name in the output.
|
|
|
|
% (".dummy_test" if PY311 else ""),
|
2016-08-11 10:47:12 +00:00
|
|
|
)
|
2021-01-19 07:35:16 +00:00
|
|
|
self.assertEqual(repr(event[3][1]), "AssertionError('0 != 1')")
|
2016-08-11 10:47:12 +00:00
|
|
|
|
|
|
|
event = events[2]
|
2021-01-19 07:35:16 +00:00
|
|
|
self.assertEqual(repr(event[3][1]), "AssertionError('2 != 1')")
|