Skip to content

Commit

Permalink
Call Django's TestCase.setUpClass() when the database guaranteed to be
Browse files Browse the repository at this point in the history
available. Fixes #189, #79, #117.
  • Loading branch information
pelme committed Jan 2, 2015
1 parent cce59c0 commit 8a9aacb
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import contextlib

import pytest
import new

from .django_compat import is_django_unittest
from .fixtures import (_django_db_setup, _live_server_helper, admin_client,
Expand Down Expand Up @@ -204,6 +205,21 @@ def pytest_configure():
_setup_django()


def pytest_runtest_setup(item):

if django_settings_is_configured() and is_django_unittest(item):
cls = item.cls

if hasattr(cls, '__real_setUpClass'):
return

cls.__real_setUpClass = cls.setUpClass
cls.__real_tearDownClass = cls.tearDownClass

cls.setUpClass = new.instancemethod(lambda cls: None, cls)
cls.tearDownClass = new.instancemethod(lambda cls: None, cls)


@pytest.fixture(autouse=True, scope='session')
def _django_test_environment(request):
"""
Expand Down Expand Up @@ -265,14 +281,21 @@ def _django_db_marker(request):
request.getfuncargvalue('db')


@pytest.fixture(autouse=True)
@pytest.fixture(autouse=True, scope='class')
def _django_setup_unittest(request, _django_cursor_wrapper):
"""Setup a django unittest, internal to pytest-django"""
if django_settings_is_configured() and is_django_unittest(request):
request.getfuncargvalue('_django_test_environment')
request.getfuncargvalue('_django_db_setup')

_django_cursor_wrapper.enable()
request.addfinalizer(_django_cursor_wrapper.disable)
request.node.cls.__real_setUpClass()

def teardown():
request.node.cls.__real_tearDownClass()
_django_cursor_wrapper.restore()

request.addfinalizer(teardown)


@pytest.fixture(autouse=True, scope='function')
Expand Down

0 comments on commit 8a9aacb

Please sign in to comment.