Skip to content

Commit 3d71b6f

Browse files
asfaltboypelme
authored andcommitted
Handle TestCase with when using --pdb option (#406)
This commit works around the root cause in Django: https://code.djangoproject.com/ticket/27391
1 parent 5455dca commit 3d71b6f

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

pytest_django/plugin.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,22 @@ def _django_setup_unittest(request, django_db_blocker):
387387

388388
cls = request.node.cls
389389

390+
# implement missing (as of 1.10) debug() method for django's TestCase
391+
# see pytest-dev/pytest-django#406
392+
def _cleaning_debug(self):
393+
testMethod = getattr(self, self._testMethodName)
394+
skipped = (
395+
getattr(self.__class__, "__unittest_skip__", False) or
396+
getattr(testMethod, "__unittest_skip__", False))
397+
398+
if not skipped:
399+
self._pre_setup()
400+
super(cls, self).debug()
401+
if not skipped:
402+
self._post_teardown()
403+
404+
cls.debug = _cleaning_debug
405+
390406
_restore_class_methods(cls)
391407
cls.setUpClass()
392408
_disable_class_methods(cls)

tests/test_unittest.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,46 @@ class TestCaseWithTrDbFixture(TestCase):
235235
def test_simple(self):
236236
# We only want to check setup/teardown does not conflict
237237
assert 1
238+
239+
240+
def test_pdb_enabled(django_testdir):
241+
"""
242+
Make sure the database is flushed and tests are isolated when
243+
using the --pdb option.
244+
245+
See issue #405 for details:
246+
https://github.com/pytest-dev/pytest-django/issues/405
247+
"""
248+
249+
django_testdir.create_test_module('''
250+
import os
251+
252+
from django.test import TestCase
253+
from django.conf import settings
254+
255+
from .app.models import Item
256+
257+
class TestPDBIsolation(TestCase):
258+
def setUp(self):
259+
"""setUp should be called after starting a transaction"""
260+
assert Item.objects.count() == 0
261+
Item.objects.create(name='Some item')
262+
Item.objects.create(name='Some item again')
263+
264+
def test_count(self):
265+
self.assertEqual(Item.objects.count(), 2)
266+
assert Item.objects.count() == 2
267+
Item.objects.create(name='Foo')
268+
self.assertEqual(Item.objects.count(), 3)
269+
270+
def test_count_again(self):
271+
self.test_count()
272+
273+
def tearDown(self):
274+
"""tearDown should be called before rolling back the database"""
275+
assert Item.objects.count() == 3
276+
277+
''')
278+
279+
result = django_testdir.runpytest_subprocess('-v', '--pdb')
280+
assert result.ret == 0

0 commit comments

Comments
 (0)