mirror of
https://github.com/django/django.git
synced 2024-12-22 00:55:44 +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.translation import gettext as _
|
||||
|
||||
# Make unittest ignore frames in this module when reporting failures.
|
||||
__unittest = True
|
||||
|
||||
|
||||
class CSPMiddleware(MiddlewareMixin):
|
||||
"""The admin's JavaScript should be compatible with CSP."""
|
||||
|
@ -1,5 +1,8 @@
|
||||
from .api import get_messages
|
||||
|
||||
# Make unittest ignore frames in this module when reporting failures.
|
||||
__unittest = True
|
||||
|
||||
|
||||
class MessagesTestMixin:
|
||||
def assertMessages(self, response, expected_messages, *, ordered=True):
|
||||
|
@ -67,6 +67,9 @@ __all__ = (
|
||||
"skipUnlessDBFeature",
|
||||
)
|
||||
|
||||
# Make unittest ignore frames in this module when reporting failures.
|
||||
__unittest = True
|
||||
|
||||
|
||||
if not PY311:
|
||||
# Backport of unittest.case._enter_context() from Python 3.11.
|
||||
|
@ -240,7 +240,9 @@ Templates
|
||||
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
|
||||
~~~~
|
||||
|
@ -1,5 +1,7 @@
|
||||
import importlib
|
||||
import sys
|
||||
import traceback
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
from django.conf import settings
|
||||
@ -185,3 +187,17 @@ class AssertMessagesTest(MessagesTestMixin, SimpleTestCase):
|
||||
)
|
||||
with self.assertRaisesMessage(AssertionError, msg):
|
||||
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 sys
|
||||
import threading
|
||||
import traceback
|
||||
import unittest
|
||||
import warnings
|
||||
from io import StringIO
|
||||
@ -1113,6 +1114,19 @@ class JSONEqualTests(SimpleTestCase):
|
||||
with self.assertRaises(AssertionError):
|
||||
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):
|
||||
def test_simple_equal(self):
|
||||
|
Loading…
Reference in New Issue
Block a user