mirror of
https://github.com/django/django.git
synced 2024-12-22 17:16:24 +00:00
Fixed #35622 -- Made unittest ignore Django assertions in traceback frames.
Co-authored-by: Natalia <124304+nessita@users.noreply.github.com>
This commit is contained in:
parent
e1606d27b4
commit
9582745257
@ -6,6 +6,9 @@ from django.test.selenium import SeleniumTestCase
|
|||||||
from django.utils.deprecation import MiddlewareMixin
|
from django.utils.deprecation import MiddlewareMixin
|
||||||
from django.utils.translation import gettext as _
|
from django.utils.translation import gettext as _
|
||||||
|
|
||||||
|
# Make unittest ignore frames in this module when reporting failures.
|
||||||
|
__unittest = True
|
||||||
|
|
||||||
|
|
||||||
class CSPMiddleware(MiddlewareMixin):
|
class CSPMiddleware(MiddlewareMixin):
|
||||||
"""The admin's JavaScript should be compatible with CSP."""
|
"""The admin's JavaScript should be compatible with CSP."""
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
from .api import get_messages
|
from .api import get_messages
|
||||||
|
|
||||||
|
# Make unittest ignore frames in this module when reporting failures.
|
||||||
|
__unittest = True
|
||||||
|
|
||||||
|
|
||||||
class MessagesTestMixin:
|
class MessagesTestMixin:
|
||||||
def assertMessages(self, response, expected_messages, *, ordered=True):
|
def assertMessages(self, response, expected_messages, *, ordered=True):
|
||||||
|
@ -67,6 +67,9 @@ __all__ = (
|
|||||||
"skipUnlessDBFeature",
|
"skipUnlessDBFeature",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Make unittest ignore frames in this module when reporting failures.
|
||||||
|
__unittest = True
|
||||||
|
|
||||||
|
|
||||||
if not PY311:
|
if not PY311:
|
||||||
# Backport of unittest.case._enter_context() from Python 3.11.
|
# Backport of unittest.case._enter_context() from Python 3.11.
|
||||||
|
@ -240,7 +240,9 @@ Templates
|
|||||||
Tests
|
Tests
|
||||||
~~~~~
|
~~~~~
|
||||||
|
|
||||||
* ...
|
* Stack frames from Django's custom assertions are now hidden. This makes test
|
||||||
|
failures easier to read and enables :option:`test --pdb` to directly enter
|
||||||
|
into the failing test method.
|
||||||
|
|
||||||
URLs
|
URLs
|
||||||
~~~~
|
~~~~
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
import importlib
|
import importlib
|
||||||
import sys
|
import sys
|
||||||
|
import traceback
|
||||||
|
import unittest
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
@ -185,3 +187,17 @@ class AssertMessagesTest(MessagesTestMixin, SimpleTestCase):
|
|||||||
)
|
)
|
||||||
with self.assertRaisesMessage(AssertionError, msg):
|
with self.assertRaisesMessage(AssertionError, msg):
|
||||||
self.assertMessages(response, [])
|
self.assertMessages(response, [])
|
||||||
|
|
||||||
|
def test_method_frames_ignored_by_unittest(self):
|
||||||
|
response = FakeResponse()
|
||||||
|
try:
|
||||||
|
self.assertMessages(response, [object()])
|
||||||
|
except AssertionError:
|
||||||
|
exc_type, exc, tb = sys.exc_info()
|
||||||
|
|
||||||
|
result = unittest.TestResult()
|
||||||
|
result.addFailure(self, (exc_type, exc, tb))
|
||||||
|
stack = traceback.extract_tb(exc.__traceback__)
|
||||||
|
self.assertEqual(len(stack), 1)
|
||||||
|
# Top element in the stack is this method, not assertMessages.
|
||||||
|
self.assertEqual(stack[-1].name, "test_method_frames_ignored_by_unittest")
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import threading
|
import threading
|
||||||
|
import traceback
|
||||||
import unittest
|
import unittest
|
||||||
import warnings
|
import warnings
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
@ -1113,6 +1114,19 @@ class JSONEqualTests(SimpleTestCase):
|
|||||||
with self.assertRaises(AssertionError):
|
with self.assertRaises(AssertionError):
|
||||||
self.assertJSONNotEqual(valid_json, invalid_json)
|
self.assertJSONNotEqual(valid_json, invalid_json)
|
||||||
|
|
||||||
|
def test_method_frames_ignored_by_unittest(self):
|
||||||
|
try:
|
||||||
|
self.assertJSONEqual("1", "2")
|
||||||
|
except AssertionError:
|
||||||
|
exc_type, exc, tb = sys.exc_info()
|
||||||
|
|
||||||
|
result = unittest.TestResult()
|
||||||
|
result.addFailure(self, (exc_type, exc, tb))
|
||||||
|
stack = traceback.extract_tb(exc.__traceback__)
|
||||||
|
self.assertEqual(len(stack), 1)
|
||||||
|
# Top element in the stack is this method, not assertJSONEqual.
|
||||||
|
self.assertEqual(stack[-1].name, "test_method_frames_ignored_by_unittest")
|
||||||
|
|
||||||
|
|
||||||
class XMLEqualTests(SimpleTestCase):
|
class XMLEqualTests(SimpleTestCase):
|
||||||
def test_simple_equal(self):
|
def test_simple_equal(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user