Skip to content

Commit ec30dcd

Browse files
authored
Django unittests: restore "debug" function (#771)
Fixes #769.
1 parent 0dbe355 commit ec30dcd

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

pytest_django/plugin.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -522,8 +522,9 @@ def _django_setup_unittest(request, django_db_blocker):
522522

523523
cls = request.node.cls
524524

525-
# implement missing (as of 1.10) debug() method for django's TestCase
526-
# see pytest-dev/pytest-django#406
525+
# Implement missing (as of 2.2) debug() wrapper/method for Django's TestCase.
526+
# See pytest-dev/pytest-django#406.
527+
# Pending PR for Django: https://github.com/django/django/pull/7436.
527528
def _cleaning_debug(self):
528529
testMethod = getattr(self, self._testMethodName)
529530
skipped = getattr(self.__class__, "__unittest_skip__", False) or getattr(
@@ -536,6 +537,7 @@ def _cleaning_debug(self):
536537
if not skipped:
537538
self._post_teardown()
538539

540+
orig_debug = cls.debug
539541
cls.debug = _cleaning_debug
540542

541543
with django_db_blocker.unblock():
@@ -551,6 +553,8 @@ def _cleaning_debug(self):
551553
else:
552554
yield
553555

556+
cls.debug = orig_debug
557+
554558

555559
@pytest.fixture(scope="function", autouse=True)
556560
def _dj_autoclear_mailbox():

tests/test_unittest.py

+31
Original file line numberDiff line numberDiff line change
@@ -456,3 +456,34 @@ def tearDown(self):
456456

457457
result = django_testdir.runpytest_subprocess("-v", "--pdb")
458458
assert result.ret == 0
459+
460+
461+
def test_debug_restored(django_testdir):
462+
django_testdir.create_test_module(
463+
"""
464+
from django.test import TestCase
465+
466+
pre_setup_count = 0
467+
468+
469+
class TestClass1(TestCase):
470+
471+
def test_method(self):
472+
pass
473+
474+
475+
class TestClass2(TestClass1):
476+
477+
def _pre_setup(self):
478+
global pre_setup_count
479+
pre_setup_count += 1
480+
super(TestClass2, self)._pre_setup()
481+
482+
def test_method(self):
483+
assert pre_setup_count == 1
484+
"""
485+
)
486+
487+
result = django_testdir.runpytest_subprocess("--pdb")
488+
result.stdout.fnmatch_lines(["*= 2 passed in *"])
489+
assert result.ret == 0

0 commit comments

Comments
 (0)