Skip to content

Add a failing unittest case when using --pdb option #406

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 2 commits into from
Nov 21, 2016
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
16 changes: 16 additions & 0 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,22 @@ 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
def _cleaning_debug(self):
testMethod = getattr(self, self._testMethodName)
skipped = (
getattr(self.__class__, "__unittest_skip__", False) or
getattr(testMethod, "__unittest_skip__", False))

if not skipped:
self._pre_setup()
super(cls, self).debug()
if not skipped:
self._post_teardown()

cls.debug = _cleaning_debug

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a comment/doc!

It was mentioned to report it for Django itself also.. has that been done?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment added in 74bef8d. I'm not sure this deserves mention in docs (besides maybe in the changelog). I'll submit a bug report with Django when I get a chance.

_restore_class_methods(cls)
cls.setUpClass()
_disable_class_methods(cls)
Expand Down
43 changes: 43 additions & 0 deletions tests/test_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,46 @@ class TestCaseWithTrDbFixture(TestCase):
def test_simple(self):
# We only want to check setup/teardown does not conflict
assert 1


def test_pdb_enabled(django_testdir):
"""
Make sure the database is flushed and tests are isolated when
using the --pdb option.

See issue #405 for details:
https://github.com/pytest-dev/pytest-django/issues/405
"""

django_testdir.create_test_module('''
import os

from django.test import TestCase
from django.conf import settings

from .app.models import Item

class TestPDBIsolation(TestCase):
def setUp(self):
"""setUp should be called after starting a transaction"""
assert Item.objects.count() == 0
Item.objects.create(name='Some item')
Item.objects.create(name='Some item again')

def test_count(self):
self.assertEqual(Item.objects.count(), 2)
assert Item.objects.count() == 2
Item.objects.create(name='Foo')
self.assertEqual(Item.objects.count(), 3)

def test_count_again(self):
self.test_count()

def tearDown(self):
"""tearDown should be called before rolling back the database"""
assert Item.objects.count() == 3

''')

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