Refs #33173 -- Fixed test_runner/test_utils tests on Python 3.11+.

Python 3.11 uses fully qualified test name in unittest output. See
755be9b150
This commit is contained in:
Mariusz Felisiak 2022-04-07 07:02:21 +02:00 committed by GitHub
parent bfe9665502
commit 2ee4caf56b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 10 deletions

View File

@ -4,6 +4,7 @@ from io import StringIO
from django.db import connection from django.db import connection
from django.test import TestCase from django.test import TestCase
from django.test.runner import DiscoverRunner from django.test.runner import DiscoverRunner
from django.utils.version import PY311
from .models import Person from .models import Person
@ -109,14 +110,17 @@ class TestDebugSQL(unittest.TestCase):
), ),
] ]
# Python 3.11 uses fully qualified test name in the output.
method_name = ".runTest" if PY311 else ""
test_class_path = "test_runner.test_debug_sql.TestDebugSQL"
verbose_expected_outputs = [ verbose_expected_outputs = [
"runTest (test_runner.test_debug_sql.TestDebugSQL.FailingTest) ... FAIL", f"runTest ({test_class_path}.FailingTest{method_name}) ... FAIL",
"runTest (test_runner.test_debug_sql.TestDebugSQL.ErrorTest) ... ERROR", f"runTest ({test_class_path}.ErrorTest{method_name}) ... ERROR",
"runTest (test_runner.test_debug_sql.TestDebugSQL.PassingTest) ... ok", f"runTest ({test_class_path}.PassingTest{method_name}) ... ok",
# If there are errors/failures in subtests but not in test itself, # If there are errors/failures in subtests but not in test itself,
# the status is not written. That behavior comes from Python. # the status is not written. That behavior comes from Python.
"runTest (test_runner.test_debug_sql.TestDebugSQL.FailingSubTest) ...", f"runTest ({test_class_path}.FailingSubTest{method_name}) ...",
"runTest (test_runner.test_debug_sql.TestDebugSQL.ErrorSubTest) ...", f"runTest ({test_class_path}.ErrorSubTest{method_name}) ...",
( (
"""SELECT COUNT(*) AS "__count" """ """SELECT COUNT(*) AS "__count" """
"""FROM "test_runner_person" WHERE """ """FROM "test_runner_person" WHERE """

View File

@ -4,6 +4,7 @@ import unittest
from django.test import SimpleTestCase from django.test import SimpleTestCase
from django.test.runner import RemoteTestResult from django.test.runner import RemoteTestResult
from django.utils.version import PY311
try: try:
import tblib.pickling_support import tblib.pickling_support
@ -125,7 +126,9 @@ class RemoteTestResultTest(SimpleTestCase):
self.assertEqual(event[0], "addSubTest") self.assertEqual(event[0], "addSubTest")
self.assertEqual( self.assertEqual(
str(event[2]), str(event[2]),
"dummy_test (test_runner.test_parallel.SampleFailingSubtest) (index=0)", "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 ""),
) )
self.assertEqual(repr(event[3][1]), "AssertionError('0 != 1')") self.assertEqual(repr(event[3][1]), "AssertionError('0 != 1')")

View File

@ -48,6 +48,7 @@ from django.test.utils import (
from django.urls import NoReverseMatch, path, reverse, reverse_lazy from django.urls import NoReverseMatch, path, reverse, reverse_lazy
from django.utils.deprecation import RemovedInDjango50Warning from django.utils.deprecation import RemovedInDjango50Warning
from django.utils.log import DEFAULT_LOGGING from django.utils.log import DEFAULT_LOGGING
from django.utils.version import PY311
from .models import Car, Person, PossessedCar from .models import Car, Person, PossessedCar
from .views import empty_response from .views import empty_response
@ -100,9 +101,11 @@ class SkippingTestCase(SimpleTestCase):
SkipTestCase("test_foo").test_foo, SkipTestCase("test_foo").test_foo,
ValueError, ValueError,
"skipUnlessDBFeature cannot be used on test_foo (test_utils.tests." "skipUnlessDBFeature cannot be used on test_foo (test_utils.tests."
"SkippingTestCase.test_skip_unless_db_feature.<locals>.SkipTestCase) " "SkippingTestCase.test_skip_unless_db_feature.<locals>.SkipTestCase%s) "
"as SkippingTestCase.test_skip_unless_db_feature.<locals>.SkipTestCase " "as SkippingTestCase.test_skip_unless_db_feature.<locals>.SkipTestCase "
"doesn't allow queries against the 'default' database.", "doesn't allow queries against the 'default' database."
# Python 3.11 uses fully qualified test name in the output.
% (".test_foo" if PY311 else ""),
) )
def test_skip_if_db_feature(self): def test_skip_if_db_feature(self):
@ -145,9 +148,11 @@ class SkippingTestCase(SimpleTestCase):
SkipTestCase("test_foo").test_foo, SkipTestCase("test_foo").test_foo,
ValueError, ValueError,
"skipIfDBFeature cannot be used on test_foo (test_utils.tests." "skipIfDBFeature cannot be used on test_foo (test_utils.tests."
"SkippingTestCase.test_skip_if_db_feature.<locals>.SkipTestCase) " "SkippingTestCase.test_skip_if_db_feature.<locals>.SkipTestCase%s) "
"as SkippingTestCase.test_skip_if_db_feature.<locals>.SkipTestCase " "as SkippingTestCase.test_skip_if_db_feature.<locals>.SkipTestCase "
"doesn't allow queries against the 'default' database.", "doesn't allow queries against the 'default' database."
# Python 3.11 uses fully qualified test name in the output.
% (".test_foo" if PY311 else ""),
) )