Skip to content

Django unittests: restore "debug" function #771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,9 @@ def _django_setup_unittest(request, django_db_blocker):

cls = request.node.cls

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

orig_debug = cls.debug
cls.debug = _cleaning_debug

with django_db_blocker.unblock():
Expand All @@ -551,6 +553,8 @@ def _cleaning_debug(self):
else:
yield

cls.debug = orig_debug


@pytest.fixture(scope="function", autouse=True)
def _dj_autoclear_mailbox():
Expand Down
31 changes: 31 additions & 0 deletions tests/test_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,3 +456,34 @@ def tearDown(self):

result = django_testdir.runpytest_subprocess("-v", "--pdb")
assert result.ret == 0


def test_debug_restored(django_testdir):
django_testdir.create_test_module(
"""
from django.test import TestCase

pre_setup_count = 0


class TestClass1(TestCase):

def test_method(self):
pass


class TestClass2(TestClass1):

def _pre_setup(self):
global pre_setup_count
pre_setup_count += 1
super(TestClass2, self)._pre_setup()

def test_method(self):
assert pre_setup_count == 1
"""
)

result = django_testdir.runpytest_subprocess("--pdb")
result.stdout.fnmatch_lines(["*= 2 passed in *"])
assert result.ret == 0